Interface Hasher

All Known Implementing Classes:
AbstractHashFunction.AbstractHasher, AbstractHashFunction.BufferedHasher

public interface Hasher
Streaming hash calculator interface 流式哈希计算器接口

Provides a fluent API for incrementally building hash values by adding data piece by piece. This is useful for hashing large data or combining multiple values into a single hash.

提供流畅的API,通过逐块添加数据来增量构建哈希值。 这对于哈希大数据或将多个值组合成单个哈希非常有用。

Features | 主要功能:

  • Fluent method chaining - 流畅的方法链
  • Multiple data type support - 多种数据类型支持
  • Object hashing via Funnel - 通过Funnel的对象哈希

Usage Examples | 使用示例:

Hasher hasher = OpenHash.murmur3_128().newHasher();
HashCode hash = hasher
    .putUtf8("Hello")
    .putInt(42)
    .putLong(System.currentTimeMillis())
    .hash();

Security | 安全性:

  • Thread-safe: No (stateful) - 线程安全: 否(有状态)
Since:
JDK 25, opencode-base-hash V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Computes the hash value 计算哈希值
    putBoolean(boolean b)
    Adds a boolean value to the hash computation 向哈希计算添加boolean值
    putByte(byte b)
    Adds a byte to the hash computation 向哈希计算添加一个字节
    putBytes(byte[] bytes)
    Adds a byte array to the hash computation 向哈希计算添加字节数组
    putBytes(byte[] bytes, int offset, int length)
    Adds a portion of a byte array to the hash computation 向哈希计算添加字节数组的一部分
    Adds a ByteBuffer to the hash computation 向哈希计算添加ByteBuffer
    putChar(char c)
    Adds a char value to the hash computation 向哈希计算添加char值
    putDouble(double d)
    Adds a double value to the hash computation 向哈希计算添加double值
    putFloat(float f)
    Adds a float value to the hash computation 向哈希计算添加float值
    putInt(int i)
    Adds an int value to the hash computation 向哈希计算添加int值
    putLong(long l)
    Adds a long value to the hash computation 向哈希计算添加long值
    <T> Hasher
    putObject(T instance, Funnel<? super T> funnel)
    Adds an object using a Funnel to the hash computation 使用Funnel向哈希计算添加对象
    putShort(short s)
    Adds a short value to the hash computation 向哈希计算添加short值
    putString(CharSequence charSequence, Charset charset)
    Adds a string with specified charset to the hash computation 向哈希计算添加指定字符集的字符串
    default Hasher
    putUtf8(CharSequence charSequence)
    Adds a UTF-8 string to the hash computation 向哈希计算添加UTF-8字符串
  • Method Details

    • putByte

      Hasher putByte(byte b)
      Adds a byte to the hash computation 向哈希计算添加一个字节
      Parameters:
      b - byte value | 字节值
      Returns:
      this hasher | 此hasher
    • putBytes

      Hasher putBytes(byte[] bytes)
      Adds a byte array to the hash computation 向哈希计算添加字节数组
      Parameters:
      bytes - byte array | 字节数组
      Returns:
      this hasher | 此hasher
    • putBytes

      Hasher putBytes(byte[] bytes, int offset, int length)
      Adds a portion of a byte array to the hash computation 向哈希计算添加字节数组的一部分
      Parameters:
      bytes - byte array | 字节数组
      offset - starting offset | 起始偏移
      length - number of bytes | 字节数
      Returns:
      this hasher | 此hasher
    • putBytes

      Hasher putBytes(ByteBuffer buffer)
      Adds a ByteBuffer to the hash computation 向哈希计算添加ByteBuffer
      Parameters:
      buffer - byte buffer | 字节缓冲区
      Returns:
      this hasher | 此hasher
    • putShort

      Hasher putShort(short s)
      Adds a short value to the hash computation 向哈希计算添加short值
      Parameters:
      s - short value | short值
      Returns:
      this hasher | 此hasher
    • putInt

      Hasher putInt(int i)
      Adds an int value to the hash computation 向哈希计算添加int值
      Parameters:
      i - int value | int值
      Returns:
      this hasher | 此hasher
    • putLong

      Hasher putLong(long l)
      Adds a long value to the hash computation 向哈希计算添加long值
      Parameters:
      l - long value | long值
      Returns:
      this hasher | 此hasher
    • putFloat

      Hasher putFloat(float f)
      Adds a float value to the hash computation 向哈希计算添加float值
      Parameters:
      f - float value | float值
      Returns:
      this hasher | 此hasher
    • putDouble

      Hasher putDouble(double d)
      Adds a double value to the hash computation 向哈希计算添加double值
      Parameters:
      d - double value | double值
      Returns:
      this hasher | 此hasher
    • putBoolean

      Hasher putBoolean(boolean b)
      Adds a boolean value to the hash computation 向哈希计算添加boolean值
      Parameters:
      b - boolean value | boolean值
      Returns:
      this hasher | 此hasher
    • putChar

      Hasher putChar(char c)
      Adds a char value to the hash computation 向哈希计算添加char值
      Parameters:
      c - char value | char值
      Returns:
      this hasher | 此hasher
    • putString

      Hasher putString(CharSequence charSequence, Charset charset)
      Adds a string with specified charset to the hash computation 向哈希计算添加指定字符集的字符串
      Parameters:
      charSequence - string | 字符串
      charset - character set | 字符集
      Returns:
      this hasher | 此hasher
    • putUtf8

      default Hasher putUtf8(CharSequence charSequence)
      Adds a UTF-8 string to the hash computation 向哈希计算添加UTF-8字符串
      Parameters:
      charSequence - string | 字符串
      Returns:
      this hasher | 此hasher
    • putObject

      <T> Hasher putObject(T instance, Funnel<? super T> funnel)
      Adds an object using a Funnel to the hash computation 使用Funnel向哈希计算添加对象
      Type Parameters:
      T - object type | 对象类型
      Parameters:
      instance - object instance | 对象实例
      funnel - serialization funnel | 序列化通道
      Returns:
      this hasher | 此hasher
    • hash

      HashCode hash()
      Computes the hash value 计算哈希值

      This method can only be called once. After calling hash(), this Hasher should not be used again.

      此方法只能调用一次。调用hash()后,此Hasher不应再使用。

      Returns:
      computed hash code | 计算的哈希码