Class UnsignedLong
- java.lang.Object
-
- java.lang.Number
-
- com.google.common.primitives.UnsignedLong
-
- All Implemented Interfaces:
Serializable,Comparable<UnsignedLong>
@GwtCompatible(serializable=true) public final class UnsignedLong extends Number implements Comparable<UnsignedLong>, Serializable
A wrapper class for unsignedlongvalues, supporting arithmetic operations.In some cases, when speed is more important than code readability, it may be faster simply to treat primitive
longvalues as unsigned, using the methods fromUnsignedLongs.See the Guava User Guide article on unsigned primitive utilities.
- Since:
- 11.0
- Author:
- Louis Wasserman, Colin Evans
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static UnsignedLongMAX_VALUEstatic UnsignedLongONEstatic UnsignedLongZERO
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigIntegerbigIntegerValue()Returns the value of thisUnsignedLongas aBigInteger.intcompareTo(UnsignedLong o)Compares this object with the specified object for order.UnsignedLongdividedBy(UnsignedLong val)Returns the result of dividing this byval.doubledoubleValue()Returns the value of thisUnsignedLongas adouble, analogous to a widening primitive conversion fromlongtodouble, and correctly rounded.booleanequals(Object obj)Indicates whether some other object is "equal to" this one.floatfloatValue()Returns the value of thisUnsignedLongas afloat, analogous to a widening primitive conversion fromlongtofloat, and correctly rounded.static UnsignedLongfromLongBits(long bits)Returns anUnsignedLongcorresponding to a given bit representation.inthashCode()Returns a hash code value for the object.intintValue()Returns the value of thisUnsignedLongas anint.longlongValue()Returns the value of thisUnsignedLongas along.UnsignedLongminus(UnsignedLong val)Returns the result of subtracting this andval.UnsignedLongmod(UnsignedLong val)Returns this moduloval.UnsignedLongplus(UnsignedLong val)Returns the result of adding this andval.UnsignedLongtimes(UnsignedLong val)Returns the result of multiplying this andval.StringtoString()Returns a string representation of theUnsignedLongvalue, in base 10.StringtoString(int radix)Returns a string representation of theUnsignedLongvalue, in baseradix.static UnsignedLongvalueOf(long value)Returns anUnsignedLongrepresenting the same value as the specifiedlong.static UnsignedLongvalueOf(String string)Returns anUnsignedLongholding the value of the specifiedString, parsed as an unsignedlongvalue.static UnsignedLongvalueOf(String string, int radix)Returns anUnsignedLongholding the value of the specifiedString, parsed as an unsignedlongvalue in the specified radix.static UnsignedLongvalueOf(BigInteger value)Returns aUnsignedLongrepresenting the same value as the specifiedBigInteger.-
Methods inherited from class java.lang.Number
byteValue, shortValue
-
-
-
-
Field Detail
-
ZERO
public static final UnsignedLong ZERO
-
ONE
public static final UnsignedLong ONE
-
MAX_VALUE
public static final UnsignedLong MAX_VALUE
-
-
Method Detail
-
fromLongBits
public static UnsignedLong fromLongBits(long bits)
Returns anUnsignedLongcorresponding to a given bit representation. The argument is interpreted as an unsigned 64-bit value. Specifically, the sign bit ofbitsis interpreted as a normal bit, and all other bits are treated as usual.If the argument is nonnegative, the returned result will be equal to
bits, otherwise, the result will be equal to2^64 + bits.To represent decimal constants less than
2^63, considervalueOf(long)instead.- Since:
- 14.0
-
valueOf
@CanIgnoreReturnValue public static UnsignedLong valueOf(long value)
Returns anUnsignedLongrepresenting the same value as the specifiedlong.- Throws:
IllegalArgumentException- ifvalueis negative- Since:
- 14.0
-
valueOf
@CanIgnoreReturnValue public static UnsignedLong valueOf(BigInteger value)
Returns aUnsignedLongrepresenting the same value as the specifiedBigInteger. This is the inverse operation ofbigIntegerValue().- Throws:
IllegalArgumentException- ifvalueis negative orvalue >= 2^64
-
valueOf
@CanIgnoreReturnValue public static UnsignedLong valueOf(String string)
Returns anUnsignedLongholding the value of the specifiedString, parsed as an unsignedlongvalue.- Throws:
NumberFormatException- if the string does not contain a parsable unsignedlongvalue
-
valueOf
@CanIgnoreReturnValue public static UnsignedLong valueOf(String string, int radix)
Returns anUnsignedLongholding the value of the specifiedString, parsed as an unsignedlongvalue in the specified radix.- Throws:
NumberFormatException- if the string does not contain a parsable unsignedlongvalue, orradixis not betweenCharacter.MIN_RADIXandCharacter.MAX_RADIX
-
plus
public UnsignedLong plus(UnsignedLong val)
Returns the result of adding this andval. If the result would have more than 64 bits, returns the low 64 bits of the result.- Since:
- 14.0
-
minus
public UnsignedLong minus(UnsignedLong val)
Returns the result of subtracting this andval. If the result would have more than 64 bits, returns the low 64 bits of the result.- Since:
- 14.0
-
times
public UnsignedLong times(UnsignedLong val)
Returns the result of multiplying this andval. If the result would have more than 64 bits, returns the low 64 bits of the result.- Since:
- 14.0
-
dividedBy
public UnsignedLong dividedBy(UnsignedLong val)
Returns the result of dividing this byval.- Since:
- 14.0
-
mod
public UnsignedLong mod(UnsignedLong val)
Returns this moduloval.- Since:
- 14.0
-
intValue
public int intValue()
Returns the value of thisUnsignedLongas anint.
-
longValue
public long longValue()
Returns the value of thisUnsignedLongas along. This is an inverse operation tofromLongBits(long).Note that if this
UnsignedLongholds a value>= 2^63, the returned value will be equal tothis - 2^64.
-
floatValue
public float floatValue()
Returns the value of thisUnsignedLongas afloat, analogous to a widening primitive conversion fromlongtofloat, and correctly rounded.- Specified by:
floatValuein classNumber- Returns:
- the numeric value represented by this object after conversion
to type
float.
-
doubleValue
public double doubleValue()
Returns the value of thisUnsignedLongas adouble, analogous to a widening primitive conversion fromlongtodouble, and correctly rounded.- Specified by:
doubleValuein classNumber- Returns:
- the numeric value represented by this object after conversion
to type
double.
-
bigIntegerValue
public BigInteger bigIntegerValue()
Returns the value of thisUnsignedLongas aBigInteger.
-
compareTo
public int compareTo(UnsignedLong o)
Description copied from interface:java.lang.ComparableCompares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.The implementor must ensure
sgn(x.compareTo(y)) == -sgn(y.compareTo(x))for allxandy. (This implies thatx.compareTo(y)must throw an exception iffy.compareTo(x)throws an exception.)The implementor must also ensure that the relation is transitive:
(x.compareTo(y) > 0 && y.compareTo(z) > 0)impliesx.compareTo(z) > 0.Finally, the implementor must ensure that
x.compareTo(y)==0implies thatsgn(x.compareTo(z)) == sgn(y.compareTo(z)), for allz.It is strongly recommended, but not strictly required that
(x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements theComparableinterface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."In the foregoing description, the notation
sgn(expression)designates the mathematical signum function, which is defined to return one of-1,0, or1according to whether the value of expression is negative, zero, or positive, respectively.- Specified by:
compareToin interfaceComparable<UnsignedLong>- Parameters:
o- the object to be compared.- Returns:
- a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
-
hashCode
public int hashCode()
Description copied from class:java.lang.ObjectReturns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided byHashMap.The general contract of
hashCodeis:- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
hashCodemethod must consistently return the same integer, provided no information used inequalscomparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. - If two objects are equal according to the
equals(Object)method, then calling thehashCodemethod on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal
according to the
Object.equals(java.lang.Object)method, then calling thehashCodemethod on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by class
Objectdoes return distinct integers for distinct objects. (The hashCode may or may not be implemented as some function of an object's memory address at some point in time.)- Overrides:
hashCodein classObject- Returns:
- a hash code value for this object.
- See Also:
Object.equals(java.lang.Object),System.identityHashCode(java.lang.Object)
- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
-
equals
public boolean equals(@CheckForNull Object obj)
Description copied from class:java.lang.ObjectIndicates whether some other object is "equal to" this one.The
equalsmethod implements an equivalence relation on non-null object references:- It is reflexive: for any non-null reference value
x,x.equals(x)should returntrue. - It is symmetric: for any non-null reference values
xandy,x.equals(y)should returntrueif and only ify.equals(x)returnstrue. - It is transitive: for any non-null reference values
x,y, andz, ifx.equals(y)returnstrueandy.equals(z)returnstrue, thenx.equals(z)should returntrue. - It is consistent: for any non-null reference values
xandy, multiple invocations ofx.equals(y)consistently returntrueor consistently returnfalse, provided no information used inequalscomparisons on the objects is modified. - For any non-null reference value
x,x.equals(null)should returnfalse.
The
equalsmethod for classObjectimplements the most discriminating possible equivalence relation on objects; that is, for any non-null reference valuesxandy, this method returnstrueif and only ifxandyrefer to the same object (x == yhas the valuetrue).Note that it is generally necessary to override the
hashCodemethod whenever this method is overridden, so as to maintain the general contract for thehashCodemethod, which states that equal objects must have equal hash codes.- Overrides:
equalsin classObject- Parameters:
obj- the reference object with which to compare.- Returns:
trueif this object is the same as the obj argument;falseotherwise.- See Also:
Object.hashCode(),HashMap
- It is reflexive: for any non-null reference value
-
toString
public String toString()
Returns a string representation of theUnsignedLongvalue, in base 10.
-
-