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