Class AbstractNonStreamingHashFunction

java.lang.Object
com.google.common.hash.AbstractHashFunction
com.google.common.hash.AbstractNonStreamingHashFunction
All Implemented Interfaces:
HashFunction
Direct Known Subclasses:
FarmHashFingerprint64, Fingerprint2011

abstract class AbstractNonStreamingHashFunction extends AbstractHashFunction
Skeleton implementation of HashFunction, appropriate for non-streaming algorithms. All the hash computation done using newHasher() are delegated to the hashBytes(byte[], int, int) method.
  • Constructor Details

    • AbstractNonStreamingHashFunction

      AbstractNonStreamingHashFunction()
  • Method Details

    • newHasher

      public Hasher newHasher()
      Description copied from interface: HashFunction
      Begins a new hash code computation by returning an initialized, stateful Hasher instance that is ready to receive data. Example:
      
       HashFunction hf = Hashing.md5();
       HashCode hc = hf.newHasher()
           .putLong(id)
           .putBoolean(isActive)
           .hash();
       
    • newHasher

      public Hasher newHasher(int expectedInputSize)
      Description copied from interface: HashFunction
      Begins a new hash code computation as HashFunction.newHasher(), but provides a hint of the expected size of the input (in bytes). This is only important for non-streaming hash functions (hash functions that need to buffer their whole input before processing any of it).
      Specified by:
      newHasher in interface HashFunction
      Overrides:
      newHasher in class AbstractHashFunction
    • hashInt

      public HashCode hashInt(int input)
      Description copied from interface: HashFunction
      Shortcut for newHasher().putInt(input).hash(); returns the hash code for the given int value, interpreted in little-endian byte order. The implementation might perform better than its longhand equivalent, but should not perform worse.
      Specified by:
      hashInt in interface HashFunction
      Overrides:
      hashInt in class AbstractHashFunction
    • hashLong

      public HashCode hashLong(long input)
      Description copied from interface: HashFunction
      Shortcut for newHasher().putLong(input).hash(); returns the hash code for the given long value, interpreted in little-endian byte order. The implementation might perform better than its longhand equivalent, but should not perform worse.
      Specified by:
      hashLong in interface HashFunction
      Overrides:
      hashLong in class AbstractHashFunction
    • hashUnencodedChars

      public HashCode hashUnencodedChars(CharSequence input)
      Description copied from interface: HashFunction
      Shortcut for newHasher().putUnencodedChars(input).hash(). The implementation might perform better than its longhand equivalent, but should not perform worse. Note that no character encoding is performed; the low byte and high byte of each char are hashed directly (in that order).

      Warning: This method will produce different output than most other languages do when running the same hash function on the equivalent input. For cross-language compatibility, use HashFunction.hashString(java.lang.CharSequence, java.nio.charset.Charset), usually with a charset of UTF-8. For other use cases, use hashUnencodedChars.

      Specified by:
      hashUnencodedChars in interface HashFunction
      Overrides:
      hashUnencodedChars in class AbstractHashFunction
    • hashString

      public HashCode hashString(CharSequence input, Charset charset)
      Description copied from interface: HashFunction
      Shortcut for newHasher().putString(input, charset).hash(). Characters are encoded using the given Charset. The implementation might perform better than its longhand equivalent, but should not perform worse.

      Warning: This method, which reencodes the input before hashing it, is useful only for cross-language compatibility. For other use cases, prefer HashFunction.hashUnencodedChars(java.lang.CharSequence), which is faster, produces the same output across Java releases, and hashes every char in the input, even if some are invalid.

      Specified by:
      hashString in interface HashFunction
      Overrides:
      hashString in class AbstractHashFunction
    • hashBytes

      public abstract HashCode hashBytes(byte[] input, int off, int len)
      Description copied from interface: HashFunction
      Shortcut for newHasher().putBytes(input, off, len).hash(). The implementation might perform better than its longhand equivalent, but should not perform worse.
      Specified by:
      hashBytes in interface HashFunction
      Overrides:
      hashBytes in class AbstractHashFunction
    • hashBytes

      public HashCode hashBytes(ByteBuffer input)
      Description copied from interface: HashFunction
      Shortcut for newHasher().putBytes(input).hash(). The implementation might perform better than its longhand equivalent, but should not perform worse.
      Specified by:
      hashBytes in interface HashFunction
      Overrides:
      hashBytes in class AbstractHashFunction