001: /*
002: * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.corba.se.impl.dynamicany;
027:
028: import org.omg.CORBA.TypeCode;
029: import org.omg.CORBA.Any;
030: import org.omg.CORBA.NO_IMPLEMENT;
031: import org.omg.DynamicAny.*;
032: import org.omg.DynamicAny.DynAnyPackage.*;
033: import java.math.BigDecimal;
034: import java.math.BigInteger;
035: import org.omg.CORBA.TypeCodePackage.BadKind;
036:
037: import com.sun.corba.se.spi.orb.ORB;
038: import com.sun.corba.se.spi.logging.CORBALogDomains;
039: import com.sun.corba.se.impl.logging.ORBUtilSystemException;
040:
041: public class DynFixedImpl extends DynAnyBasicImpl implements DynFixed {
042: //
043: // Constructors
044: //
045:
046: private DynFixedImpl() {
047: this (null, (Any) null, false);
048: }
049:
050: protected DynFixedImpl(ORB orb, Any any, boolean copyValue) {
051: super (orb, any, copyValue);
052: }
053:
054: // Sets the current position to -1 and the value to zero.
055: protected DynFixedImpl(ORB orb, TypeCode typeCode) {
056: super (orb, typeCode);
057: index = NO_INDEX;
058: }
059:
060: //
061: // DynAny interface methods
062: //
063: /*
064: public int component_count() {
065: return 0;
066: }
067: */
068: //
069: // DynFixed interface methods
070: //
071: public String get_value() {
072: if (status == STATUS_DESTROYED) {
073: throw wrapper.dynAnyDestroyed();
074: }
075: return any.extract_fixed().toString();
076: }
077:
078: // Initializes the value of the DynFixed.
079: // The val string must contain a fixed string constant in the same format
080: // as used for IDL fixed-point literals.
081: //
082: // It may consist of an integer part, an optional decimal point,
083: // a fraction part and an optional letter d or D.
084: // The integer and fraction parts both must be sequences of decimal (base 10) digits.
085: // Either the integer part or the fraction part, but not both, may be missing.
086: //
087: // If val contains a value whose scale exceeds that of the DynFixed or is not initialized,
088: // the operation raises InvalidValue.
089: // The return value is true if val can be represented as the DynFixed without loss of precision.
090: // If val has more fractional digits than can be represented in the DynFixed,
091: // fractional digits are truncated and the return value is false.
092: // If val does not contain a valid fixed-point literal or contains extraneous characters
093: // other than leading or trailing white space, the operation raises TypeMismatch.
094: //
095: public boolean set_value(String val)
096: throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
097: org.omg.DynamicAny.DynAnyPackage.InvalidValue {
098: if (status == STATUS_DESTROYED) {
099: throw wrapper.dynAnyDestroyed();
100: }
101: int digits = 0;
102: int scale = 0;
103: boolean preservedPrecision = true;
104: try {
105: digits = any.type().fixed_digits();
106: scale = any.type().fixed_scale();
107: } catch (BadKind ex) { // impossible
108: }
109: // First get rid of leading or trailing whitespace which is allowed
110: String string = val.trim();
111: if (string.length() == 0)
112: throw new TypeMismatch();
113: // Now scan for the sign
114: String sign = "";
115: if (string.charAt(0) == '-') {
116: sign = "-";
117: string = string.substring(1);
118: } else if (string.charAt(0) == '+') {
119: sign = "+";
120: string = string.substring(1);
121: }
122: // Now get rid of the letter d or D.
123: int dIndex = string.indexOf('d');
124: if (dIndex == -1) {
125: dIndex = string.indexOf('D');
126: }
127: if (dIndex != -1) {
128: string = string.substring(0, dIndex);
129: }
130: // Just to be sure
131: if (string.length() == 0)
132: throw new TypeMismatch();
133: // Now look for the dot to determine the integer part
134: String integerPart;
135: String fractionPart;
136: int currentScale;
137: int currentDigits;
138: int dotIndex = string.indexOf('.');
139: if (dotIndex == -1) {
140: integerPart = string;
141: fractionPart = null;
142: currentScale = 0;
143: currentDigits = integerPart.length();
144: } else if (dotIndex == 0) {
145: integerPart = null;
146: fractionPart = string;
147: currentScale = fractionPart.length();
148: currentDigits = currentScale;
149: } else {
150: integerPart = string.substring(0, dotIndex);
151: fractionPart = string.substring(dotIndex + 1);
152: currentScale = fractionPart.length();
153: currentDigits = integerPart.length() + currentScale;
154: }
155: // Let's see if we have to drop some precision
156: if (currentDigits > digits) {
157: preservedPrecision = false;
158: // truncate the fraction part
159: if (integerPart.length() < digits) {
160: fractionPart = fractionPart.substring(0, digits
161: - integerPart.length());
162: } else if (integerPart.length() == digits) {
163: // currentScale > 0
164: // drop the fraction completely
165: fractionPart = null;
166: } else {
167: // integerPart.length() > digits
168: // unable to truncate fraction part
169: throw new InvalidValue();
170: }
171: }
172: // If val contains a value whose scale exceeds that of the DynFixed or is not initialized,
173: // the operation raises InvalidValue.
174: // Reinterpreted to mean raise InvalidValue only if the integer part exceeds precision,
175: // which is handled above (integerPart.length() > digits)
176: /*
177: if (currentScale > scale) {
178: throw new InvalidValue("Scale exceeds " + scale);
179: }
180: */
181: // Now check whether both parts are valid numbers
182: BigDecimal result;
183: try {
184: new BigInteger(integerPart);
185: if (fractionPart == null) {
186: result = new BigDecimal(sign + integerPart);
187: } else {
188: new BigInteger(fractionPart);
189: result = new BigDecimal(sign + integerPart + "."
190: + fractionPart);
191: }
192: } catch (NumberFormatException nfe) {
193: throw new TypeMismatch();
194: }
195: any.insert_fixed(result, any.type());
196: return preservedPrecision;
197: }
198:
199: public String toString() {
200: int digits = 0;
201: int scale = 0;
202: try {
203: digits = any.type().fixed_digits();
204: scale = any.type().fixed_scale();
205: } catch (BadKind ex) { // impossible
206: }
207: return "DynFixed with value=" + this .get_value() + ", digits="
208: + digits + ", scale=" + scale;
209: }
210: }
|