Top Description Inners Fields Constructors Methods
com.google.common.hash

public abstract Class HashCode

extends Object
Class Inheritance
Known Direct Subclasses
com.google.common.hash.HashCode.IntHashCode, com.google.common.hash.HashCode.LongHashCode, com.google.common.hash.HashCode.BytesHashCode
Annotations
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkState

An immutable hash code of arbitrary bit length.
Authors
Dimitris Andreou, Kurt Alfred Kluever
Since
11.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static class

Field Summary

Modifier and TypeField and Description
private static final char[]

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public abstract byte[]
asBytes()

Returns the value of this hash code as a byte array.

public abstract int
asInt()

Returns the first four bytes of this hashcode's bytes, converted to an int value in little-endian order.

public abstract long
asLong()

Returns the first eight bytes of this hashcode's bytes, converted to a long value in little-endian order.

public abstract int
bits()

Returns the number of bits in this hash code; a positive multiple of 8.

private static int
decode(char ch)

public final boolean
equals(Object
the reference object with which to compare.
object
)

Overrides java.lang.Object.equals.

Returns true if object is a HashCode instance with the identical byte representation to this hash code.
pack-priv abstract boolean
equalsSameBits(HashCode that)

Returns whether this HashCode and that HashCode have the same value, given that they have the same number of bits.

public static HashCode
fromBytes(byte[] bytes)

Creates a HashCode from a byte array.

pack-priv static HashCode
fromBytesNoCopy(byte[] bytes)

Creates a HashCode from a byte array.

public static HashCode
fromInt(int hash)

Creates a 32-bit HashCode representation of the given int value.

public static HashCode
fromLong(long hash)

Creates a 64-bit HashCode representation of the given long value.

public static HashCode
fromString(String string)

Creates a HashCode from a hexadecimal (base 16) encoded string.

pack-priv byte[]
getBytesInternal()

Returns a mutable view of the underlying bytes for the given HashCode if it is a byte-based hashcode.

public final int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a "Java hash code" for this HashCode instance; this is well-defined (so, for example, you can safely put HashCode instances into a HashSet) but is otherwise probably not what you want to use.
public abstract long
padToLong()

If this hashcode has enough bits, returns asLong(), otherwise returns a long value with asBytes() as the least-significant bytes and 0x00 as the remaining most-significant bytes.

public final String
toString()

Overrides java.lang.Object.toString.

Returns a string containing each byte of asBytes, in order, as a two-digit unsigned hexadecimal number in lower case.
public int

Returns:

the number of bytes written to dest
writeBytesTo
(byte[]
the byte array into which the hash code will be written
dest
,
int
the start offset in the data
offset
,
int
the maximum number of bytes to write
maxLength
)

Copies bytes from this hash code into dest.

pack-priv abstract void
writeBytesToImpl(byte[] dest, int offset, int maxLength)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

hexDigitsback to summary
private static final char[] hexDigits

Constructor Detail

HashCodeback to summary
pack-priv HashCode()

Method Detail

asBytesback to summary
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 this HashCode object or any other arrays returned by this method.

asIntback to summary
public abstract int asInt()

Returns the first four bytes of this hashcode's bytes, converted to an int value in little-endian order.

Exceptions
IllegalStateException:
if bits() < 32
asLongback to summary
public abstract long asLong()

Returns the first eight bytes of this hashcode's bytes, converted to a long value in little-endian order.

Exceptions
IllegalStateException:
if bits() < 64
bitsback to summary
public abstract int bits()

Returns the number of bits in this hash code; a positive multiple of 8.

decodeback to summary
private static int decode(char ch)
equalsback to summary
public final boolean equals(Object object)

Overrides java.lang.Object.equals.

Returns true if object is a HashCode 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.

Parameters
object:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
equalsSameBitsback to summary
pack-priv abstract boolean equalsSameBits(HashCode that)

Returns whether this HashCode and that HashCode have the same value, given that they have the same number of bits.

fromBytesback to summary
public static HashCode fromBytes(byte[] bytes)

Creates a HashCode from a byte array. The array is defensively copied to preserve the immutability contract of HashCode. The array cannot be empty.

Since
15.0 (since 12.0 in HashCodes)
fromBytesNoCopyback to summary
pack-priv static HashCode fromBytesNoCopy(byte[] bytes)

Creates a HashCode from a byte array. The array is not copied defensively, so it must be handed-off so as to preserve the immutability contract of HashCode.

fromIntback to summary
public static HashCode fromInt(int hash)

Creates a 32-bit HashCode representation of the given int value. The underlying bytes are interpreted in little endian order.

Since
15.0 (since 12.0 in HashCodes)
fromLongback to summary
public static HashCode fromLong(long hash)

