Class Fingerprint

java.lang.Object
cloud.opencode.base.hash.simhash.Fingerprint

public final class Fingerprint extends Object
Text fingerprint representation for SimHash SimHash 的文本指纹表示

Represents a SimHash fingerprint value with utility methods for comparing similarity between fingerprints.

表示 SimHash 指纹值,提供比较指纹间相似度的工具方法。

Features | 主要功能:

  • Hamming distance calculation - 海明距离计算
  • Similarity score computation - 相似度分数计算
  • Hex and binary representation - 十六进制和二进制表示

Usage Examples | 使用示例:

Fingerprint fp1 = Fingerprint.of64(simHash.hash(text1));
Fingerprint fp2 = Fingerprint.of64(simHash.hash(text2));

int distance = fp1.hammingDistance(fp2);
double similarity = fp1.similarity(fp2);

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: No - 空值安全: 否
Since:
JDK 25, opencode-base-hash V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Fingerprint(long value, int bits)
    Creates a fingerprint 创建指纹
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Gets the number of bits 获取位数
    boolean
     
    fromHex(String hex, int bits)
    Creates a fingerprint from hexadecimal string 从十六进制字符串创建指纹
    int
    Calculates the Hamming distance to another fingerprint 计算与另一个指纹的海明距离
    int
     
    boolean
    isSimilar(Fingerprint other, int threshold)
    Checks if similar within threshold 检查是否在阈值内相似
    of(long value, int bits)
    Creates a fingerprint with specified bits 创建指定位数的指纹
    of32(int value)
    Creates a 32-bit fingerprint 创建32位指纹
    of64(long value)
    Creates a 64-bit fingerprint 创建64位指纹
    double
    Calculates the similarity to another fingerprint 计算与另一个指纹的相似度
    Converts to binary string 转换为二进制字符串
    Converts to hexadecimal string 转换为十六进制字符串
     
    long
    Gets the fingerprint value 获取指纹值

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Fingerprint

      public Fingerprint(long value, int bits)
      Creates a fingerprint 创建指纹
      Parameters:
      value - fingerprint value | 指纹值
      bits - number of bits | 位数
  • Method Details

    • value

      public long value()
      Gets the fingerprint value 获取指纹值
      Returns:
      fingerprint value | 指纹值
    • bits

      public int bits()
      Gets the number of bits 获取位数
      Returns:
      number of bits | 位数
    • hammingDistance

      public int hammingDistance(Fingerprint other)
      Calculates the Hamming distance to another fingerprint 计算与另一个指纹的海明距离
      Parameters:
      other - other fingerprint | 另一个指纹
      Returns:
      number of different bits | 不同的位数
    • similarity

      public double similarity(Fingerprint other)
      Calculates the similarity to another fingerprint 计算与另一个指纹的相似度
      Parameters:
      other - other fingerprint | 另一个指纹
      Returns:
      similarity (0.0 - 1.0) | 相似度(0.0 - 1.0)
    • isSimilar

      public boolean isSimilar(Fingerprint other, int threshold)
      Checks if similar within threshold 检查是否在阈值内相似
      Parameters:
      other - other fingerprint | 另一个指纹
      threshold - Hamming distance threshold | 海明距离阈值
      Returns:
      true if similar | 如果相似返回true
    • toHex

      public String toHex()
      Converts to hexadecimal string 转换为十六进制字符串
      Returns:
      hexadecimal string | 十六进制字符串
    • toBinary

      public String toBinary()
      Converts to binary string 转换为二进制字符串
      Returns:
      binary string | 二进制字符串
    • of64

      public static Fingerprint of64(long value)
      Creates a 64-bit fingerprint 创建64位指纹
      Parameters:
      value - fingerprint value | 指纹值
      Returns:
      fingerprint | 指纹
    • of32

      public static Fingerprint of32(int value)
      Creates a 32-bit fingerprint 创建32位指纹
      Parameters:
      value - fingerprint value | 指纹值
      Returns:
      fingerprint | 指纹
    • of

      public static Fingerprint of(long value, int bits)
      Creates a fingerprint with specified bits 创建指定位数的指纹
      Parameters:
      value - fingerprint value | 指纹值
      bits - number of bits | 位数
      Returns:
      fingerprint | 指纹
    • fromHex

      public static Fingerprint fromHex(String hex, int bits)
      Creates a fingerprint from hexadecimal string 从十六进制字符串创建指纹
      Parameters:
      hex - hexadecimal string | 十六进制字符串
      bits - number of bits | 位数
      Returns:
      fingerprint | 指纹
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object