Class AbstractHashFunction

java.lang.Object
cloud.opencode.base.hash.function.AbstractHashFunction
All Implemented Interfaces:
HashFunction
Direct Known Subclasses:
Adler32HashFunction, Crc32HashFunction, Fnv1aHashFunction, HmacHashFunction, MessageDigestHashFunction, Murmur3HashFunction, SipHashFunction, XxHashFunction

public abstract class AbstractHashFunction extends Object implements HashFunction
Abstract base class for hash functions 哈希函数抽象基类

Provides common implementations for hash function methods, reducing code duplication in concrete implementations.

提供哈希函数方法的通用实现,减少具体实现中的代码重复。

Features | 主要功能:

  • Default method implementations - 默认方法实现
  • AbstractHasher base class - AbstractHasher基类
  • Common utility methods - 通用工具方法

Usage Examples | 使用示例:

// Extend to create custom hash functions
// 扩展以创建自定义哈希函数
public class MyHash extends AbstractHashFunction {
    @Override
    public HashCode hash(byte[] data) {
        // custom hashing logic
    }
}

Security | 安全性:

  • Thread-safe: Implementation-dependent - 线程安全: 取决于实现
  • Null-safe: No - 空值安全: 否

Performance | 性能特性:

  • Time complexity: O(n) where n = input size - O(n), n为输入大小
  • Space complexity: O(1) for hash state - 哈希状态 O(1)
Since:
JDK 25, opencode-base-hash V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

    • bits

      protected final int bits
      Hash function bits
    • name

      protected final String name
      Algorithm name
  • Constructor Details

    • AbstractHashFunction

      protected AbstractHashFunction(int bits, String name)
      Creates an abstract hash function 创建抽象哈希函数
      Parameters:
      bits - hash output bits | 哈希输出位数
      name - algorithm name | 算法名称
  • Method Details

    • bits

      public int bits()
      Description copied from interface: HashFunction
      Gets the number of bits in the hash output 获取哈希输出的位数
      Specified by:
      bits in interface HashFunction
      Returns:
      number of bits | 位数
    • name

      public String name()
      Description copied from interface: HashFunction
      Gets the algorithm name 获取算法名称
      Specified by:
      name in interface HashFunction
      Returns:
      algorithm name | 算法名称
    • newHasher

      public Hasher newHasher(int expectedInputSize)
      Description copied from interface: HashFunction
      Creates a Hasher with expected input size hint 创建带预期输入大小提示的Hasher
      Specified by:
      newHasher in interface HashFunction
      Parameters:
      expectedInputSize - expected input size in bytes | 预期输入大小(字节)
      Returns:
      a new Hasher | 新的Hasher
    • hashBytes

      public HashCode hashBytes(byte[] input)
      Description copied from interface: HashFunction
      Computes hash of a byte array 计算字节数组的哈希
      Specified by:
      hashBytes in interface HashFunction
      Parameters:
      input - input bytes | 输入字节
      Returns:
      hash code | 哈希码
    • hashInt

      public HashCode hashInt(int input)
      Description copied from interface: HashFunction
      Computes hash of an int value 计算int值的哈希
      Specified by:
      hashInt in interface HashFunction
      Parameters:
      input - int value | int值
      Returns:
      hash code | 哈希码
    • hashLong

      public HashCode hashLong(long input)
      Description copied from interface: HashFunction
      Computes hash of a long value 计算long值的哈希
      Specified by:
      hashLong in interface HashFunction
      Parameters:
      input - long value | long值
      Returns:
      hash code | 哈希码
    • hashObject

      public <T> HashCode hashObject(T instance, Funnel<? super T> funnel)
      Description copied from interface: HashFunction
      Computes hash of an object using a Funnel 使用Funnel计算对象的哈希
      Specified by:
      hashObject in interface HashFunction
      Type Parameters:
      T - object type | 对象类型
      Parameters:
      instance - object instance | 对象实例
      funnel - serialization funnel | 序列化通道
      Returns:
      hash code | 哈希码
    • toString

      public String toString()
      Overrides:
      toString in class Object