Creates a 64-bit HashCode representation of the given long value. The underlying bytes are interpreted in little endian order.

Since
15.0 (since 12.0 in HashCodes)
fromStringback to summary
public static HashCode fromString(String string)

Creates a HashCode 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 lenient base 16 decoding, please use com.google.common.io.BaseEncoding#decode (and pass the result to fromBytes).

Since
15.0
getBytesInternalback to summary
pack-priv byte[] getBytesInternal()

Returns a mutable view of the underlying bytes for the given HashCode if it is a byte-based hashcode. Otherwise it returns HashCode#asBytes. Do not mutate this array or else you will break the immutability contract of HashCode.

hashCodeback to summary
public final int hashCode()

Overrides java.lang.Object.hashCode.

Returns a "Java hash code" for this HashCode instance; this is well-defined (so, for example, you can safely put HashCode instances into a HashSet) but is otherwise probably not what you want to use.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

Annotations
@Override
padToLongback to summary
public abstract long padToLong()

If this hashcode has enough bits, returns asLong(), otherwise returns a long value with asBytes() as the least-significant bytes and 0x00 as the remaining most-significant bytes.

Since
14.0 (since 11.0 as Hashing.padToLong(HashCode))
toStringback to summary
public final String toString()

Overrides java.lang.Object.toString.

Returns a string containing each byte of asBytes, 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 of HashCode 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, see fromString.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override
writeBytesToback to summary
public int writeBytesTo(byte[] dest, int offset, int maxLength)

Copies bytes from this hash code into dest.

Parameters
dest:byte[]

the byte array into which the hash code will be written

offset:int

the start offset in the data

maxLength:int

the maximum number of bytes to write

Returns:int

the number of bytes written to dest

Annotations
@CanIgnoreReturnValue
Exceptions
IndexOutOfBoundsException:
if there is not enough room in dest
writeBytesToImplback to summary
pack-priv abstract void writeBytesToImpl(byte[] dest, int offset, int maxLength)
com.google.common.hash back to summary

private final Class HashCode.BytesHashCode

extends HashCode
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable

Field Summary

Modifier and TypeField and Description
pack-priv final byte[]
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
BytesHashCode(byte[] bytes)

Method Summary

Modifier and TypeMethod and Description
public byte[]
asBytes()

Implements abstract com.google.common.hash.HashCode.asBytes.

Returns the value of this hash code as a byte array.
public int
asInt()

Implements abstract com.google.common.hash.HashCode.asInt.

Returns the first four bytes of this hashcode's bytes, converted to an int value in little-endian order.
public long
asLong()

Implements abstract com.google.common.hash.HashCode.asLong.

Returns the first eight bytes of this hashcode's bytes, converted to a long value in little-endian order.
public int
bits()

Implements abstract com.google.common.hash.HashCode.bits.

Returns the number of bits in this hash code; a positive multiple of 8.
pack-priv boolean
equalsSameBits(HashCode that)

Implements abstract com.google.common.hash.HashCode.equalsSameBits.

Returns whether this HashCode and that HashCode have the same value, given that they have the same number of bits.
pack-priv byte[]
getBytesInternal()

Overrides com.google.common.hash.HashCode.getBytesInternal.

Returns a mutable view of the underlying bytes for the given HashCode if it is a byte-based hashcode.
public long
padToLong()

Implements abstract com.google.common.hash.HashCode.padToLong.

If this hashcode has enough bits, returns asLong(), otherwise returns a long value with asBytes() as the least-significant bytes and 0x00 as the remaining most-significant bytes.
pack-priv void
writeBytesToImpl(byte[] dest, int offset, int maxLength)

Implements abstract com.google.common.hash.HashCode.writeBytesToImpl.

Inherited from com.google.common.hash.HashCode:
equalsfromBytesfromBytesNoCopyfromIntfromLongfromStringhashCodetoStringwriteBytesTo

Field Detail

bytesback to summary
pack-priv final byte[] bytes
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

BytesHashCodeback to summary
pack-priv BytesHashCode(byte[] bytes)

Method Detail

asBytesback to summary
public byte[] asBytes()

Implements abstract com.google.common.hash.HashCode.asBytes.

Doc from com.google.common.hash.HashCode.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 this HashCode object or any other arrays returned by this method.

Annotations
@Override
asIntback to summary
public int asInt()

Implements abstract com.google.common.hash.HashCode.asInt.

Doc from com.google.common.hash.HashCode.asInt.

Returns the first four bytes of this hashcode's bytes, converted to an int value in little-endian order.

Annotations
@Override
asLongback to summary
public long asLong()

