DecimalWrapper.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.io.Serializable;
  22. import java.math.BigDecimal;
  23. import java.math.BigInteger;

  24. /**
  25.  * DecimalWrapper
  26.  *
  27.  *
  28.  * @author Andrea Poli (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class DecimalWrapper implements Serializable {

  33.     private static final long serialVersionUID = 1L;
  34.    
  35.     private int minInteger = 0;
  36.     private int minDecimal = 0;
  37.     private int maxInteger = 0;
  38.     private int maxDecimal = 0;
  39.     private Object object;

  40.     public DecimalWrapper(){}
  41.     public DecimalWrapper(int minInteger, int maxInteger, Object object){
  42.         this.minInteger = minInteger;
  43.         this.maxInteger = maxInteger;
  44.         this.object = object;
  45.     }
  46.     public DecimalWrapper(int minInteger, int maxInteger,
  47.             int minDecimal, int maxDecimal,
  48.             Object object){
  49.         this.minInteger = minInteger;
  50.         this.maxInteger = maxInteger;
  51.         this.minDecimal = minDecimal;
  52.         this.maxDecimal = maxDecimal;
  53.         this.object = object;
  54.     }
  55.    
  56.    
  57.     public int getMinInteger() {
  58.         return this.minInteger;
  59.     }
  60.     public void setMinInteger(int minInteger) {
  61.         this.minInteger = minInteger;
  62.     }
  63.     public int getMinDecimal() {
  64.         return this.minDecimal;
  65.     }
  66.     public void setMinDecimal(int minDecimal) {
  67.         this.minDecimal = minDecimal;
  68.     }
  69.     public int getMaxInteger() {
  70.         return this.maxInteger;
  71.     }
  72.     public void setMaxInteger(int maxInteger) {
  73.         this.maxInteger = maxInteger;
  74.     }
  75.     public int getMaxDecimal() {
  76.         return this.maxDecimal;
  77.     }
  78.     public void setMaxDecimal(int maxDecimal) {
  79.         this.maxDecimal = maxDecimal;
  80.     }
  81.    
  82.    
  83.     public Object getObject() {
  84.         return this.object;
  85.     }
  86.     public Object getObject(Class<?> c) {
  87.         if(this.object==null){
  88.             return null;
  89.         }
  90.        
  91.         Float fValue = null;
  92.         Double dValue = null;
  93.         Long lValue = null;
  94.         Integer iValue = null;
  95.         Short sValue = null;
  96.         if(this.object instanceof Float){
  97.             fValue = (Float) this.object;
  98.         }
  99.         else if(this.object instanceof Double){
  100.             dValue = (Double) this.object;
  101.         }
  102.         else if(this.object instanceof Long){
  103.             lValue = (Long) this.object;
  104.         }
  105.         else if(this.object instanceof Integer){
  106.             iValue = (Integer) this.object;
  107.         }
  108.         else if(this.object instanceof Short){
  109.             sValue = (Short) this.object;
  110.         }
  111.         else if(this.object instanceof BigInteger){
  112.             BigInteger bigInteger = (BigInteger) this.object;
  113.             if(c.isAssignableFrom(BigInteger.class)){
  114.                 return bigInteger;
  115.             }
  116.             else if(c.isAssignableFrom(Float.class)){
  117.                 fValue = bigInteger.floatValue();
  118.             }
  119.             else if(c.isAssignableFrom(Double.class)){
  120.                 dValue = bigInteger.doubleValue();
  121.             }
  122.             else if(c.isAssignableFrom(Long.class)){
  123.                 lValue = bigInteger.longValue();
  124.             }
  125.             else if(c.isAssignableFrom(Integer.class)){
  126.                 iValue = bigInteger.intValue();
  127.             }
  128.             else if(c.isAssignableFrom(Short.class)){
  129.                 sValue = bigInteger.shortValue();
  130.             }
  131.             else {
  132.                 throw new RuntimeException("Tipo impostato dal Wrapper ["+this.object.getClass().getName()+"] non gestito rispetto al tipo richiesto '"+c.getName()+"'");
  133.             }
  134.         }
  135.         else if(this.object instanceof BigDecimal){
  136.             BigDecimal bigDecimal = (BigDecimal) this.object;
  137.             if(c.isAssignableFrom(BigInteger.class)){
  138.                 return bigDecimal;
  139.             }
  140.             else if(c.isAssignableFrom(Float.class)){
  141.                 fValue = bigDecimal.floatValue();
  142.             }
  143.             else if(c.isAssignableFrom(Double.class)){
  144.                 dValue = bigDecimal.doubleValue();
  145.             }
  146.             else if(c.isAssignableFrom(Long.class)){
  147.                 lValue = bigDecimal.longValue();
  148.             }
  149.             else if(c.isAssignableFrom(Integer.class)){
  150.                 iValue = bigDecimal.intValue();
  151.             }
  152.             else if(c.isAssignableFrom(Short.class)){
  153.                 sValue = bigDecimal.shortValue();
  154.             }
  155.             else {
  156.                 throw new RuntimeException("Tipo impostato dal Wrapper ["+this.object.getClass().getName()+"] non gestito rispetto al tipo richiesto '"+c.getName()+"'");
  157.             }
  158.         }
  159.         else{
  160.             throw new RuntimeException("Tipo impostato dal Wrapper ["+this.object.getClass().getName()+"] non gestito");
  161.         }
  162.        
  163.        
  164.         if(c.isAssignableFrom(Float.class)){
  165.             if(fValue!=null){
  166.                 return (Float) fValue;
  167.             }
  168.             else if(dValue!=null){
  169.                 return dValue.floatValue();
  170.             }
  171.             else if(lValue!=null){
  172.                 return lValue.floatValue();
  173.             }
  174.             else if(iValue!=null){
  175.                 return iValue.floatValue();
  176.             }
  177.             else if(sValue!=null){
  178.                 return  sValue.floatValue();
  179.             }
  180.         }
  181.         else if(c.isAssignableFrom(Double.class)){
  182.             if(fValue!=null){
  183.                 return fValue.doubleValue();
  184.             }
  185.             else if(dValue!=null){
  186.                 return dValue;
  187.             }
  188.             else if(lValue!=null){
  189.                 return lValue.doubleValue();
  190.             }
  191.             else if(iValue!=null){
  192.                 return iValue.doubleValue();
  193.             }
  194.             else if(sValue!=null){
  195.                 return sValue.doubleValue();
  196.             }
  197.         }
  198.         else if(c.isAssignableFrom(Long.class)){
  199.             if(fValue!=null){
  200.                 return fValue.longValue();
  201.             }
  202.             else if(dValue!=null){
  203.                 return dValue.longValue();
  204.             }
  205.             else if(lValue!=null){
  206.                 return lValue;
  207.             }
  208.             else if(iValue!=null){
  209.                 return iValue.longValue();
  210.             }
  211.             else if(sValue!=null){
  212.                 return sValue.longValue();
  213.             }
  214.         }
  215.         else if(c.isAssignableFrom(Integer.class)){
  216.             if(fValue!=null){
  217.                 return fValue.intValue();
  218.             }
  219.             else if(dValue!=null){
  220.                 return dValue.intValue();
  221.             }
  222.             else if(lValue!=null){
  223.                 return lValue.intValue();
  224.             }
  225.             else if(iValue!=null){
  226.                 return iValue;
  227.             }
  228.             else if(sValue!=null){
  229.                 return sValue.intValue();
  230.             }
  231.         }
  232.         else if(c.isAssignableFrom(Short.class)){
  233.             if(fValue!=null){
  234.                 return fValue.shortValue();
  235.             }
  236.             else if(dValue!=null){
  237.                 return dValue.shortValue();
  238.             }
  239.             else if(lValue!=null){
  240.                 return lValue.shortValue();
  241.             }
  242.             else if(iValue!=null){
  243.                 return iValue.shortValue();
  244.             }
  245.             else if(sValue!=null){
  246.                 return sValue;
  247.             }
  248.         }
  249.         else{
  250.             throw new RuntimeException("Tipo richiesto ["+c.getName()+"] non gestito");
  251.         }
  252.        
  253.        
  254.         return this.object;
  255.     }
  256.     public void setObject(Object object) {
  257.         this.object = object;
  258.     }
  259. }