1 7 8 package com.sun.corba.se.impl.dynamicany; 9 10 import org.omg.CORBA.TypeCode ; 11 import org.omg.CORBA.Any ; 12 import org.omg.CORBA.NO_IMPLEMENT ; 13 import org.omg.DynamicAny.*; 14 import org.omg.DynamicAny.DynAnyPackage.*; 15 import java.math.BigDecimal ; 16 import java.math.BigInteger ; 17 import org.omg.CORBA.TypeCodePackage.BadKind ; 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 29 private DynFixedImpl() { 30 this(null, (Any )null, false); 31 } 32 33 protected DynFixedImpl(ORB orb, Any any, boolean copyValue) { 34 super(orb, any, copyValue); 35 } 36 37 protected DynFixedImpl(ORB orb, TypeCode typeCode) { 39 super(orb, typeCode); 40 index = NO_INDEX; 41 } 42 43 51 55 public String get_value () { 56 if (status == STATUS_DESTROYED) { 57 throw wrapper.dynAnyDestroyed() ; 58 } 59 return any.extract_fixed().toString(); 60 } 61 62 public boolean set_value (String val) 80 throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch , 81 org.omg.DynamicAny.DynAnyPackage.InvalidValue 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 ex) { } 94 String string = val.trim(); 96 if (string.length() == 0) 97 throw new TypeMismatch(); 98 String 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 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 if (string.length() == 0) 117 throw new TypeMismatch(); 118 String integerPart; 120 String 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 if (currentDigits > digits) { 142 preservedPrecision = false; 143 if (integerPart.length() < digits) { 145 fractionPart = fractionPart.substring(0, digits - integerPart.length()); 146 } else if (integerPart.length() == digits) { 147 fractionPart = null; 150 } else { 151 throw new InvalidValue(); 154 } 155 } 156 165 BigDecimal result; 167 try { 168 new BigInteger (integerPart); 169 if (fractionPart == null) { 170 result = new BigDecimal (sign + integerPart); 171 } else { 172 new BigInteger (fractionPart); 173 result = new BigDecimal (sign + integerPart + "." + fractionPart); 174 } 175 } catch (NumberFormatException nfe) { 176 throw new TypeMismatch(); 177 } 178 any.insert_fixed(result, any.type()); 179 return preservedPrecision; 180 } 181 182 public String 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 ex) { } 190 return "DynFixed with value=" + this.get_value() + ", digits=" + digits + ", scale=" + scale; 191 } 192 } 193 | Popular Tags |