diff options
Diffstat (limited to 'guava/src/com/google/common/primitives/UnsignedLong.java')
-rw-r--r-- | guava/src/com/google/common/primitives/UnsignedLong.java | 153 |
1 files changed, 23 insertions, 130 deletions
diff --git a/guava/src/com/google/common/primitives/UnsignedLong.java b/guava/src/com/google/common/primitives/UnsignedLong.java index a48de6b..62f7f81 100644 --- a/guava/src/com/google/common/primitives/UnsignedLong.java +++ b/guava/src/com/google/common/primitives/UnsignedLong.java @@ -17,31 +17,30 @@ package com.google.common.primitives; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.annotations.Beta; -import com.google.common.annotations.GwtCompatible; - import java.io.Serializable; import java.math.BigInteger; -import javax.annotation.CheckReturnValue; import javax.annotation.Nullable; +import com.google.common.annotations.Beta; +import com.google.common.annotations.GwtCompatible; + /** * A wrapper class for unsigned {@code long} values, supporting arithmetic operations. * * <p>In some cases, when speed is more important than code readability, it may be faster simply to * treat primitive {@code long} values as unsigned, using the methods from {@link UnsignedLongs}. * - * <p>See the Guava User Guide article on <a href= - * "http://code.google.com/p/guava-libraries/wiki/PrimitivesExplained#Unsigned_support"> - * unsigned primitive utilities</a>. + * <p><b>Please do not extend this class; it will be made final in the near future.</b> * * @author Louis Wasserman * @author Colin Evans * @since 11.0 */ +@Beta @GwtCompatible(serializable = true) -public final class UnsignedLong extends Number implements Comparable<UnsignedLong>, Serializable { +public class UnsignedLong extends Number implements Comparable<UnsignedLong>, Serializable { + // TODO(user): make final as soon as util.UnsignedLong is migrated over private static final long UNSIGNED_MASK = 0x7fffffffffffffffL; @@ -51,7 +50,7 @@ public final class UnsignedLong extends Number implements Comparable<UnsignedLon private final long value; - private UnsignedLong(long value) { + protected UnsignedLong(long value) { this.value = value; } @@ -61,49 +60,14 @@ public final class UnsignedLong extends Number implements Comparable<UnsignedLon * * <p>Put another way, if {@code value} is negative, the returned result will be equal to * {@code 2^64 + value}; otherwise, the returned result will be equal to {@code value}. - * - * @deprecated Use {@link #fromLongBits(long)}. This method is scheduled to be removed in Guava - * release 15.0. */ - @Deprecated - @Beta public static UnsignedLong asUnsigned(long value) { - return fromLongBits(value); - } - - /** - * Returns an {@code UnsignedLong} corresponding to a given bit representation. - * The argument is interpreted as an unsigned 64-bit value. Specifically, the sign bit - * of {@code bits} is interpreted as a normal bit, and all other bits are treated as usual. - * - * <p>If the argument is nonnegative, the returned result will be equal to {@code bits}, - * otherwise, the result will be equal to {@code 2^64 + bits}. - * - * <p>To represent decimal constants less than {@code 2^63}, consider {@link #valueOf(long)} - * instead. - * - * @since 14.0 - */ - public static UnsignedLong fromLongBits(long bits) { - // TODO(user): consider caching small values, like Long.valueOf - return new UnsignedLong(bits); + return new UnsignedLong(value); } /** - * Returns an {@code UnsignedLong} representing the same value as the specified {@code long}. - * - * @throws IllegalArgumentException if {@code value} is negative - * @since 14.0 - */ - public static UnsignedLong valueOf(long value) { - checkArgument(value >= 0, - "value (%s) is outside the range for an unsigned long value", value); - return fromLongBits(value); - } - - /** - * Returns a {@code UnsignedLong} representing the same value as the specified - * {@code BigInteger}. This is the inverse operation of {@link #bigIntegerValue()}. + * Returns a {@code UnsignedLong} representing the same value as the specified {@code BigInteger} + * . This is the inverse operation of {@link #bigIntegerValue()}. * * @throws IllegalArgumentException if {@code value} is negative or {@code value >= 2^64} */ @@ -111,7 +75,7 @@ public final class UnsignedLong extends Number implements Comparable<UnsignedLon checkNotNull(value); checkArgument(value.signum() >= 0 && value.bitLength() <= Long.SIZE, "value (%s) is outside the range for an unsigned long value", value); - return fromLongBits(value.longValue()); + return asUnsigned(value.longValue()); } /** @@ -134,121 +98,50 @@ public final class UnsignedLong extends Number implements Comparable<UnsignedLon * {@link Character#MAX_RADIX} */ public static UnsignedLong valueOf(String string, int radix) { - return fromLongBits(UnsignedLongs.parseUnsignedLong(string, radix)); + return asUnsigned(UnsignedLongs.parseUnsignedLong(string, radix)); } /** * Returns the result of adding this and {@code val}. If the result would have more than 64 bits, * returns the low 64 bits of the result. - * - * @deprecated Use {@link #plus(UnsignedLong)}. This method is scheduled to be removed in Guava - * release 15.0. */ - @Deprecated - @Beta public UnsignedLong add(UnsignedLong val) { - return plus(val); - } - - /** - * Returns the result of adding this and {@code val}. If the result would have more than 64 bits, - * returns the low 64 bits of the result. - * - * @since 14.0 - */ - public UnsignedLong plus(UnsignedLong val) { - return fromLongBits(this.value + checkNotNull(val).value); + checkNotNull(val); + return asUnsigned(this.value + val.value); } /** * Returns the result of subtracting this and {@code val}. If the result would be negative, * returns the low 64 bits of the result. - * - * @deprecated Use {@link #minus(UnsignedLong)}. This method is scheduled to be removed in Guava - * release 15.0. */ - @Deprecated - @Beta public UnsignedLong subtract(UnsignedLong val) { - return minus(val); - } - - /** - * Returns the result of subtracting this and {@code val}. If the result would have more than 64 - * bits, returns the low 64 bits of the result. - * - * @since 14.0 - */ - public UnsignedLong minus(UnsignedLong val) { - return fromLongBits(this.value - checkNotNull(val).value); + checkNotNull(val); + return asUnsigned(this.value - val.value); } /** * Returns the result of multiplying this and {@code val}. If the result would have more than 64 * bits, returns the low 64 bits of the result. - * - * @deprecated Use {@link #times(UnsignedLong)}. This method is scheduled to be removed in Guava - * release 15.0. */ - @Deprecated - @Beta public UnsignedLong multiply(UnsignedLong val) { - return times(val); - } - - /** - * Returns the result of multiplying this and {@code val}. If the result would have more than 64 - * bits, returns the low 64 bits of the result. - * - * @since 14.0 - */ - @CheckReturnValue - public UnsignedLong times(UnsignedLong val) { - return fromLongBits(value * checkNotNull(val).value); + checkNotNull(val); + return asUnsigned(value * val.value); } /** * Returns the result of dividing this by {@code val}. - * - * @deprecated Use {@link #dividedBy(UnsignedLong)}. This method is scheduled to be removed in - * Guava release 15.0. */ - @Deprecated - @Beta public UnsignedLong divide(UnsignedLong val) { - return dividedBy(val); - } - - /** - * Returns the result of dividing this by {@code val}. - * - * @since 14.0 - */ - @CheckReturnValue - public UnsignedLong dividedBy(UnsignedLong val) { - return fromLongBits(UnsignedLongs.divide(value, checkNotNull(val).value)); + checkNotNull(val); + return asUnsigned(UnsignedLongs.divide(value, val.value)); } /** * Returns the remainder of dividing this by {@code val}. - * - * @deprecated Use {@link #mod(UnsignedLong)}. This method is scheduled to be removed in Guava - * release 15.0. */ - @Deprecated - @Beta public UnsignedLong remainder(UnsignedLong val) { - return mod(val); - } - - /** - * Returns this modulo {@code val}. - * - * @since 14.0 - */ - @CheckReturnValue - public UnsignedLong mod(UnsignedLong val) { - return fromLongBits(UnsignedLongs.remainder(value, checkNotNull(val).value)); + checkNotNull(val); + return asUnsigned(UnsignedLongs.remainder(value, val.value)); } /** |