Decimal2String.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.utils.jaxb;

  21. import java.text.DecimalFormat;
  22. import java.text.DecimalFormatSymbols;
  23. import java.util.Locale;

  24. import javax.xml.bind.annotation.adapters.XmlAdapter;

  25. /**
  26.  * DateTime2Date
  27.  *
  28.  *
  29.  * @author Andrea Poli (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class Decimal2String extends XmlAdapter<String, DecimalWrapper>
  34. {
  35.     @Override
  36.     public String marshal(DecimalWrapper v) throws Exception {
  37.         if(v==null){
  38.             return null;
  39.         }
  40.         StringBuilder pattern = new StringBuilder();
  41.         for (int i = 0; i < v.getMaxInteger(); i++) {
  42.             pattern.append("0");
  43.         }
  44.         if(v.getMaxDecimal()>0){
  45.             pattern.append(".");
  46.             for (int i = 0; i < v.getMaxDecimal(); i++) {
  47.                 pattern.append("0");
  48.             }  
  49.         }
  50.         DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); // per avere la punteggiatura come separatore
  51.         DecimalFormat df = new DecimalFormat(pattern.toString(),dfs);

  52.         //System.out.println("MARSHALL -> ["+pattern.toString()+"] int["+v.getInteger()+"] dec["+v.getDecimal()+"] ["+df.format(v.getObject())+"]");
  53.         String value =  df.format(v.getObject());
  54.        
  55.         if( (v.getMinInteger()!=v.getMaxInteger())
  56.                 ||
  57.                 (v.getMinDecimal()!=v.getMaxDecimal())  ){
  58.             //System.out.println("VALORE OTTENUTO ["+value+"]");
  59.             if(value.contains(".")){
  60.                 String [] split = value.split("\\.");
  61.                 String left = split[0];
  62.                 String right = split[1];
  63.                 if(v.getMinInteger()!=v.getMaxInteger()){
  64.                     left = this.getLeftMinString(split[0], v.getMinInteger());
  65.                 }
  66.                 if(v.getMinDecimal()!=v.getMaxDecimal()){
  67.                     right = this.getRightMinString(split[1], v.getMinDecimal());
  68.                 }
  69.                 value =  left + "." + right ;
  70.             }else{
  71.                 value = this.getLeftMinString(value, v.getMinInteger());
  72.             }
  73.             //System.out.println("VALORE CORRETTO ["+value+"]");
  74.             return value;
  75.         }else{
  76.             return value;
  77.         }
  78.     }
  79.    
  80.     private String getLeftMinString(String value,int min){
  81.         StringBuilder bf = new StringBuilder();
  82.         for (int i = (value.length()-1); i >= 0; i--) {
  83.             if(value.charAt(i) != '0' || !this.onlyLeftZero(value, i)){
  84.                 bf.append(value.charAt(i));
  85.             }
  86.             else{
  87.                 if(bf.length()>=min){
  88.                     break;
  89.                 }
  90.                 else{
  91.                     bf.append(value.charAt(i));
  92.                 }
  93.             }
  94.         }
  95.         return bf.reverse().toString();
  96.     }
  97.     private String getRightMinString(String value,int min){
  98.         StringBuilder bf = new StringBuilder();
  99.         for (int i = 0; i <value.length(); i++) {
  100.             if(value.charAt(i) != '0' || !this.onlyRightZero(value, i)){
  101.                 bf.append(value.charAt(i));
  102.             }
  103.             else{
  104.                 if(bf.length()>=min){
  105.                     break;
  106.                 }
  107.                 else{
  108.                     bf.append(value.charAt(i));
  109.                 }
  110.             }
  111.         }
  112.         return bf.reverse().toString();
  113.     }
  114.    
  115.     private boolean onlyLeftZero(String value, int fromIndex){
  116.         for (int i = fromIndex; i >=0; i--) {
  117.             if(value.charAt(i) != '0'){
  118.                 return false;
  119.             }
  120.         }
  121.         return true;
  122.     }
  123.     private boolean onlyRightZero(String value, int fromIndex){
  124.         for (int i = fromIndex; i <value.length(); i++) {
  125.             if(value.charAt(i) != '0'){
  126.                 return false;
  127.             }
  128.         }
  129.         return true;
  130.     }
  131.    
  132.     @Override
  133.     public DecimalWrapper unmarshal(String sParam) throws Exception {
  134.         if(sParam==null){
  135.             return null;
  136.         }
  137.         String s = sParam.trim();
  138.         StringBuilder pattern = new StringBuilder();
  139.         DecimalWrapper dw = new DecimalWrapper();
  140.         if(s.contains(".")){
  141.             String [] split = s.split("\\.");
  142.             dw.setMinInteger(split[0].length());
  143.             dw.setMaxInteger(split[0].length());
  144.             for (int i = 0; i < split[0].length(); i++) {
  145.                 pattern.append("0");
  146.             }
  147.             pattern.append(".");
  148.             dw.setMinDecimal(split[1].length());
  149.             dw.setMaxDecimal(split[1].length());
  150.             for (int i = 0; i < split[1].length(); i++) {
  151.                 pattern.append("0");
  152.             }
  153.         }
  154.         else{
  155.             dw.setMinInteger(s.length());
  156.             dw.setMaxInteger(s.length());
  157.             for (int i = 0; i < s.length(); i++) {
  158.                 pattern.append("0");
  159.             }
  160.         }
  161.        
  162.         DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); // per avere la punteggiatura come separatore
  163.         DecimalFormat df = new DecimalFormat(pattern.toString(),dfs);
  164.         Object o = df.parseObject(s);
  165.         dw.setObject(o);
  166.        
  167.         //System.out.println("UNMARSHALL -> ["+pattern.toString()+"] string["+s+"] ["+dw.getObject()+"] min["+dw.getInteger()+"] dec["+dw.getDecimal()+"]");
  168.        
  169.         return dw;
  170.     }
  171. }