Implements abstract com.google.common.hash.HashCode.asLong.

Doc from com.google.common.hash.HashCode.asLong.

Returns the first eight bytes of this hashcode's bytes, converted to a long value in little-endian order.

Annotations
@Override
bitsback to summary
public int bits()

Implements abstract com.google.common.hash.HashCode.bits.

Doc from com.google.common.hash.HashCode.bits.

Returns the number of bits in this hash code; a positive multiple of 8.

Annotations
@Override
equalsSameBitsback to summary
pack-priv boolean equalsSameBits(HashCode that)

Implements abstract com.google.common.hash.HashCode.equalsSameBits.

Doc from com.google.common.hash.HashCode.equalsSameBits.

Returns whether this HashCode and that HashCode have the same value, given that they have the same number of bits.

Annotations
@Override
getBytesInternalback to summary
pack-priv byte[] getBytesInternal()

Overrides com.google.common.hash.HashCode.getBytesInternal.

Doc from com.google.common.hash.HashCode.getBytesInternal.

Returns a mutable view of the underlying bytes for the given HashCode if it is a byte-based hashcode. Otherwise it returns HashCode#asBytes. Do not mutate this array or else you will break the immutability contract of HashCode.

Annotations
@Override
padToLongback to summary
public long padToLong()

Implements abstract com.google.common.hash.HashCode.padToLong.

Doc from com.google.common.hash.HashCode.padToLong.

If this hashcode has enough bits, returns asLong(), otherwise returns a long value with asBytes() as the least-significant bytes and 0x00 as the remaining most-significant bytes.

Annotations
@Override
writeBytesToImplback to summary
pack-priv void writeBytesToImpl(byte[] dest, int offset, int maxLength)

Implements abstract com.google.common.hash.HashCode.writeBytesToImpl.

Annotations
@Override
com.google.common.hash back to summary

private final Class HashCode.IntHashCode

extends HashCode
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable

Field Summary

Modifier and TypeField and Description
pack-priv final int
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
IntHashCode(int hash)

Method Summary

Modifier and TypeMethod and Description
public byte[]
asBytes()

Implements abstract com.google.common.hash.HashCode.asBytes.

Returns the value of this hash code as a byte array.
public int
asInt()

Implements abstract com.google.common.hash.HashCode.asInt.

Returns the first four bytes of this hashcode's bytes, converted to an int value in little-endian order.
public long
asLong()

Implements abstract com.google.common.hash.HashCode.asLong.

Returns the first eight bytes of this hashcode's bytes, converted to a long value in little-endian order.
public int
bits()

Implements abstract com.google.common.hash.HashCode.bits.

Returns the number of bits in this hash code; a positive multiple of 8.
pack-priv boolean
equalsSameBits(HashCode that)

Implements abstract com.google.common.hash.HashCode.equalsSameBits.

Returns whether this HashCode and that HashCode have the same value, given that they have the same number of bits.
public long
padToLong()

Implements abstract com.google.common.hash.HashCode.padToLong.

If this hashcode has enough bits, returns asLong(), otherwise returns a long value with asBytes() as the least-significant bytes and 0x00 as the remaining most-significant bytes.
pack-priv void
writeBytesToImpl(byte[] dest, int offset, int maxLength)

Implements abstract com.google.common.hash.HashCode.writeBytesToImpl.

Inherited from com.google.common.hash.HashCode:
equalsfromBytesfromBytesNoCopyfromIntfromLongfromStringgetBytesInternalhashCodetoStringwriteBytesTo

Field Detail

hashback to summary
pack-priv final int hash
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

IntHashCodeback to summary
pack-priv IntHashCode(int hash)

Method Detail

asBytesback to summary
public byte[] asBytes()

Implements abstract com.google.common.hash.HashCode.asBytes.

Doc from com.google.common.hash.HashCode.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 this HashCode object or any other arrays returned by this method.

Annotations
@Override
asIntback to summary
public int asInt()

Implements abstract com.google.common.hash.HashCode.asInt.

Doc from com.google.common.hash.HashCode.asInt.

Returns the first four bytes of this hashcode's bytes, converted to an int value in little-endian order.

Annotations
@Override
asLongback to summary
public long asLong()

Implements abstract com.google.common.hash.HashCode.asLong.

Doc from com.google.common.hash.HashCode.asLong.

Returns the first eight bytes of this hashcode's bytes, converted to a long value in little-endian order.

Annotations
@Override
bitsback to summary
public int bits()

Implements abstract com.google.common.hash.HashCode.bits.

Doc from com.google.common.hash.HashCode.bits.

Returns the number of bits in this hash code; a positive multiple of 8.

