2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.CORBA
.DynAn
;
41 import org
.omg
.CORBA
.Any
;
42 import org
.omg
.CORBA
.BAD_OPERATION
;
43 import org
.omg
.CORBA
.BAD_PARAM
;
44 import org
.omg
.CORBA
.ORB
;
45 import org
.omg
.CORBA
.TypeCode
;
46 import org
.omg
.DynamicAny
.DynAny
;
47 import org
.omg
.DynamicAny
.DynAnyPackage
.InvalidValue
;
48 import org
.omg
.DynamicAny
.DynAnyPackage
.TypeMismatch
;
49 import org
.omg
.DynamicAny
.DynFixed
;
50 import org
.omg
.DynamicAny
.DynFixedOperations
;
52 import java
.math
.BigDecimal
;
55 * Implements DynAny, holding CORBA <code>fixed</code>. This class is derived
56 * from gnuDynEnm to avoid repetetive inclusion of unused DynAny methods.
58 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
60 public class gnuDynFixed
extends UndivideableAny
implements DynFixed
63 * Use serialVersionUID for interoperability.
65 private static final long serialVersionUID
= 1;
68 * The default value, assigned in the new instance.
70 static final BigDecimal ZERO
= new BigDecimal("0.0");
73 * The content of the dyn fixed, wrapped in this DynAny.
78 * The number of digits after the decimal point.
83 * The number of digits.
88 * Create a new instance of the dyn fixed.
90 public gnuDynFixed(TypeCode oType
, TypeCode aType
,
91 gnuDynAnyFactory aFactory
, ORB anOrb
94 super(oType
, aType
, aFactory
, anOrb
);
97 digits
= final_type
.fixed_digits();
98 scale
= final_type
.fixed_scale();
102 throw new BAD_PARAM("Not a fixed");
108 * Clone the current instance.
110 public gnuDynFixed(gnuDynFixed from
)
112 super(from
.official_type
, from
.final_type
, from
.factory
, from
.orb
);
113 digits
= from
.digits
;
119 * Get the value of the wrapped dyn fixed, as string.
121 public String
get_value()
123 return value
.toString();
129 public boolean set_value(String fixed_value
)
130 throws TypeMismatch
, InvalidValue
132 // Count the digits till decimal point.
135 boolean leading0
= true;
137 for (int i
= 0; i
< fixed_value
.length(); i
++)
139 c
= fixed_value
.charAt(i
);
140 if (Character
.isDigit(c
))
142 if (!(c
== '0' && leading0
))
150 if (digs
> (digits
- scale
))
151 throw new InvalidValue("Too many digits: " + digs
+ " for " + digits
+
157 value
= new BigDecimal(fixed_value
);
159 catch (NumberFormatException ex
)
161 if (fixed_value
.trim().length() == 0)
162 throw new InvalidValue("Empty string passed");
165 new TypeMismatch("Not a number: '" + fixed_value
+ "'");
171 return value
.scale() <= scale
;
175 * Assign the value from another BigDecimal.
177 public void assign(DynAny from
) throws TypeMismatch
179 checkType(official_type
, from
.type());
181 if (from
instanceof gnuDynFixed
)
183 gnuDynFixed other
= (gnuDynFixed
) from
;
186 else if (from
instanceof DynFixedOperations
)
188 value
= new BigDecimal(((DynFixedOperations
) from
).get_value());
191 throw new TypeMismatch("Not a DynFixed");
200 return new gnuDynFixed(this);
204 * Compare for equality.
206 public boolean equal(DynAny other
)
208 if (other
instanceof gnuDynFixed
)
210 // Normally, this code would be executed.
211 return value
.equals(((gnuDynFixed
) other
).value
);
213 if (other
instanceof DynFixedOperations
)
215 // This may be involved when mixing implementations.
216 return ((DynFixedOperations
) other
).get_value().equals(get_value());
223 * Set the value from Any (must hold <code>fixed</code> with the matching
226 public void from_any(Any an_any
) throws TypeMismatch
, InvalidValue
230 checkType(official_type
, an_any
.type());
232 value
= an_any
.extract_fixed();
235 catch (BAD_OPERATION e
)
237 InvalidValue t
= new InvalidValue();
244 * Create and return Any, holding this DynFixed value.
249 g
.insert_fixed(value
, official_type
);