1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: import ;
53:
54:
60: public class gnuDynFixed extends UndivideableAny implements DynFixed
61: {
62:
65: private static final long serialVersionUID = 1;
66:
67:
70: static final BigDecimal ZERO = new BigDecimal("0.0");
71:
72:
75: BigDecimal value;
76:
77:
80: final int scale;
81:
82:
85: final int digits;
86:
87:
90: public gnuDynFixed(TypeCode oType, TypeCode aType,
91: gnuDynAnyFactory aFactory, ORB anOrb
92: )
93: {
94: super(oType, aType, aFactory, anOrb);
95: try
96: {
97: digits = final_type.fixed_digits();
98: scale = final_type.fixed_scale();
99: }
100: catch (Exception e)
101: {
102: throw new BAD_PARAM("Not a fixed");
103: }
104: value = ZERO;
105: }
106:
107:
110: public gnuDynFixed(gnuDynFixed from)
111: {
112: super(from.official_type, from.final_type, from.factory, from.orb);
113: digits = from.digits;
114: scale = from.scale;
115: value = from.value;
116: }
117:
118:
121: public String get_value()
122: {
123: return value.toString();
124: }
125:
126:
129: public boolean set_value(String fixed_value)
130: throws TypeMismatch, InvalidValue
131: {
132:
133: int digs = 0;
134: char c;
135: boolean leading0 = true;
136: Digs:
137: for (int i = 0; i < fixed_value.length(); i++)
138: {
139: c = fixed_value.charAt(i);
140: if (Character.isDigit(c))
141: {
142: if (!(c == '0' && leading0))
143: digs++;
144: if (c != '0')
145: leading0 = false;
146: }
147: else if (c == '.')
148: break Digs;
149: }
150: if (digs > (digits - scale))
151: throw new InvalidValue("Too many digits: " + digs + " for " + digits +
152: "." + scale
153: );
154:
155: try
156: {
157: value = new BigDecimal(fixed_value);
158: }
159: catch (NumberFormatException ex)
160: {
161: if (fixed_value.trim().length() == 0)
162: throw new InvalidValue("Empty string passed");
163:
164: TypeMismatch inva =
165: new TypeMismatch("Not a number: '" + fixed_value + "'");
166: inva.initCause(ex);
167: throw inva;
168: }
169:
170: valueChanged();
171: return value.scale() <= scale;
172: }
173:
174:
177: public void assign(DynAny from) throws TypeMismatch
178: {
179: checkType(official_type, from.type());
180:
181: if (from instanceof gnuDynFixed)
182: {
183: gnuDynFixed other = (gnuDynFixed) from;
184: value = other.value;
185: }
186: else if (from instanceof DynFixedOperations)
187: {
188: value = new BigDecimal(((DynFixedOperations) from).get_value());
189: }
190: else
191: throw new TypeMismatch("Not a DynFixed");
192: valueChanged();
193: }
194:
195:
198: public DynAny copy()
199: {
200: return new gnuDynFixed(this);
201: }
202:
203:
206: public boolean equal(DynAny other)
207: {
208: if (other instanceof gnuDynFixed)
209: {
210:
211: return value.equals(((gnuDynFixed) other).value);
212: }
213: if (other instanceof DynFixedOperations)
214: {
215:
216: return ((DynFixedOperations) other).get_value().equals(get_value());
217: }
218: else
219: return false;
220: }
221:
222:
226: public void from_any(Any an_any) throws TypeMismatch, InvalidValue
227: {
228: try
229: {
230: checkType(official_type, an_any.type());
231:
232: value = an_any.extract_fixed();
233: valueChanged();
234: }
235: catch (BAD_OPERATION e)
236: {
237: InvalidValue t = new InvalidValue();
238: t.initCause(e);
239: throw t;
240: }
241: }
242:
243:
246: public Any to_any()
247: {
248: Any g = createAny();
249: g.insert_fixed(value, official_type);
250: return g;
251: }
252: }