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 com.legstar.coxb.CobolElement;
14  import com.legstar.coxb.ICobolArrayFloatBinding;
15  import com.legstar.coxb.ICobolComplexBinding;
16  import com.legstar.coxb.CobolElementVisitor;
17  import com.legstar.coxb.common.CArrayBinding;
18  import com.legstar.coxb.host.HostException;
19  
20  import java.math.BigDecimal;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  /**
25   * This class implements the behavior of an array of comp-1 cobol elements
26   * bound to a JAXB Float property.
27   *
28   * @author Fady Moussallam
29   * 
30   */
31  public class CArrayFloatBinding extends CArrayBinding implements ICobolArrayFloatBinding {
32  
33      /** The current list for this array. */
34      private List < Float > mList = null;
35  
36      /**
37       * Constructor for a cobol element to java binding.
38       * 
39       * @param bindingName the identifier for this binding
40       * @param jaxbName the name of the bound java property
41       * @param jaxbType the type of the bound java property
42       * @param cobolAnnotations the cobol annotations for this element
43       * @param parentBinding a reference to the parent binding if any
44       */
45      public CArrayFloatBinding(
46              final String bindingName,
47              final String jaxbName,
48              final Class < ? > jaxbType,
49              final CobolElement cobolAnnotations,
50              final ICobolComplexBinding parentBinding) {
51          super(bindingName, jaxbName, jaxbType, cobolAnnotations, parentBinding);
52      }
53  
54      /** {@inheritDoc} */
55      public void accept(final CobolElementVisitor cev)
56      throws HostException {
57          cev.visit(this);
58      }
59  
60      /** {@inheritDoc} */
61      public int calcItemByteLength() {
62          return CFloatBinding.calcFloatByteLength();
63      }
64  
65      /**
66       * @return the List of items
67       */
68      public List < Float > getFloatList() {
69          return mList;
70      }
71  
72      /**
73       * @param list the items List to set
74       */
75      public void setFloatList(
76              final List < Float > list) {
77          mList = list;
78      }
79  
80      /**
81       * @return the internal List as BigDecimals
82       */
83      public List < BigDecimal > getBigDecimalList() {
84          List < BigDecimal > list = new ArrayList < BigDecimal >();
85          for (Float value : mList) {
86              list.add(new BigDecimal(value));
87          }
88          return list;
89      }
90  
91      /**
92       * @param list the internal List of BigDecimals to set
93       */
94      public void setBigDecimalList(
95              final List < BigDecimal > list) {
96          mList = new ArrayList < Float >();
97          for (BigDecimal value : list) {
98              mList.add(value.floatValue());
99          }
100     }
101 
102     /** {@inheritDoc} */
103     public Object getObjectValue(
104             final Class < ? > type) throws HostException {
105         if (type.equals(Float.class)) {
106             return mList;
107         } else if (type.equals(BigDecimal.class)) {
108             return getBigDecimalList();
109         } else {
110             throw new HostException("Attempt to get binding " + getBindingName()
111                     + " as an incompatible type " + type);
112         }
113     }
114 
115     /** {@inheritDoc} */
116     @SuppressWarnings("unchecked")
117     public void setObjectValue(final Object value) throws HostException {
118         if (value == null) {
119             mList = null;
120             return;
121         }
122         if (value instanceof List) {
123             if (((List < ? >) value).size() == 0) {
124                 mList = new ArrayList < Float >();
125                 return;
126             }
127             /* We assume all items will have the same type as the first one.
128              * The unchecked cast might break at runtime. */
129             Object item = ((List < ? >) value).get(0);
130             if (item instanceof Float) {
131                 mList = (List < Float >) value;
132                 return;
133             } else if (item instanceof BigDecimal) {
134                 setBigDecimalList((List < BigDecimal >) value);
135                 return;
136             }
137         }
138         throw new HostException("Attempt to set binding " + getBindingName()
139                 + " from an incompatible value " + value);
140     }
141 
142     /** {@inheritDoc} */
143     public boolean isSet() {
144         return (mList != null);
145     }
146 
147 }