Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / gnu / CORBA / DynAn / gnuDynFixed.java
blob9655f0387da9a7c488bf1c909a7d02bf869e9318
1 /* gnuDynFixed.java --
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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;
54 /**
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
62 /**
63 * Use serialVersionUID for interoperability.
65 private static final long serialVersionUID = 1;
67 /**
68 * The default value, assigned in the new instance.
70 static final BigDecimal ZERO = new BigDecimal("0.0");
72 /**
73 * The content of the dyn fixed, wrapped in this DynAny.
75 BigDecimal value;
77 /**
78 * The number of digits after the decimal point.
80 final int scale;
82 /**
83 * The number of digits.
85 final int digits;
87 /**
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);
95 try
97 digits = final_type.fixed_digits();
98 scale = final_type.fixed_scale();
100 catch (Exception e)
102 throw new BAD_PARAM("Not a fixed");
104 value = ZERO;
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;
114 scale = from.scale;
115 value = from.value;
119 * Get the value of the wrapped dyn fixed, as string.
121 public String get_value()
123 return value.toString();
127 * Set the value.
129 public boolean set_value(String fixed_value)
130 throws TypeMismatch, InvalidValue
132 // Count the digits till decimal point.
133 int digs = 0;
134 char c;
135 boolean leading0 = true;
136 Digs:
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))
143 digs++;
144 if (c != '0')
145 leading0 = false;
147 else if (c == '.')
148 break Digs;
150 if (digs > (digits - scale))
151 throw new InvalidValue("Too many digits: " + digs + " for " + digits +
152 "." + scale
157 value = new BigDecimal(fixed_value);
159 catch (NumberFormatException ex)
161 if (fixed_value.trim().length() == 0)
162 throw new InvalidValue("Empty string passed");
164 TypeMismatch inva =
165 new TypeMismatch("Not a number: '" + fixed_value + "'");
166 inva.initCause(ex);
167 throw inva;
170 valueChanged();
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;
184 value = other.value;
186 else if (from instanceof DynFixedOperations)
188 value = new BigDecimal(((DynFixedOperations) from).get_value());
190 else
191 throw new TypeMismatch("Not a DynFixed");
192 valueChanged();
196 * Create a copy.
198 public DynAny copy()
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());
218 else
219 return false;
223 * Set the value from Any (must hold <code>fixed</code> with the matching
224 * typecode.).
226 public void from_any(Any an_any) throws TypeMismatch, InvalidValue
230 checkType(official_type, an_any.type());
232 value = an_any.extract_fixed();
233 valueChanged();
235 catch (BAD_OPERATION e)
237 InvalidValue t = new InvalidValue();
238 t.initCause(e);
239 throw t;
244 * Create and return Any, holding this DynFixed value.
246 public Any to_any()
248 Any g = createAny();
249 g.insert_fixed(value, official_type);
250 return g;