javadoc.com.google.common.primitives.UnsignedInts.html Maven / Gradle / Ivy
UnsignedInts (Guava: Google Core Libraries for Java 11.0.1 API)
Overview
Package
Class
Use
Tree
Deprecated
Index
Help
PREV CLASS
NEXT CLASS
FRAMES
NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD
DETAIL: FIELD | CONSTR | METHOD
com.google.common.primitives
Class UnsignedInts
java.lang.Object
com.google.common.primitives.UnsignedInts
@Beta
@GwtCompatible
public final class UnsignedInts
- extends Object
Static utility methods pertaining to int
primitives that interpret values as
unsigned (that is, any negative value x
is treated as the positive value
2^32 + x
). The methods for which signedness is not an issue are in Ints
, as well
as signed versions of methods for which signedness is an issue.
In addition, this class provides several static methods for converting an int
to a
String
and a String
to an int
that treat the int
as an unsigned
number.
Users of these utilities must be extremely careful not to mix up signed and unsigned
int
values. When possible, it is recommended that the UnsignedInteger
wrapper
class be used, at a small efficiency penalty, to enforce the distinction in the type system.
- Since:
- 11.0
- Author:
- Louis Wasserman
Method Summary | |
---|---|
static int |
compare(int a,
int b)
Compares the two specified int values, treating them as unsigned values between
0 and 2^32 - 1 inclusive. |
static int |
divide(int dividend,
int divisor)
Returns dividend / divisor, where the dividend and divisor are treated as unsigned 32-bit quantities. |
static String |
join(String separator,
int... array)
Returns a string containing the supplied unsigned int values separated by
separator . |
static Comparator<int[]> |
lexicographicalComparator()
Returns a comparator that compares two arrays of unsigned int values lexicographically. |
static int |
max(int... array)
Returns the greatest value present in array , treating values as unsigned. |
static int |
min(int... array)
Returns the least value present in array , treating values as unsigned. |
static int |
parseUnsignedInt(String s)
Returns the unsigned int value represented by the given decimal string. |
static int |
parseUnsignedInt(String string,
int radix)
Returns the unsigned int value represented by a string with the given radix. |
static int |
remainder(int dividend,
int divisor)
Returns dividend % divisor, where the dividend and divisor are treated as unsigned 32-bit quantities. |
static long |
toLong(int value)
Returns the value of the given int as a long , when treated as unsigned. |
static String |
toString(int x)
Returns a string representation of x, where x is treated as unsigned. |
static String |
toString(int x,
int radix)
Returns a string representation of x for the given radix, where x is treated
as unsigned. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
compare
public static int compare(int a, int b)
- Compares the two specified
int
values, treating them as unsigned values between0
and2^32 - 1
inclusive.- Parameters:
a
- the first unsignedint
to compareb
- the second unsignedint
to compare- Returns:
- a negative value if
a
is less thanb
; a positive value ifa
is greater thanb
; or zero if they are equal
toLong
public static long toLong(int value)
- Returns the value of the given
int
as along
, when treated as unsigned.
min
public static int min(int... array)
- Returns the least value present in
array
, treating values as unsigned.- Parameters:
array
- a nonempty array of unsignedint
values- Returns:
- the value present in
array
that is less than or equal to every other value in the array according tocompare(int, int)
- Throws:
IllegalArgumentException
- ifarray
is empty
max
public static int max(int... array)
- Returns the greatest value present in
array
, treating values as unsigned.- Parameters:
array
- a nonempty array of unsignedint
values- Returns:
- the value present in
array
that is greater than or equal to every other value in the array according tocompare(int, int)
- Throws:
IllegalArgumentException
- ifarray
is empty
join
public static String join(String separator, int... array)
- Returns a string containing the supplied unsigned
int
values separated byseparator
. For example,join("-", 1, 2, 3)
returns the string"1-2-3"
.- Parameters:
separator
- the text that should appear between consecutive values in the resulting string (but not at the start or end)array
- an array of unsignedint
values, possibly empty
lexicographicalComparator
public static Comparator<int[]> lexicographicalComparator()
- Returns a comparator that compares two arrays of unsigned
int
values lexicographically. That is, it compares, usingcompare(int, int)
), the first pair of values that follow any common prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For example,[] < [1] < [1, 2] < [2] < [1 << 31]
.The returned comparator is inconsistent with
Object.equals(Object)
(since arrays support only identity equality), but it is consistent withArrays.equals(int[], int[])
.
divide
public static int divide(int dividend, int divisor)
- Returns dividend / divisor, where the dividend and divisor are treated as unsigned 32-bit
quantities.
- Parameters:
dividend
- the dividend (numerator)divisor
- the divisor (denominator)- Throws:
ArithmeticException
- if divisor is 0
remainder
public static int remainder(int dividend, int divisor)
- Returns dividend % divisor, where the dividend and divisor are treated as unsigned 32-bit
quantities.
- Parameters:
dividend
- the dividend (numerator)divisor
- the divisor (denominator)- Throws:
ArithmeticException
- if divisor is 0
parseUnsignedInt
public static int parseUnsignedInt(String s)
- Returns the unsigned
int
value represented by the given decimal string.- Throws:
NumberFormatException
- if the string does not contain a valid unsigned integer, or if the value represented is too large to fit in an unsignedint
.NullPointerException
- ifs
is null
parseUnsignedInt
public static int parseUnsignedInt(String string, int radix)
- Returns the unsigned
int
value represented by a string with the given radix.- Parameters:
string
- the string containing the unsigned integer representation to be parsed.radix
- the radix to use while parsings
; must be betweenCharacter.MIN_RADIX
andCharacter.MAX_RADIX
.- Throws:
NumberFormatException
- if the string does not contain a valid unsignedint
, or if supplied radix is invalid.
toString
public static String toString(int x)
- Returns a string representation of x, where x is treated as unsigned.
toString
public static String toString(int x, int radix)
- Returns a string representation of
x
for the given radix, wherex
is treated as unsigned.- Parameters:
x
- the value to convert to a string.radix
- the radix to use while working withx
- Throws:
IllegalArgumentException
- ifradix
is not betweenCharacter.MIN_RADIX
andCharacter.MAX_RADIX
.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 2010-2012. All Rights Reserved.