1
2
3
4
5
6
7
8
9
10
11 package com.legstar.coxb.impl;
12
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.math.BigDecimal;
16 import java.math.BigInteger;
17
18 import com.legstar.coxb.CobolElement;
19 import com.legstar.coxb.ICobolComplexBinding;
20 import com.legstar.coxb.common.CBinding;
21 import com.legstar.coxb.host.HostException;
22
23
24
25
26
27 public abstract class AbstractNumericBinding extends CBinding {
28
29
30 private BigDecimal mValue = null;
31
32
33
34
35
36
37
38
39
40
41 public AbstractNumericBinding(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 public void setByteValue(final Byte value) throws HostException {
50 mValue = new BigDecimal(value);
51 }
52
53
54 public Byte getByteValue() throws HostException {
55 return mValue.byteValue();
56 }
57
58
59 public void setShortValue(final Short value) throws HostException {
60 mValue = new BigDecimal(value);
61 }
62
63
64 public Short getShortValue() throws HostException {
65 return mValue.shortValue();
66 }
67
68
69 public void setIntegerValue(final Integer value) throws HostException {
70 mValue = new BigDecimal(value);
71 }
72
73
74 public Integer getIntegerValue() throws HostException {
75 return mValue.intValue();
76 }
77
78
79 public void setLongValue(final Long value) throws HostException {
80 mValue = new BigDecimal(value);
81 }
82
83
84 public Long getLongValue() throws HostException {
85 return mValue.longValue();
86 }
87
88
89 public void setFloatValue(final Float value) throws HostException {
90 mValue = new BigDecimal(value);
91 }
92
93
94 public Float getFloatValue() throws HostException {
95 return mValue.floatValue();
96 }
97
98
99 public void setDoubleValue(final Double value) throws HostException {
100 mValue = new BigDecimal(value);
101 }
102
103
104 public Double getDoubleValue() throws HostException {
105 return mValue.doubleValue();
106 }
107
108
109 public void setBigDecimalValue(final BigDecimal value) throws HostException {
110 mValue = value;
111 }
112
113
114 public BigDecimal getBigDecimalValue() throws HostException {
115 return mValue;
116 }
117
118
119 public void setBigIntegerValue(final BigInteger value) throws HostException {
120 mValue = new BigDecimal(value);
121 }
122
123
124 public BigInteger getBigIntegerValue() throws HostException {
125 return mValue.toBigInteger();
126 }
127
128
129 public void setBooleanValue(Boolean value) throws HostException {
130 mValue = new BigDecimal((value) ? "1" : "0");
131 }
132
133
134 public Boolean getBooleanValue() throws HostException {
135 if (mValue.intValue() == 0) {
136 return false;
137 }
138 return true;
139 }
140
141
142
143
144
145
146
147
148
149 public Object getEnumValue(final Class < ? > type) throws HostException {
150 try {
151 Method value = type.getMethod("value");
152 Method fromValue = type.getMethod("fromValue",
153 value.getReturnType());
154 return fromValue
155 .invoke(null, getObjectValue(value.getReturnType()));
156 } catch (SecurityException e) {
157 throw new HostException(e);
158 } catch (IllegalArgumentException e) {
159 throw new HostException(e);
160 } catch (IllegalAccessException e) {
161 throw new HostException(e);
162 } catch (NoSuchMethodException e) {
163 throw new HostException(e);
164 } catch (InvocationTargetException e) {
165 throw new HostException(e);
166 }
167 }
168
169
170
171
172
173
174
175
176
177 public void setEnumValue(Enum < ? > value) throws HostException {
178 try {
179 Method valueMethod = value.getClass().getMethod("value");
180 Object numValue = valueMethod.invoke(value);
181 setObjectValue(numValue);
182 } catch (SecurityException e) {
183 throw new HostException(e);
184 } catch (IllegalArgumentException e) {
185 throw new HostException(e);
186 } catch (IllegalAccessException e) {
187 throw new HostException(e);
188 } catch (NoSuchMethodException e) {
189 throw new HostException(e);
190 } catch (InvocationTargetException e) {
191 throw new HostException(e);
192 }
193 }
194
195
196 public Object getObjectValue(final Class < ? > type) throws HostException {
197 if (type.equals(BigDecimal.class)) {
198 return mValue;
199 } else if (type.equals(BigInteger.class)) {
200 return getBigIntegerValue();
201 } else if (type.equals(Long.class) || type.equals(long.class)) {
202 return getLongValue();
203 } else if (type.equals(Float.class) || type.equals(float.class)) {
204 return getFloatValue();
205 } else if (type.equals(Double.class) || type.equals(double.class)) {
206 return getDoubleValue();
207 } else if (type.equals(Integer.class) || type.equals(int.class)) {
208 return getIntegerValue();
209 } else if (type.equals(Short.class) || type.equals(short.class)) {
210 return getShortValue();
211 } else if (type.equals(Byte.class) || type.equals(byte.class)) {
212 return getByteValue();
213 } else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
214 return getBooleanValue();
215 } else if (type.isEnum()) {
216 return getEnumValue(type);
217 } else {
218 throw new HostException("Attempt to get binding "
219 + getBindingName() + " as an incompatible type " + type);
220 }
221 }
222
223
224 public void setObjectValue(final Object value) throws HostException {
225 if (value == null) {
226 mValue = null;
227 return;
228 }
229 if (value instanceof BigDecimal) {
230 mValue = (BigDecimal) value;
231 } else if (value instanceof BigInteger) {
232 setBigIntegerValue((BigInteger) value);
233 } else if (value instanceof Long) {
234 setLongValue((Long) value);
235 } else if (value instanceof Float) {
236 setFloatValue((Float) value);
237 } else if (value instanceof Double) {
238 setDoubleValue((Double) value);
239 } else if (value instanceof Integer) {
240 setIntegerValue((Integer) value);
241 } else if (value instanceof Short) {
242 setShortValue((Short) value);
243 } else if (value instanceof Byte) {
244 setByteValue((Byte) value);
245 } else if (value instanceof Boolean) {
246 setBooleanValue((Boolean) value);
247 } else if (value instanceof Enum) {
248 setEnumValue((Enum < ? >) value);
249 } else {
250 throw new HostException("Attempt to set binding "
251 + getBindingName() + " from an incompatible value " + value);
252 }
253 }
254
255
256 public boolean isSet() {
257 return (mValue != null);
258 }
259 }