KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > dynamicany > DynFixedImpl


1 /*
2  * @(#)DynFixedImpl.java 1.8 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.dynamicany;
9
10 import org.omg.CORBA.TypeCode JavaDoc;
11 import org.omg.CORBA.Any JavaDoc;
12 import org.omg.CORBA.NO_IMPLEMENT JavaDoc;
13 import org.omg.DynamicAny.*;
14 import org.omg.DynamicAny.DynAnyPackage.*;
15 import java.math.BigDecimal JavaDoc;
16 import java.math.BigInteger JavaDoc;
17 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
18
19 import com.sun.corba.se.spi.orb.ORB ;
20 import com.sun.corba.se.spi.logging.CORBALogDomains ;
21 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
22
23 public class DynFixedImpl extends DynAnyBasicImpl implements DynFixed
24 {
25     //
26
// Constructors
27
//
28

29     private DynFixedImpl() {
30         this(null, (Any JavaDoc)null, false);
31     }
32
33     protected DynFixedImpl(ORB orb, Any JavaDoc any, boolean copyValue) {
34         super(orb, any, copyValue);
35     }
36
37     // Sets the current position to -1 and the value to zero.
38
protected DynFixedImpl(ORB orb, TypeCode JavaDoc typeCode) {
39         super(orb, typeCode);
40         index = NO_INDEX;
41     }
42
43     //
44
// DynAny interface methods
45
//
46
/*
47     public int component_count() {
48     return 0;
49     }
50 */

51     //
52
// DynFixed interface methods
53
//
54

55     public String JavaDoc get_value () {
56         if (status == STATUS_DESTROYED) {
57         throw wrapper.dynAnyDestroyed() ;
58         }
59     return any.extract_fixed().toString();
60     }
61
62     // Initializes the value of the DynFixed.
63
// The val string must contain a fixed string constant in the same format
64
// as used for IDL fixed-point literals.
65
//
66
// It may consist of an integer part, an optional decimal point,
67
// a fraction part and an optional letter d or D.
68
// The integer and fraction parts both must be sequences of decimal (base 10) digits.
69
// Either the integer part or the fraction part, but not both, may be missing.
70
//
71
// If val contains a value whose scale exceeds that of the DynFixed or is not initialized,
72
// the operation raises InvalidValue.
73
// The return value is true if val can be represented as the DynFixed without loss of precision.
74
// If val has more fractional digits than can be represented in the DynFixed,
75
// fractional digits are truncated and the return value is false.
76
// If val does not contain a valid fixed-point literal or contains extraneous characters
77
// other than leading or trailing white space, the operation raises TypeMismatch.
78
//
79
public boolean set_value (String JavaDoc val)
80         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc,
81                org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
82     {
83         if (status == STATUS_DESTROYED) {
84         throw wrapper.dynAnyDestroyed() ;
85         }
86         int digits = 0;
87         int scale = 0;
88         boolean preservedPrecision = true;
89         try {
90             digits = any.type().fixed_digits();
91             scale = any.type().fixed_scale();
92         } catch (BadKind JavaDoc ex) { // impossible
93
}
94         // First get rid of leading or trailing whitespace which is allowed
95
String JavaDoc string = val.trim();
96         if (string.length() == 0)
97             throw new TypeMismatch();
98         // Now scan for the sign
99
String JavaDoc sign = "";
100         if (string.charAt(0) == '-') {
101             sign = "-";
102             string = string.substring(1);
103         } else if (string.charAt(0) == '+') {
104             sign = "+";
105             string = string.substring(1);
106         }
107         // Now get rid of the letter d or D.
108
int dIndex = string.indexOf('d');
109         if (dIndex == -1) {
110             dIndex = string.indexOf('D');
111         }
112         if (dIndex != -1) {
113             string = string.substring(0, dIndex);
114         }
115         // Just to be sure
116
if (string.length() == 0)
117             throw new TypeMismatch();
118         // Now look for the dot to determine the integer part
119
String JavaDoc integerPart;
120         String JavaDoc fractionPart;
121         int currentScale;
122         int currentDigits;
123         int dotIndex = string.indexOf('.');
124         if (dotIndex == -1) {
125             integerPart = string;
126             fractionPart = null;
127             currentScale = 0;
128             currentDigits = integerPart.length();
129         } else if (dotIndex == 0 ) {
130             integerPart = null;
131             fractionPart = string;
132             currentScale = fractionPart.length();
133             currentDigits = currentScale;
134         } else {
135             integerPart = string.substring(0, dotIndex);
136             fractionPart = string.substring(dotIndex + 1);
137             currentScale = fractionPart.length();
138             currentDigits = integerPart.length() + currentScale;
139         }
140         // Let's see if we have to drop some precision
141
if (currentDigits > digits) {
142             preservedPrecision = false;
143             // truncate the fraction part
144
if (integerPart.length() < digits) {
145                 fractionPart = fractionPart.substring(0, digits - integerPart.length());
146             } else if (integerPart.length() == digits) {
147                 // currentScale > 0
148
// drop the fraction completely
149
fractionPart = null;
150             } else {
151                 // integerPart.length() > digits
152
// unable to truncate fraction part
153
throw new InvalidValue();
154             }
155         }
156         // If val contains a value whose scale exceeds that of the DynFixed or is not initialized,
157
// the operation raises InvalidValue.
158
// Reinterpreted to mean raise InvalidValue only if the integer part exceeds precision,
159
// which is handled above (integerPart.length() > digits)
160
/*
161         if (currentScale > scale) {
162             throw new InvalidValue("Scale exceeds " + scale);
163         }
164 */

165         // Now check whether both parts are valid numbers
166
BigDecimal JavaDoc result;
167         try {
168             new BigInteger JavaDoc(integerPart);
169             if (fractionPart == null) {
170                 result = new BigDecimal JavaDoc(sign + integerPart);
171             } else {
172                 new BigInteger JavaDoc(fractionPart);
173                 result = new BigDecimal JavaDoc(sign + integerPart + "." + fractionPart);
174             }
175         } catch (NumberFormatException JavaDoc nfe) {
176             throw new TypeMismatch();
177         }
178         any.insert_fixed(result, any.type());
179         return preservedPrecision;
180     }
181
182     public String JavaDoc toString() {
183         int digits = 0;
184         int scale = 0;
185         try {
186             digits = any.type().fixed_digits();
187             scale = any.type().fixed_scale();
188         } catch (BadKind JavaDoc ex) { // impossible
189
}
190         return "DynFixed with value=" + this.get_value() + ", digits=" + digits + ", scale=" + scale;
191     }
192 }
193
Popular Tags