Record Class Checksum

java.lang.Object
java.lang.Record
cloud.opencode.base.io.checksum.Checksum
Record Components:
algorithm - the algorithm name | 算法名称
bytes - the checksum bytes | 校验和字节
hex - the hex string | 十六进制字符串

public record Checksum(String algorithm, byte[] bytes, String hex) extends Record
Checksum Result 校验和结果

Immutable record representing a checksum calculation result. Contains algorithm name, raw bytes, and hex string representation.

表示校验和计算结果的不可变记录。 包含算法名称、原始字节和十六进制字符串表示。

Features | 主要功能:

  • Algorithm identification - 算法标识
  • Hex string format - 十六进制字符串格式
  • Byte array access - 字节数组访问

Usage Examples | 使用示例:

Checksum checksum = OpenChecksum.calculateMd5(path);
String hex = checksum.hex();
byte[] bytes = checksum.bytes();

Security | 安全性:

  • Thread-safe: Yes (immutable record with defensive copy) - 线程安全: 是(不可变记录,使用防御性拷贝)
  • Null-safe: No, all fields validated non-null - 空值安全: 否,所有字段验证非null
Since:
JDK 25, opencode-base-io V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Checksum(String algorithm, byte[] bytes)
    Creates a checksum from bytes 从字节创建校验和
    Checksum(String algorithm, byte[] bytes, String hex)
    Compact constructor for validation 紧凑构造器用于验证
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the value of the algorithm record component.
    byte[]
    Gets a copy of the checksum bytes 获取校验和字节的副本
    boolean
    Indicates whether some other object is "equal to" this one.
    static Checksum
    fromHex(String algorithm, String hexString)
    Creates a checksum from hex string 从十六进制字符串创建校验和
    int
    Returns a hash code value for this object.
    hex()
    Returns the value of the hex record component.
    boolean
    Checks if this checksum matches another 检查此校验和是否与另一个匹配
    boolean
    matches(String hexString)
    Checks if this checksum matches a hex string 检查此校验和是否与十六进制字符串匹配
    Returns a string representation of this record class.

    Methods inherited from class Object

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

    • Checksum

      public Checksum(String algorithm, byte[] bytes)
      Creates a checksum from bytes 从字节创建校验和
      Parameters:
      algorithm - the algorithm | 算法
      bytes - the checksum bytes | 校验和字节
    • Checksum

      public Checksum(String algorithm, byte[] bytes, String hex)
      Compact constructor for validation 紧凑构造器用于验证
  • Method Details

    • bytes

      public byte[] bytes()
      Gets a copy of the checksum bytes 获取校验和字节的副本
      Returns:
      byte array copy | 字节数组副本
    • matches

      public boolean matches(Checksum other)
      Checks if this checksum matches another 检查此校验和是否与另一个匹配
      Parameters:
      other - the other checksum | 另一个校验和
      Returns:
      true if match | 如果匹配返回true
    • matches

      public boolean matches(String hexString)
      Checks if this checksum matches a hex string 检查此校验和是否与十六进制字符串匹配
      Parameters:
      hexString - the hex string | 十六进制字符串
      Returns:
      true if match | 如果匹配返回true
    • fromHex

      public static Checksum fromHex(String algorithm, String hexString)
      Creates a checksum from hex string 从十六进制字符串创建校验和
      Parameters:
      algorithm - the algorithm | 算法
      hexString - the hex string | 十六进制字符串
      Returns:
      checksum | 校验和
    • equals

      public boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • hashCode

      public int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • algorithm

      public String algorithm()
      Returns the value of the algorithm record component.
      Returns:
      the value of the algorithm record component
    • hex

      public String hex()
      Returns the value of the hex record component.
      Returns:
      the value of the hex record component