--- a/jdk/src/share/classes/java/math/BigInteger.java Wed Sep 14 08:33:34 2011 -0700
+++ b/jdk/src/share/classes/java/math/BigInteger.java Wed Sep 14 11:32:11 2011 -0700
@@ -2919,6 +2919,7 @@
* result with the opposite sign.
*
* @return this BigInteger converted to an {@code int}.
+ * @see #intValueExact()
*/
public int intValue() {
int result = 0;
@@ -2939,6 +2940,7 @@
* result with the opposite sign.
*
* @return this BigInteger converted to a {@code long}.
+ * @see #longValueExact()
*/
public long longValue() {
long result = 0;
@@ -3382,4 +3384,84 @@
}
return result;
}
+
+ /**
+ * Converts this {@code BigInteger} to a {@code long}, checking
+ * for lost information. If the value of this {@code BigInteger}
+ * is out of the range of the {@code long} type, then an
+ * {@code ArithmeticException} is thrown.
+ *
+ * @return this {@code BigInteger} converted to a {@code long}.
+ * @throws ArithmeticException if the value of {@code this} will
+ * not exactly fit in a {@code long}.
+ * @see BigInteger#longValue
+ * @since 1.8
+ */
+ public long longValueExact() {
+ if (mag.length <= 2 && bitLength() <= 63)
+ return longValue();
+ else
+ throw new ArithmeticException("BigInteger out of long range");
+ }
+
+ /**
+ * Converts this {@code BigInteger} to an {@code int}, checking
+ * for lost information. If the value of this {@code BigInteger}
+ * is out of the range of the {@code int} type, then an
+ * {@code ArithmeticException} is thrown.
+ *
+ * @return this {@code BigInteger} converted to an {@code int}.
+ * @throws ArithmeticException if the value of {@code this} will
+ * not exactly fit in a {@code int}.
+ * @see BigInteger#intValue
+ * @since 1.8
+ */
+ public int intValueExact() {
+ if (mag.length <= 1 && bitLength() <= 31)
+ return intValue();
+ else
+ throw new ArithmeticException("BigInteger out of int range");
+ }
+
+ /**
+ * Converts this {@code BigInteger} to a {@code short}, checking
+ * for lost information. If the value of this {@code BigInteger}
+ * is out of the range of the {@code short} type, then an
+ * {@code ArithmeticException} is thrown.
+ *
+ * @return this {@code BigInteger} converted to a {@code short}.
+ * @throws ArithmeticException if the value of {@code this} will
+ * not exactly fit in a {@code short}.
+ * @see BigInteger#shortValue
+ * @since 1.8
+ */
+ public short shortValueExact() {
+ if (mag.length <= 1 && bitLength() <= 31) {
+ int value = intValue();
+ if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE)
+ return shortValue();
+ }
+ throw new ArithmeticException("BigInteger out of short range");
+ }
+
+ /**
+ * Converts this {@code BigInteger} to a {@code byte}, checking
+ * for lost information. If the value of this {@code BigInteger}
+ * is out of the range of the {@code byte} type, then an
+ * {@code ArithmeticException} is thrown.
+ *
+ * @return this {@code BigInteger} converted to a {@code byte}.
+ * @throws ArithmeticException if the value of {@code this} will
+ * not exactly fit in a {@code byte}.
+ * @see BigInteger#byteValue
+ * @since 1.8
+ */
+ public byte byteValueExact() {
+ if (mag.length <= 1 && bitLength() <= 31) {
+ int value = intValue();
+ if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE)
+ return byteValue();
+ }
+ throw new ArithmeticException("BigInteger out of byte range");
+ }
}