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  
15  import com.legstar.coxb.CobolElement;
16  import com.legstar.coxb.ICobolComplexBinding;
17  import com.legstar.coxb.ICobolFloatBinding;
18  import com.legstar.coxb.CobolElementVisitor;
19  import com.legstar.coxb.common.CBinding;
20  import com.legstar.coxb.host.HostException;
21  
22  /**
23   * This class implements the behavior of a comp-1 cobol element bound to
24   * a JAXB Float property.
25   *
26   * @author Fady Moussallam
27   * 
28   */
29  public class CFloatBinding extends CBinding
30  implements ICobolFloatBinding {
31  
32      /** The current value for this element. */
33      private Float mValue = null;
34  
35      /** Floats have a fixed host byte length. */
36      public static final int BYTE_LENGTH = 4;
37      
38      /**
39       * Constructor for a cobol element to java binding.
40       * 
41       * @param bindingName the identifier for this binding
42       * @param jaxbName the name of the bound java property
43       * @param jaxbType the type of the bound java property
44       * @param cobolAnnotations the cobol annotations for this element
45       * @param parentBinding a reference to the parent binding
46       */
47      public CFloatBinding(
48              final String bindingName,
49              final String jaxbName,
50              final Class < ? > jaxbType,
51              final CobolElement cobolAnnotations,
52              final ICobolComplexBinding parentBinding) {
53          super(bindingName, jaxbName, jaxbType, cobolAnnotations, parentBinding);
54      }
55  
56      /** {@inheritDoc} */
57      public void accept(final CobolElementVisitor cev)
58      throws HostException {
59          cev.visit(this);
60      }
61  
62      /** {@inheritDoc} */
63      public Float getFloatValue() throws HostException {
64          return mValue;
65      }
66  
67      /** {@inheritDoc} */
68      public void setFloatValue(final Float value) throws HostException {
69          mValue = value;
70      }
71  
72      /** {@inheritDoc} */
73      public BigDecimal getBigDecimalValue() throws HostException {
74          return new BigDecimal(mValue);
75      }
76  
77      /** {@inheritDoc} */
78      public void setBigDecimalValue(
79              final BigDecimal value) throws HostException {
80          mValue = value.floatValue();
81      }
82  
83      /** {@inheritDoc} */
84      public int calcByteLength() {
85          return calcFloatByteLength();
86      }
87  
88      /**
89       * Calculates the host byte length for a COMP-1.
90       * @return the host byte length for a COMP-1
91       */
92      public static int calcFloatByteLength() {
93          return BYTE_LENGTH;
94      }
95  
96      /** {@inheritDoc} */
97      public Object getObjectValue(
98              final Class < ? > type) throws HostException {
99          if (type.equals(Float.class) || type.equals(float.class)) {
100             return mValue;
101         } else if (type.equals(BigDecimal.class)) {
102             return getBigDecimalValue();
103         } else {
104             throw new HostException("Attempt to get binding " + getBindingName()
105                     + " as an incompatible type " + type);
106         }
107     }
108 
109     /** {@inheritDoc} */
110     public void setObjectValue(final Object value) throws HostException {
111         if (value == null) {
112             mValue = null;
113             return;
114         }
115         if (value instanceof Float) {
116             mValue = (Float) value;
117         } else if (value instanceof BigDecimal) {
118             setBigDecimalValue((BigDecimal) value);
119         } else {
120             throw new HostException("Attempt to set binding " + getBindingName()
121                     + " from an incompatible value " + value);
122         }
123     }
124 
125     /** {@inheritDoc} */
126     public boolean isSet() {
127         return (mValue != null);
128     }
129 }