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.math.BigDecimal;
14  import java.math.BigInteger;
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import com.legstar.coxb.CobolElement;
19  import com.legstar.coxb.ICobolComplexBinding;
20  import com.legstar.coxb.common.CArrayBinding;
21  import com.legstar.coxb.host.HostException;
22  
23  /**
24   * Generic class for numeric arrays bindings.
25   * 
26   */
27  public abstract class AbstractArrayNumericBinding extends CArrayBinding {
28  
29      /** The current list for this array. */
30      private List < BigDecimal > mList = 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 if any
40       */
41      public AbstractArrayNumericBinding(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      /**
49       * @return the internal List as Bytes
50       */
51      public List < Byte > getByteList() {
52          List < Byte > list = new ArrayList < Byte >();
53          for (BigDecimal value : mList) {
54              list.add(value.byteValue());
55          }
56          return list;
57      }
58  
59      /**
60       * @param list the internal List of Bytes to set
61       */
62      public void setByteList(final List < Byte > list) {
63          mList = new ArrayList < BigDecimal >();
64          for (Byte value : list) {
65              mList.add(new BigDecimal(value));
66          }
67      }
68  
69      /**
70       * @return the internal List as Shorts
71       */
72      public List < Short > getShortList() {
73          List < Short > list = new ArrayList < Short >();
74          for (BigDecimal value : mList) {
75              list.add(value.shortValue());
76          }
77          return list;
78      }
79  
80      /**
81       * @param list the internal List of Shorts to set
82       */
83      public void setShortList(final List < Short > list) {
84          mList = new ArrayList < BigDecimal >();
85          for (Short value : list) {
86              mList.add(new BigDecimal(value));
87          }
88      }
89  
90      /**
91       * @return the internal List as Integers
92       */
93      public List < Integer > getIntegerList() {
94          List < Integer > list = new ArrayList < Integer >();
95          for (BigDecimal value : mList) {
96              list.add(value.intValue());
97          }
98          return list;
99      }
100 
101     /**
102      * @param list the internal List of Integers to set
103      */
104     public void setIntegerList(final List < Integer > list) {
105         mList = new ArrayList < BigDecimal >();
106         for (Integer value : list) {
107             mList.add(new BigDecimal(value));
108         }
109     }
110 
111     /**
112      * @return the internal List as Longs
113      */
114     public List < Long > getLongList() {
115         List < Long > list = new ArrayList < Long >();
116         for (BigDecimal value : mList) {
117             list.add(value.longValue());
118         }
119         return list;
120     }
121 
122     /**
123      * @param list the internal List of Longs to set
124      */
125     public void setLongList(final List < Long > list) {
126         mList = new ArrayList < BigDecimal >();
127         for (Long value : list) {
128             mList.add(new BigDecimal(value));
129         }
130     }
131 
132     /**
133      * @return the internal List of BigDecimals
134      */
135     public List < BigDecimal > getBigDecimalList() {
136         return mList;
137     }
138 
139     /**
140      * @param list the internal List of BigDecimals to set
141      */
142     public void setBigDecimalList(final List < BigDecimal > list) {
143         mList = list;
144     }
145 
146     /**
147      * @return the internal List as BigIntegers
148      */
149     public List < BigInteger > getBigIntegerList() {
150         List < BigInteger > list = new ArrayList < BigInteger >();
151         for (BigDecimal value : mList) {
152             list.add(value.toBigInteger());
153         }
154         return list;
155     }
156 
157     /**
158      * @param list the internal List of BigIntegers to set
159      */
160     public void setBigIntegerList(final List < BigInteger > list) {
161         mList = new ArrayList < BigDecimal >();
162         for (BigInteger value : list) {
163             mList.add(new BigDecimal(value));
164         }
165     }
166 
167     /**
168      * @return the internal List as Boolean
169      */
170     public List < Boolean > getBooleanList() {
171         List < Boolean > list = new ArrayList < Boolean >();
172         for (BigDecimal value : mList) {
173             if (value.intValue() == 0) {
174                 list.add(Boolean.FALSE);
175             } else {
176                 list.add(Boolean.TRUE);
177             }
178         }
179         return list;
180     }
181 
182     /**
183      * @param list the internal List of Boolean to set
184      */
185     public void setBooleanList(final List < Boolean > list) {
186         mList = new ArrayList < BigDecimal >();
187         for (Boolean value : list) {
188             mList.add(new BigDecimal((value) ? "1" : "0"));
189         }
190     }
191 
192     /** {@inheritDoc} */
193     public Object getObjectValue(final Class < ? > type) throws HostException {
194         if (type.equals(BigDecimal.class)) {
195             return mList;
196         } else if (type.equals(BigInteger.class)) {
197             return getBigIntegerList();
198         } else if (type.equals(Long.class) || type.equals(long.class)) {
199             return getLongList();
200         } else if (type.equals(Integer.class) || type.equals(int.class)) {
201             return getIntegerList();
202         } else if (type.equals(Short.class) || type.equals(short.class)) {
203             return getShortList();
204         } else if (type.equals(Byte.class) || type.equals(byte.class)) {
205             return getByteList();
206         } else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
207             return getBooleanList();
208         } else {
209             throw new HostException("Attempt to get binding "
210                     + getBindingName() + " as an incompatible type " + type);
211         }
212     }
213 
214     /** {@inheritDoc} */
215     @SuppressWarnings("unchecked")
216     public void setObjectValue(final Object value) throws HostException {
217         if (value == null) {
218             mList = null;
219             return;
220         }
221         if (value instanceof List) {
222             if (((List < ? >) value).size() == 0) {
223                 mList = new ArrayList < BigDecimal >();
224                 return;
225             }
226             /*
227              * We assume all items will have the same type as the first one. The
228              * unchecked cast might break at runtime.
229              */
230             Object item = ((List < ? >) value).get(0);
231             if (item instanceof BigDecimal) {
232                 mList = (List < BigDecimal >) value;
233                 return;
234             } else if (item instanceof BigInteger) {
235                 setBigIntegerList((List < BigInteger >) value);
236                 return;
237             } else if (item instanceof Long) {
238                 setLongList((List < Long >) value);
239                 return;
240             } else if (item instanceof Integer) {
241                 setIntegerList((List < Integer >) value);
242                 return;
243             } else if (item instanceof Short) {
244                 setShortList((List < Short >) value);
245                 return;
246             } else if (item instanceof Byte) {
247                 setByteList((List < Byte >) value);
248                 return;
249             } else if (item instanceof Boolean) {
250                 setBooleanList((List < Boolean >) value);
251                 return;
252             }
253         }
254         throw new HostException("Attempt to set binding " + getBindingName()
255                 + " from an incompatible value " + value);
256     }
257 
258     /** {@inheritDoc} */
259     public boolean isSet() {
260         return (mList != null);
261     }
262 }