Annotations
@Override
equalsSameBitsback to summary
pack-priv boolean equalsSameBits(HashCode that)

Implements abstract com.google.common.hash.HashCode.equalsSameBits.

Doc from com.google.common.hash.HashCode.equalsSameBits.

Returns whether this HashCode and that HashCode have the same value, given that they have the same number of bits.

Annotations
@Override
padToLongback to summary
public long padToLong()

Implements abstract com.google.common.hash.HashCode.padToLong.

Doc from com.google.common.hash.HashCode.padToLong.

If this hashcode has enough bits, returns asLong(), otherwise returns a long value with asBytes() as the least-significant bytes and 0x00 as the remaining most-significant bytes.

Annotations
@Override
writeBytesToImplback to summary
pack-priv void writeBytesToImpl(byte[] dest, int offset, int maxLength)

Implements abstract com.google.common.hash.HashCode.writeBytesToImpl.

Annotations
@Override
com.google.common.hash back to summary

private final Class HashCode.LongHashCode

extends HashCode
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable

Field Summary

Modifier and TypeField and Description
pack-priv final long
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
LongHashCode(long hash)

Method Summary

Modifier and TypeMethod and Description
public byte[]
asBytes()

Implements abstract com.google.common.hash.HashCode.asBytes.

Returns the value of this hash code as a byte array.
public int
asInt()

Implements abstract com.google.common.hash.HashCode.asInt.

Returns the first four bytes of this hashcode's bytes, converted to an int value in little-endian order.
public long
asLong()

Implements abstract com.google.common.hash.HashCode.asLong.

Returns the first eight bytes of this hashcode's bytes, converted to a long value in little-endian order.
public int
bits()

Implements abstract com.google.common.hash.HashCode.bits.

Returns the number of bits in this hash code; a positive multiple of 8.
pack-priv boolean
equalsSameBits(HashCode that)

Implements abstract com.google.common.hash.HashCode.equalsSameBits.

Returns whether this HashCode and that HashCode have the same value, given that they have the same number of bits.
public long
padToLong()

Implements abstract com.google.common.hash.HashCode.padToLong.

If this hashcode has enough bits, returns asLong(), otherwise returns a long value with asBytes() as the least-significant bytes and 0x00 as the remaining most-significant bytes.
pack-priv void
writeBytesToImpl(byte[] dest, int offset, int maxLength)

Implements abstract com.google.common.hash.HashCode.writeBytesToImpl.

Inherited from com.google.common.hash.HashCode:
equalsfromBytesfromBytesNoCopyfromIntfromLongfromStringgetBytesInternalhashCodetoStringwriteBytesTo

Field Detail

hashback to summary
pack-priv final long hash
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

LongHashCodeback to summary
pack-priv LongHashCode(long hash)

Method Detail

asBytesback to summary
public byte[] asBytes()

Implements abstract com.google.common.hash.HashCode.asBytes.

Doc from com.google.common.hash.HashCode.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 this HashCode object or any other arrays returned by this method.

Annotations
@Override
asIntback to summary
public int asInt()

Implements abstract com.google.common.hash.HashCode.asInt.

Doc from com.google.common.hash.HashCode.asInt.

Returns the first four bytes of this hashcode's bytes, converted to an int value in little-endian order.

Annotations
@Override
asLongback to summary
public long asLong()

Implements abstract com.google.common.hash.HashCode.asLong.

Doc from com.google.common.hash.HashCode.asLong.

Returns the first eight bytes of this hashcode's bytes, converted to a long value in little-endian order.

Annotations
@Override
bitsback to summary
public int bits()

Implements abstract com.google.common.hash.HashCode.bits.

Doc from com.google.common.hash.HashCode.bits.

Returns the number of bits in this hash code; a positive multiple of 8.

Annotations
@Override
equalsSameBitsback to summary
pack-priv boolean equalsSameBits(HashCode that)

Implements abstract com.google.common.hash.HashCode.equalsSameBits.

Doc from com.google.common.hash.HashCode.equalsSameBits.

Returns whether this HashCode and that HashCode have the same value, given that they have the same number of bits.

Annotations
@Override
padToLongback to summary
public long padToLong()

Implements abstract com.google.common.hash.HashCode.padToLong.

Doc from com.google.common.hash.HashCode.padToLong.

If this hashcode has enough bits, returns asLong(), otherwise returns a long value with asBytes() as the least-significant bytes and 0x00 as the remaining most-significant bytes.

Annotations
@Override
writeBytesToImplback to summary
pack-priv void writeBytesToImpl(byte[] dest, int offset, int maxLength)

Implements abstract com.google.common.hash.HashCode.writeBytesToImpl.

Annotations
@Override