View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2015 LegSem.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the GNU Lesser Public License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7    * 
8    * Contributors:
9    *     LegSem - initial API and implementation
10   ******************************************************************************/
11  package com.legstar.coxb.impl;
12  
13  import java.lang.reflect.InvocationTargetException;
14  import java.lang.reflect.Method;
15  import java.math.BigDecimal;
16  import java.math.BigInteger;
17  
18  import com.legstar.coxb.CobolElement;
19  import com.legstar.coxb.ICobolComplexBinding;
20  import com.legstar.coxb.common.CBinding;
21  import com.legstar.coxb.host.HostException;
22  
23  /**
24   * A generic binding for numeric bindings. All numeric values are stored in a
25   * BigDecimal.
26   */
27  public abstract class AbstractNumericBinding extends CBinding {
28  
29      /** The current value for this element. */
30      private BigDecimal mValue = null;
31  
32      /**
33       * Constructor for a cobol element to java binding.
34       * 
35       * @param bindingName the identifier for this binding
36       * @param jaxbName the name of the bound java property
37       * @param jaxbType the type of the bound java property
38       * @param cobolAnnotations the cobol annotations for this element
39       * @param parentBinding a reference to the parent binding
40       */
41      public AbstractNumericBinding(final String bindingName,
42              final String jaxbName, final Class < ? > jaxbType,
43              final CobolElement cobolAnnotations,
44              final ICobolComplexBinding parentBinding) {
45          super(bindingName, jaxbName, jaxbType, cobolAnnotations, parentBinding);
46      }
47  
48      /** {@inheritDoc} */
49      public void setByteValue(final Byte value) throws HostException {
50          mValue = new BigDecimal(value);
51      }
52  
53      /** {@inheritDoc} */
54      public Byte getByteValue() throws HostException {
55          return mValue.byteValue();
56      }
57  
58      /** {@inheritDoc} */
59      public void setShortValue(final Short value) throws HostException {
60          mValue = new BigDecimal(value);
61      }
62  
63      /** {@inheritDoc} */
64      public Short getShortValue() throws HostException {
65          return mValue.shortValue();
66      }
67  
68      /** {@inheritDoc} */
69      public void setIntegerValue(final Integer value) throws HostException {
70          mValue = new BigDecimal(value);
71      }
72  
73      /** {@inheritDoc} */
74      public Integer getIntegerValue() throws HostException {
75          return mValue.intValue();
76      }
77  
78      /** {@inheritDoc} */
79      public void setLongValue(final Long value) throws HostException {
80          mValue = new BigDecimal(value);
81      }
82  
83      /** {@inheritDoc} */
84      public Long getLongValue() throws HostException {
85          return mValue.longValue();
86      }
87  
88      /** {@inheritDoc} */
89      public void setFloatValue(final Float value) throws HostException {
90          mValue = new BigDecimal(value);
91      }
92  
93      /** {@inheritDoc} */
94      public Float getFloatValue() throws HostException {
95          return mValue.floatValue();
96      }
97  
98      /** {@inheritDoc} */
99      public void setDoubleValue(final Double value) throws HostException {
100         mValue = new BigDecimal(value);
101     }
102 
103     /** {@inheritDoc} */
104     public Double getDoubleValue() throws HostException {
105         return mValue.doubleValue();
106     }
107 
108     /** {@inheritDoc} */
109     public void setBigDecimalValue(final BigDecimal value) throws HostException {
110         mValue = value;
111     }
112 
113     /** {@inheritDoc} */
114     public BigDecimal getBigDecimalValue() throws HostException {
115         return mValue;
116     }
117 
118     /** {@inheritDoc} */
119     public void setBigIntegerValue(final BigInteger value) throws HostException {
120         mValue = new BigDecimal(value);
121     }
122 
123     /** {@inheritDoc} */
124     public BigInteger getBigIntegerValue() throws HostException {
125         return mValue.toBigInteger();
126     }
127 
128     /** {@inheritDoc} */
129     public void setBooleanValue(Boolean value) throws HostException {
130         mValue = new BigDecimal((value) ? "1" : "0");
131     }
132 
133     /** {@inheritDoc} */
134     public Boolean getBooleanValue() throws HostException {
135         if (mValue.intValue() == 0) {
136             return false;
137         }
138         return true;
139     }
140 
141     /**
142      * With enumerations we need to recover the actual value type using
143      * reflection.
144      * 
145      * @param type the enum member type
146      * @return an enum member
147      * @throws HostException if something wrong with the Enum methods
148      */
149     public Object getEnumValue(final Class < ? > type) throws HostException {
150         try {
151             Method value = type.getMethod("value");
152             Method fromValue = type.getMethod("fromValue",
153                     value.getReturnType());
154             return fromValue
155                     .invoke(null, getObjectValue(value.getReturnType()));
156         } catch (SecurityException e) {
157             throw new HostException(e);
158         } catch (IllegalArgumentException e) {
159             throw new HostException(e);
160         } catch (IllegalAccessException e) {
161             throw new HostException(e);
162         } catch (NoSuchMethodException e) {
163             throw new HostException(e);
164         } catch (InvocationTargetException e) {
165             throw new HostException(e);
166         }
167     }
168 
169     /**
170      * Enums cannot be manipulated directly because we have no idea what the
171      * values types actually are. Here we use reflection to recover the real
172      * value type.
173      * 
174      * @param value
175      * @throws HostException
176      */
177     public void setEnumValue(Enum < ? > value) throws HostException {
178         try {
179             Method valueMethod = value.getClass().getMethod("value");
180             Object numValue = valueMethod.invoke(value);
181             setObjectValue(numValue);
182         } catch (SecurityException e) {
183             throw new HostException(e);
184         } catch (IllegalArgumentException e) {
185             throw new HostException(e);
186         } catch (IllegalAccessException e) {
187             throw new HostException(e);
188         } catch (NoSuchMethodException e) {
189             throw new HostException(e);
190         } catch (InvocationTargetException e) {
191             throw new HostException(e);
192         }
193     }
194 
195     /** {@inheritDoc} */
196     public Object getObjectValue(final Class < ? > type) throws HostException {
197         if (type.equals(BigDecimal.class)) {
198             return mValue;
199         } else if (type.equals(BigInteger.class)) {
200             return getBigIntegerValue();
201         } else if (type.equals(Long.class) || type.equals(long.class)) {
202             return getLongValue();
203         } else if (type.equals(Float.class) || type.equals(float.class)) {
204             return getFloatValue();
205         } else if (type.equals(Double.class) || type.equals(double.class)) {
206             return getDoubleValue();
207         } else if (type.equals(Integer.class) || type.equals(int.class)) {
208             return getIntegerValue();
209         } else if (type.equals(Short.class) || type.equals(short.class)) {
210             return getShortValue();
211         } else if (type.equals(Byte.class) || type.equals(byte.class)) {
212             return getByteValue();
213         } else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
214             return getBooleanValue();
215         } else if (type.isEnum()) {
216             return getEnumValue(type);
217         } else {
218             throw new HostException("Attempt to get binding "
219                     + getBindingName() + " as an incompatible type " + type);
220         }
221     }
222 
223     /** {@inheritDoc} */
224     public void setObjectValue(final Object value) throws HostException {
225         if (value == null) {
226             mValue = null;
227             return;
228         }
229         if (value instanceof BigDecimal) {
230             mValue = (BigDecimal) value;
231         } else if (value instanceof BigInteger) {
232             setBigIntegerValue((BigInteger) value);
233         } else if (value instanceof Long) {
234             setLongValue((Long) value);
235         } else if (value instanceof Float) {
236             setFloatValue((Float) value);
237         } else if (value instanceof Double) {
238             setDoubleValue((Double) value);
239         } else if (value instanceof Integer) {
240             setIntegerValue((Integer) value);
241         } else if (value instanceof Short) {
242             setShortValue((Short) value);
243         } else if (value instanceof Byte) {
244             setByteValue((Byte) value);
245         } else if (value instanceof Boolean) {
246             setBooleanValue((Boolean) value);
247         } else if (value instanceof Enum) {
248             setEnumValue((Enum < ? >) value);
249         } else {
250             throw new HostException("Attempt to set binding "
251                     + getBindingName() + " from an incompatible value " + value);
252         }
253     }
254 
255     /** {@inheritDoc} */
256     public boolean isSet() {
257         return (mValue != null);
258     }
259 }