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 com.legstar.coxb.CobolElement;
15  import com.legstar.coxb.ICobolComplexBinding;
16  import com.legstar.coxb.ICobolDoubleBinding;
17  import com.legstar.coxb.CobolElementVisitor;
18  import com.legstar.coxb.common.CBinding;
19  import com.legstar.coxb.host.HostException;
20  
21  /**
22   * This class implements the behavior of a comp-2 cobol element bound to
23   * a JAXB Double property.
24   *
25   * @author Fady Moussallam
26   * 
27   */
28  public class CDoubleBinding extends CBinding implements ICobolDoubleBinding {
29  
30      /** The current value for this element. */
31      private Double mValue = null;
32      
33      /** Doubles have a fixed host byte length. */
34      public static final int BYTE_LENGTH = 8;
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
44       */
45      public CDoubleBinding(
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 Double getDoubleValue() throws HostException {
62          return mValue;
63      }
64  
65      /** {@inheritDoc} */
66      public void setDoubleValue(final Double value) throws HostException {
67          mValue = value;
68      }
69  
70      /** {@inheritDoc} */
71      public BigDecimal getBigDecimalValue() throws HostException {
72          return new BigDecimal(mValue);
73      }
74  
75      /** {@inheritDoc} */
76      public void setBigDecimalValue(
77              final BigDecimal value) throws HostException {
78          mValue = value.doubleValue();
79      }
80  
81      /** {@inheritDoc} */
82      public int calcByteLength() {
83          return calcDoubleByteLength();
84      }
85      
86      /**
87       * Calculates the host byte length for a COMP-2.
88       * @return the host byte length for a COMP-2
89       */
90      public static int calcDoubleByteLength() {
91          return BYTE_LENGTH;
92      }
93  
94      /** {@inheritDoc} */
95      public Object getObjectValue(
96              final Class < ? > type) throws HostException {
97          if (type.equals(Double.class) || type.equals(double.class)) {
98              return mValue;
99          } else if (type.equals(BigDecimal.class)) {
100             return getBigDecimalValue();
101         } else {
102             throw new HostException("Attempt to get binding " + getBindingName()
103                     + " as an incompatible type " + type);
104         }
105     }
106 
107     /** {@inheritDoc} */
108     public void setObjectValue(final Object value) throws HostException {
109         if (value == null) {
110             mValue = null;
111             return;
112         }
113         if (value instanceof Double) {
114             mValue = (Double) value;
115         } else if (value instanceof BigDecimal) {
116             setBigDecimalValue((BigDecimal) value);
117         } else {
118             throw new HostException("Attempt to set binding " + getBindingName()
119                     + " from an incompatible value " + value);
120         }
121     }
122 
123     /** {@inheritDoc} */
124     public boolean isSet() {
125         return (mValue != null);
126     }
127 }