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. package com.sun.corba.se.impl.dynamicany;
  8. import org.omg.CORBA.TypeCode;
  9. import org.omg.CORBA.Any;
  10. import org.omg.CORBA.NO_IMPLEMENT;
  11. import org.omg.DynamicAny.*;
  12. import org.omg.DynamicAny.DynAnyPackage.*;
  13. import java.math.BigDecimal;
  14. import java.math.BigInteger;
  15. import org.omg.CORBA.TypeCodePackage.BadKind;
  16. import com.sun.corba.se.spi.orb.ORB ;
  17. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  18. import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  19. public class DynFixedImpl extends DynAnyBasicImpl implements DynFixed
  20. {
  21. //
  22. // Constructors
  23. //
  24. private DynFixedImpl() {
  25. this(null, (Any)null, false);
  26. }
  27. protected DynFixedImpl(ORB orb, Any any, boolean copyValue) {
  28. super(orb, any, copyValue);
  29. }
  30. // Sets the current position to -1 and the value to zero.
  31. protected DynFixedImpl(ORB orb, TypeCode typeCode) {
  32. super(orb, typeCode);
  33. index = NO_INDEX;
  34. }
  35. //
  36. // DynAny interface methods
  37. //
  38. /*
  39. public int component_count() {
  40. return 0;
  41. }
  42. */
  43. //
  44. // DynFixed interface methods
  45. //
  46. public String get_value () {
  47. if (status == STATUS_DESTROYED) {
  48. throw wrapper.dynAnyDestroyed() ;
  49. }
  50. return any.extract_fixed().toString();
  51. }
  52. // Initializes the value of the DynFixed.
  53. // The val string must contain a fixed string constant in the same format
  54. // as used for IDL fixed-point literals.
  55. //
  56. // It may consist of an integer part, an optional decimal point,
  57. // a fraction part and an optional letter d or D.
  58. // The integer and fraction parts both must be sequences of decimal (base 10) digits.
  59. // Either the integer part or the fraction part, but not both, may be missing.
  60. //
  61. // If val contains a value whose scale exceeds that of the DynFixed or is not initialized,
  62. // the operation raises InvalidValue.
  63. // The return value is true if val can be represented as the DynFixed without loss of precision.
  64. // If val has more fractional digits than can be represented in the DynFixed,
  65. // fractional digits are truncated and the return value is false.
  66. // If val does not contain a valid fixed-point literal or contains extraneous characters
  67. // other than leading or trailing white space, the operation raises TypeMismatch.
  68. //
  69. public boolean set_value (String val)
  70. throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
  71. org.omg.DynamicAny.DynAnyPackage.InvalidValue
  72. {
  73. if (status == STATUS_DESTROYED) {
  74. throw wrapper.dynAnyDestroyed() ;
  75. }
  76. int digits = 0;
  77. int scale = 0;
  78. boolean preservedPrecision = true;
  79. try {
  80. digits = any.type().fixed_digits();
  81. scale = any.type().fixed_scale();
  82. } catch (BadKind ex) { // impossible
  83. }
  84. // First get rid of leading or trailing whitespace which is allowed
  85. String string = val.trim();
  86. if (string.length() == 0)
  87. throw new TypeMismatch();
  88. // Now scan for the sign
  89. String sign = "";
  90. if (string.charAt(0) == '-') {
  91. sign = "-";
  92. string = string.substring(1);
  93. } else if (string.charAt(0) == '+') {
  94. sign = "+";
  95. string = string.substring(1);
  96. }
  97. // Now get rid of the letter d or D.
  98. int dIndex = string.indexOf('d');
  99. if (dIndex == -1) {
  100. dIndex = string.indexOf('D');
  101. }
  102. if (dIndex != -1) {
  103. string = string.substring(0, dIndex);
  104. }
  105. // Just to be sure
  106. if (string.length() == 0)
  107. throw new TypeMismatch();
  108. // Now look for the dot to determine the integer part
  109. String integerPart;
  110. String fractionPart;
  111. int currentScale;
  112. int currentDigits;
  113. int dotIndex = string.indexOf('.');
  114. if (dotIndex == -1) {
  115. integerPart = string;
  116. fractionPart = null;
  117. currentScale = 0;
  118. currentDigits = integerPart.length();
  119. } else if (dotIndex == 0 ) {
  120. integerPart = null;
  121. fractionPart = string;
  122. currentScale = fractionPart.length();
  123. currentDigits = currentScale;
  124. } else {
  125. integerPart = string.substring(0, dotIndex);
  126. fractionPart = string.substring(dotIndex + 1);
  127. currentScale = fractionPart.length();
  128. currentDigits = integerPart.length() + currentScale;
  129. }
  130. // Let's see if we have to drop some precision
  131. if (currentDigits > digits) {
  132. preservedPrecision = false;
  133. // truncate the fraction part
  134. if (integerPart.length() < digits) {
  135. fractionPart = fractionPart.substring(0, digits - integerPart.length());
  136. } else if (integerPart.length() == digits) {
  137. // currentScale > 0
  138. // drop the fraction completely
  139. fractionPart = null;
  140. } else {
  141. // integerPart.length() > digits
  142. // unable to truncate fraction part
  143. throw new InvalidValue();
  144. }
  145. }
  146. // If val contains a value whose scale exceeds that of the DynFixed or is not initialized,
  147. // the operation raises InvalidValue.
  148. // Reinterpreted to mean raise InvalidValue only if the integer part exceeds precision,
  149. // which is handled above (integerPart.length() > digits)
  150. /*
  151. if (currentScale > scale) {
  152. throw new InvalidValue("Scale exceeds " + scale);
  153. }
  154. */
  155. // Now check whether both parts are valid numbers
  156. BigDecimal result;
  157. try {
  158. new BigInteger(integerPart);
  159. if (fractionPart == null) {
  160. result = new BigDecimal(sign + integerPart);
  161. } else {
  162. new BigInteger(fractionPart);
  163. result = new BigDecimal(sign + integerPart + "." + fractionPart);
  164. }
  165. } catch (NumberFormatException nfe) {
  166. throw new TypeMismatch();
  167. }
  168. any.insert_fixed(result, any.type());
  169. return preservedPrecision;
  170. }
  171. public String toString() {
  172. int digits = 0;
  173. int scale = 0;
  174. try {
  175. digits = any.type().fixed_digits();
  176. scale = any.type().fixed_scale();
  177. } catch (BadKind ex) { // impossible
  178. }
  179. return "DynFixed with value=" + this.get_value() + ", digits=" + digits + ", scale=" + scale;
  180. }
  181. }