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