Class UnsignedInteger
-
-
- com.google.common.primitives.UnsignedInteger
-
-
- All Implemented Interfaces:
,
<UnsignedInteger>
@GwtCompatible(emulated=true) public final class UnsignedInteger extends implements <UnsignedInteger>
A wrapper class for unsignedint
values, supporting arithmetic operations.In some cases, when speed is more important than code readability, it may be faster simply to treat primitive
int
values as unsigned, using the methods fromUnsignedInts
.See the Guava User Guide article on .
- Since:
- 11.0
- Author:
- Louis Wasserman
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static UnsignedInteger
MAX_VALUE
static UnsignedInteger
ONE
static UnsignedInteger
ZERO
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description bigIntegerValue()
Returns the value of thisUnsignedInteger
as a .int
compareTo(UnsignedInteger other)
Compares this unsigned integer to another unsigned integer.UnsignedInteger
dividedBy(UnsignedInteger val)
Returns the result of dividing this byval
.double
doubleValue()
Returns the value of thisUnsignedInteger
as afloat
, analogous to a widening primitive conversion fromint
todouble
, and correctly rounded.boolean
obj)
Indicates whether some other object is "equal to" this one.float
floatValue()
Returns the value of thisUnsignedInteger
as afloat
, analogous to a widening primitive conversion fromint
tofloat
, and correctly rounded.static UnsignedInteger
fromIntBits(int bits)
Returns anUnsignedInteger
corresponding to a given bit representation.int
hashCode()
Returns a hash code value for the object.int
intValue()
Returns the value of thisUnsignedInteger
as anint
.long
longValue()
Returns the value of thisUnsignedInteger
as along
.UnsignedInteger
minus(UnsignedInteger val)
Returns the result of subtracting this andval
.UnsignedInteger
mod(UnsignedInteger val)
Returns this modval
.UnsignedInteger
plus(UnsignedInteger val)
Returns the result of adding this andval
.UnsignedInteger
times(UnsignedInteger val)
Returns the result of multiplying this andval
.toString()
Returns a string representation of theUnsignedInteger
value, in base 10.toString(int radix)
Returns a string representation of theUnsignedInteger
value, in baseradix
.static UnsignedInteger
valueOf(long value)
Returns anUnsignedInteger
that is equal tovalue
, if possible.static UnsignedInteger
string)
Returns anUnsignedInteger
holding the value of the specifiedString
, parsed as an unsignedint
value.static UnsignedInteger
string, int radix)
Returns anUnsignedInteger
holding the value of the specifiedString
, parsed as an unsignedint
value in the specified radix.static UnsignedInteger
value)
Returns aUnsignedInteger
representing the same value as the specified .
-
-
-
Field Detail
-
ZERO
public static final UnsignedInteger ZERO
-
ONE
public static final UnsignedInteger ONE
-
MAX_VALUE
public static final UnsignedInteger MAX_VALUE
-
-
Method Detail
-
fromIntBits
public static UnsignedInteger fromIntBits(int bits)
Returns anUnsignedInteger
corresponding to a given bit representation. The argument is interpreted as an unsigned 32-bit value. Specifically, the sign bit ofbits
is 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^32 + bits
.To represent unsigned decimal constants, consider
valueOf(long)
instead.- Since:
- 14.0
-
valueOf
public static UnsignedInteger valueOf(long value)
Returns anUnsignedInteger
that is equal tovalue
, if possible. The inverse operation oflongValue()
.
-
valueOf
public static value)
Returns aUnsignedInteger
representing the same value as the specified . This is the inverse operation ofbigIntegerValue()
.- Throws:
- if
value
is negative orvalue >= 2^32
-
valueOf
public static string)
Returns anUnsignedInteger
holding the value of the specifiedString
, parsed as an unsignedint
value.- Throws:
- if the string does not contain a parsable unsigned
int
value
-
valueOf
public static string, int radix)
Returns anUnsignedInteger
holding the value of the specifiedString
, parsed as an unsignedint
value in the specified radix.- Throws:
- if the string does not contain a parsable unsigned
int
value
-
plus
public UnsignedInteger plus(UnsignedInteger val)
Returns the result of adding this andval
. If the result would have more than 32 bits, returns the low 32 bits of the result.- Since:
- 14.0
-
minus
public UnsignedInteger minus(UnsignedInteger val)
Returns the result of subtracting this andval
. If the result would be negative, returns the low 32 bits of the result.- Since:
- 14.0
-
times
@GwtIncompatible public UnsignedInteger times(UnsignedInteger val)
Returns the result of multiplying this andval
. If the result would have more than 32 bits, returns the low 32 bits of the result.- Since:
- 14.0
-
dividedBy
public UnsignedInteger dividedBy(UnsignedInteger val)
Returns the result of dividing this byval
.- Throws:
- if
val
is zero- Since:
- 14.0
-
mod
public UnsignedInteger mod(UnsignedInteger val)
Returns this modval
.- Throws:
- if
val
is zero- Since:
- 14.0
-
intValue
public int intValue()
Returns the value of thisUnsignedInteger
as anint
. This is an inverse operation tofromIntBits(int)
.Note that if this
UnsignedInteger
holds a value>= 2^31
, the returned value will be equal tothis - 2^32
.- Specified by:
in class
- Returns:
- the numeric value represented by this object after conversion
to type
int
.
-
longValue
public long longValue()
Returns the value of thisUnsignedInteger
as along
.- Specified by:
in class
- Returns:
- the numeric value represented by this object after conversion
to type
long
.
-
floatValue
public float floatValue()
Returns the value of thisUnsignedInteger
as afloat
, analogous to a widening primitive conversion fromint
tofloat
, and correctly rounded.- Specified by:
in class
- Returns:
- the numeric value represented by this object after conversion
to type
float
.
-
doubleValue
public double doubleValue()
Returns the value of thisUnsignedInteger
as afloat
, analogous to a widening primitive conversion fromint
todouble
, and correctly rounded.- Specified by:
in class
- Returns:
- the numeric value represented by this object after conversion
to type
double
.
-
bigIntegerValue
public bigIntegerValue()
Returns the value of thisUnsignedInteger
as a .
-
compareTo
public int compareTo(UnsignedInteger other)
Compares this unsigned integer to another unsigned integer. Returns0
if they are equal, a negative number ifthis < other
, and a positive number ifthis > other
.- Specified by:
in interface
<UnsignedInteger>
- Parameters:
other
- 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:Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by .The general contract of
hashCode
is:- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
hashCode
method must consistently return the same integer, provided no information used inequals
comparisons 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 thehashCode
method 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
method, then calling the
hashCode
method 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
Object
does 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:
in class
- Returns:
- a hash code value for this object.
- See Also:
- ,
- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
-
equals
public boolean obj)
Description copied from class:Indicates whether some other object is "equal to" this one.The
equals
method 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
x
andy
,x.equals(y)
should returntrue
if and only ify.equals(x)
returnstrue
. - It is transitive: for any non-null reference values
x
,y
, andz
, ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
, thenx.equals(z)
should returntrue
. - It is consistent: for any non-null reference values
x
andy
, multiple invocations ofx.equals(y)
consistently returntrue
or consistently returnfalse
, provided no information used inequals
comparisons on the objects is modified. - For any non-null reference value
x
,x.equals(null)
should returnfalse
.
The
equals
method for classObject
implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference valuesx
andy
, this method returnstrue
if and only ifx
andy
refer to the same object (x == y
has the valuetrue
).Note that it is generally necessary to override the
hashCode
method whenever this method is overridden, so as to maintain the general contract for thehashCode
method, which states that equal objects must have equal hash codes.- Overrides:
in class
- Parameters:
obj
- the reference object with which to compare.- Returns:
true
if this object is the same as the obj argument;false
otherwise.- See Also:
- ,
- It is reflexive: for any non-null reference value
-
toString
public toString()
Returns a string representation of theUnsignedInteger
value, in base 10.- Overrides:
in class
- Returns:
- a string representation of the object.
-
toString
public toString(int radix)
Returns a string representation of theUnsignedInteger
value, in baseradix
. Ifradix < Character.MIN_RADIX
orradix > Character.MAX_RADIX
, the radix10
is used.
-
-