MathContext.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* MathContext.java --
  2. Copyright (C) 1999, 2000, 2002, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.math;
  32. import java.io.Serializable;
  33. /**
  34. * Immutable objects describing settings such as rounding mode and digit
  35. * precision for numerical operations such as those in the BigDecimal class.
  36. * @author Anthony Balkissoon abalkiss at redhat dot com
  37. *
  38. */
  39. public final class MathContext implements Serializable
  40. {
  41. /** A MathContext for unlimited precision arithmetic * */
  42. public static final MathContext UNLIMITED =
  43. new MathContext(0, RoundingMode.HALF_UP);
  44. /**
  45. * A MathContext for the IEEE 754R Decimal32 format - 7 digit preicision and
  46. * HALF_EVEN rounding.
  47. */
  48. public static final MathContext DECIMAL32 =
  49. new MathContext(7, RoundingMode.HALF_EVEN);
  50. /**
  51. * A MathContext for the IEEE 754R Decimal64 format - 16 digit preicision and
  52. * HALF_EVEN rounding.
  53. */
  54. public static final MathContext DECIMAL64 =
  55. new MathContext(16, RoundingMode.HALF_EVEN);
  56. /**
  57. * A MathContext for the IEEE 754R Decimal128 format - 34 digit preicision and
  58. * HALF_EVEN rounding.
  59. */
  60. public static final MathContext DECIMAL128 =
  61. new MathContext(34, RoundingMode.HALF_EVEN);
  62. /**
  63. * This is the serialVersionUID reported here:
  64. * java.sun.com/j2se/1.5.0/docs/api/serialized-form.html#java.math.MathContext
  65. */
  66. private static final long serialVersionUID = 5579720004786848255L;
  67. private int precision;
  68. private RoundingMode roundMode;
  69. /**
  70. * Constructs a new MathContext with the specified precision and with HALF_UP
  71. * rounding.
  72. * @param setPrecision the precision for the new MathContext
  73. *
  74. * @throws IllegalArgumentException if precision is < 0.
  75. */
  76. public MathContext(int setPrecision)
  77. {
  78. this(setPrecision, RoundingMode.HALF_UP);
  79. }
  80. /**
  81. * Constructs a new MathContext with the specified precision and rounding
  82. * mode.
  83. * @param setPrecision the precision
  84. * @param setRoundingMode the rounding mode
  85. *
  86. * @throws IllegalArgumentException if precision is < 0.
  87. */
  88. public MathContext(int setPrecision, RoundingMode setRoundingMode)
  89. {
  90. if (setPrecision < 0)
  91. throw new IllegalArgumentException("Precision cannot be less than zero.");
  92. precision = setPrecision;
  93. roundMode = setRoundingMode;
  94. }
  95. /**
  96. * Constructs a MathContext from a String that has the same form as one
  97. * produced by the toString() method.
  98. * @param val
  99. *
  100. * @throws IllegalArgumentException if the String is not in the correct
  101. * format or if the precision specified is < 0.
  102. */
  103. public MathContext(String val)
  104. {
  105. try
  106. {
  107. int roundingModeIndex = val.indexOf("roundingMode", 10);
  108. precision = Integer.parseInt(val.substring(10, roundingModeIndex - 1));
  109. roundMode = RoundingMode.valueOf(val.substring(roundingModeIndex + 13));
  110. }
  111. catch (NumberFormatException nfe)
  112. {
  113. throw new IllegalArgumentException("String not in correct format");
  114. }
  115. catch (IllegalArgumentException iae)
  116. {
  117. throw new IllegalArgumentException("String not in correct format");
  118. }
  119. if (precision < 0)
  120. throw new IllegalArgumentException("Precision cannot be less than 0.");
  121. }
  122. /**
  123. * Returns true if x is a MathContext and has the same precision setting
  124. * and rounding mode as this MathContext.
  125. *
  126. * @return true if the above conditions hold
  127. */
  128. public boolean equals(Object x)
  129. {
  130. if (!(x instanceof MathContext))
  131. return false;
  132. MathContext mc = (MathContext)x;
  133. return mc.precision == this.precision
  134. && mc.roundMode.equals(this.roundMode);
  135. }
  136. /**
  137. * Returns the precision setting.
  138. * @return the precision setting.
  139. */
  140. public int getPrecision()
  141. {
  142. return precision;
  143. }
  144. /**
  145. * Returns the rounding mode setting. This will be one of
  146. * RoundingMode.CEILING, RoundingMode.DOWN, RoundingMode.FLOOR,
  147. * RoundingMode.HALF_DOWN, RoundingMode.HALF_EVEN, RoundingMode.HALF_UP,
  148. * RoundingMode.UNNECESSARY, or RoundingMode.UP.
  149. * @return the rounding mode setting.
  150. */
  151. public RoundingMode getRoundingMode()
  152. {
  153. return roundMode;
  154. }
  155. /**
  156. * Returns "precision=p roundingMode=MODE" where p is an int giving the
  157. * precision and MODE is UP, DOWN, HALF_UP, HALF_DOWN, HALF_EVEN, CEILING,
  158. * FLOOR, or UNNECESSARY corresponding to rounding modes.
  159. *
  160. * @return a String describing this MathContext
  161. */
  162. public String toString()
  163. {
  164. return "precision="+precision+" roundingMode="+roundMode;
  165. }
  166. /**
  167. * Returns the hashcode for this MathContext.
  168. * @return the hashcode for this MathContext.
  169. */
  170. public int hashCode()
  171. {
  172. return precision ^ roundMode.hashCode();
  173. }
  174. }