Class HashCode
- Direct Known Subclasses:
HashCode.BytesHashCode
,HashCode.IntHashCode
,HashCode.LongHashCode
- Since:
- 11.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
private static final class
private static final class
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract byte[]
asBytes()
Returns the value of this hash code as a byte array.abstract int
asInt()
Returns the first four bytes of this hashcode's bytes, converted to anint
value in little-endian order.abstract long
asLong()
Returns the first eight bytes of this hashcode's bytes, converted to along
value in little-endian order.abstract int
bits()
Returns the number of bits in this hash code; a positive multiple of 8.private static int
decode
(char ch) final boolean
Returnstrue
ifobject
is aHashCode
instance with the identical byte representation to this hash code.(package private) abstract boolean
equalsSameBits
(HashCode that) Returns whether thisHashCode
and thatHashCode
have the same value, given that they have the same number of bits.static HashCode
fromBytes
(byte[] bytes) Creates aHashCode
from a byte array.(package private) static HashCode
fromBytesNoCopy
(byte[] bytes) Creates aHashCode
from a byte array.static HashCode
fromInt
(int hash) Creates a 32-bitHashCode
representation of the given int value.static HashCode
fromLong
(long hash) Creates a 64-bitHashCode
representation of the given long value.static HashCode
fromString
(String string) Creates aHashCode
from a hexadecimal (base 16
) encoded string.(package private) byte[]
Returns a mutable view of the underlying bytes for the givenHashCode
if it is a byte-based hashcode.final int
hashCode()
Returns a "Java hash code" for thisHashCode
instance; this is well-defined (so, for example, you can safely putHashCode
instances into aHashSet
) but is otherwise probably not what you want to use.abstract long
If this hashcode has enough bits, returnsasLong()
, otherwise returns along
value withasBytes()
as the least-significant bytes and0x00
as the remaining most-significant bytes.final String
toString()
Returns a string containing each byte ofasBytes()
, in order, as a two-digit unsigned hexadecimal number in lower case.int
writeBytesTo
(byte[] dest, int offset, int maxLength) Copies bytes from this hash code intodest
.(package private) abstract void
writeBytesToImpl
(byte[] dest, int offset, int maxLength)
-
Field Details
-
hexDigits
private static final char[] hexDigits
-
-
Constructor Details
-
HashCode
HashCode()
-
-
Method Details
-
bits
public abstract int bits()Returns the number of bits in this hash code; a positive multiple of 8. -
asInt
public abstract int asInt()Returns the first four bytes of this hashcode's bytes, converted to anint
value in little-endian order.- Throws:
IllegalStateException
- ifbits() < 32
-
asLong
public abstract long asLong()Returns the first eight bytes of this hashcode's bytes, converted to along
value in little-endian order.- Throws:
IllegalStateException
- ifbits() < 64
-
padToLong
public abstract long padToLong()If this hashcode has enough bits, returnsasLong()
, otherwise returns along
value withasBytes()
as the least-significant bytes and0x00
as the remaining most-significant bytes.- Since:
- 14.0 (since 11.0 as
Hashing.padToLong(HashCode)
)
-
asBytes
public abstract byte[] asBytes()Returns the value of this hash code as a byte array. The caller may modify the byte array; changes to it will not be reflected in thisHashCode
object or any other arrays returned by this method. -
writeBytesTo
public int writeBytesTo(byte[] dest, int offset, int maxLength) Copies bytes from this hash code intodest
.- Parameters:
dest
- the byte array into which the hash code will be writtenoffset
- the start offset in the datamaxLength
- the maximum number of bytes to write- Returns:
- the number of bytes written to
dest
- Throws:
IndexOutOfBoundsException
- if there is not enough room indest
-
writeBytesToImpl
abstract void writeBytesToImpl(byte[] dest, int offset, int maxLength) -
getBytesInternal
byte[] getBytesInternal()Returns a mutable view of the underlying bytes for the givenHashCode
if it is a byte-based hashcode. Otherwise it returnsasBytes()
. Do not mutate this array or else you will break the immutability contract ofHashCode
. -
equalsSameBits
Returns whether thisHashCode
and thatHashCode
have the same value, given that they have the same number of bits. -
fromInt
Creates a 32-bitHashCode
representation of the given int value. The underlying bytes are interpreted in little endian order.- Since:
- 15.0 (since 12.0 in HashCodes)
-
fromLong
Creates a 64-bitHashCode
representation of the given long value. The underlying bytes are interpreted in little endian order.- Since:
- 15.0 (since 12.0 in HashCodes)
-
fromBytes
Creates aHashCode
from a byte array. The array is defensively copied to preserve the immutability contract ofHashCode
. The array cannot be empty.- Since:
- 15.0 (since 12.0 in HashCodes)
-
fromBytesNoCopy
Creates aHashCode
from a byte array. The array is not copied defensively, so it must be handed-off so as to preserve the immutability contract ofHashCode
. -
fromString
Creates aHashCode
from a hexadecimal (base 16
) encoded string. The string must be at least 2 characters long, and contain only valid, lower-cased hexadecimal characters.This method accepts the exact format generated by
toString()
. If you require more lenientbase 16
decoding, please useBaseEncoding.decode(java.lang.CharSequence)
(and pass the result tofromBytes(byte[])
).- Since:
- 15.0
-
decode
private static int decode(char ch) -
equals
Returnstrue
ifobject
is aHashCode
instance with the identical byte representation to this hash code.Security note: this method uses a constant-time (not short-circuiting) implementation to protect against timing attacks.
-
hashCode
public final int hashCode()Returns a "Java hash code" for thisHashCode
instance; this is well-defined (so, for example, you can safely putHashCode
instances into aHashSet
) but is otherwise probably not what you want to use. -
toString
Returns a string containing each byte ofasBytes()
, in order, as a two-digit unsigned hexadecimal number in lower case.Note that if the output is considered to be a single hexadecimal number, whether this string is big-endian or little-endian depends on the byte order of
asBytes()
. This may be surprising for implementations ofHashCode
that represent the number in big-endian since everything else in the hashing API uniformly treats multibyte values as little-endian.To create a
HashCode
from its string representation, seefromString(java.lang.String)
.
-