Class HashCode
- java.lang.Object
- 
- com.exonum.binding.common.hash.HashCode
 
- 
 public abstract class HashCode extends Object An immutable hash code of arbitrary bit length.- Since:
- 11.0
- Author:
- Dimitris Andreou, Kurt Alfred Kluever
 
- 
- 
Method SummaryModifier and Type Method Description abstract byte[]asBytes()Returns the value of this hash code as a byte array.abstract intasInt()Returns the first four bytes of this hashcode's bytes, converted to anintvalue in little-endian order.abstract longasLong()Returns the first eight bytes of this hashcode's bytes, converted to alongvalue in little-endian order.abstract intbits()Returns the number of bits in this hash code; a positive multiple of 8.booleanequals(Object object)Returnstrueifobjectis aHashCodeinstance with the identical byte representation to this hash code.static HashCodefromBytes(byte[] bytes)Creates aHashCodefrom a byte array.static HashCodefromInt(int hash)Creates a 32-bitHashCoderepresentation of the given int value.static HashCodefromLong(long hash)Creates a 64-bitHashCoderepresentation of the given long value.static HashCodefromString(String string)Creates aHashCodefrom a hexadecimal (base 16) encoded string.inthashCode()Returns a "Java hash code" for thisHashCodeinstance; this is well-defined (so, for example, you can safely putHashCodeinstances into aHashSet) but is otherwise probably not what you want to use.abstract longpadToLong()If this hashcode has enough bits, returnsasLong(), otherwise returns alongvalue withasBytes()as the least-significant bytes and0x00as the remaining most-significant bytes.StringtoString()Returns a string containing each byte ofasBytes(), in order, as a two-digit unsigned hexadecimal number in lower case.intwriteBytesTo(byte[] dest, int offset, int maxLength)Copies bytes from this hash code intodest.
 
- 
- 
- 
Method Detail- 
bitspublic abstract int bits() Returns the number of bits in this hash code; a positive multiple of 8.
 - 
asIntpublic abstract int asInt() Returns the first four bytes of this hashcode's bytes, converted to anintvalue in little-endian order.- Throws:
- IllegalStateException- if- bits() < 32
 
 - 
asLongpublic abstract long asLong() Returns the first eight bytes of this hashcode's bytes, converted to alongvalue in little-endian order.- Throws:
- IllegalStateException- if- bits() < 64
 
 - 
padToLongpublic abstract long padToLong() If this hashcode has enough bits, returnsasLong(), otherwise returns alongvalue withasBytes()as the least-significant bytes and0x00as the remaining most-significant bytes.- Since:
- 14.0 (since 11.0 as Hashing.padToLong(HashCode))
 
 - 
asBytespublic 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 thisHashCodeobject or any other arrays returned by this method.
 - 
writeBytesTo@CanIgnoreReturnValue 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 written
- offset- the start offset in the data
- maxLength- the maximum number of bytes to write
- Returns:
- the number of bytes written to dest
- Throws:
- IndexOutOfBoundsException- if there is not enough room in- dest
 
 - 
fromIntpublic static HashCode fromInt(int hash) Creates a 32-bitHashCoderepresentation of the given int value. The underlying bytes are interpreted in little endian order.- Since:
- 15.0 (since 12.0 in HashCodes)
 
 - 
fromLongpublic static HashCode fromLong(long hash) Creates a 64-bitHashCoderepresentation of the given long value. The underlying bytes are interpreted in little endian order.- Since:
- 15.0 (since 12.0 in HashCodes)
 
 - 
fromBytespublic static HashCode fromBytes(byte[] bytes) Creates aHashCodefrom 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)
 
 - 
fromStringpublic static HashCode fromString(String string) Creates aHashCodefrom 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 16decoding, please useBaseEncoding.decode(java.lang.CharSequence)(and pass the result tofromBytes(byte[])).- Since:
- 15.0
 
 - 
equalspublic final boolean equals(@Nullable Object object) Returnstrueifobjectis aHashCodeinstance 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. 
 - 
hashCodepublic final int hashCode() Returns a "Java hash code" for thisHashCodeinstance; this is well-defined (so, for example, you can safely putHashCodeinstances into aHashSet) but is otherwise probably not what you want to use.
 - 
toStringpublic final String 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, this hash code's bytes are the big-endian representation of that number. This may be surprising since everything else in the hashing API uniformly treats multibyte values as little-endian. But this format conveniently matches that of utilities such as the UNIX md5sumcommand.To create a HashCodefrom its string representation, seefromString(java.lang.String).
 
- 
 
-