Record Class ETag

java.lang.Object
java.lang.Record
cloud.opencode.base.web.cache.ETag
Record Components:
value - the opaque tag value (without quotes) | 不含引号的标签值
weak - whether this is a weak ETag | 是否为弱 ETag

public record ETag(String value, boolean weak) extends Record
HTTP ETag (Entity Tag) generation and matching HTTP ETag(实体标签)生成与匹配

Provides ETag creation, parsing, and comparison following RFC 7232 semantics, including strong and weak comparison functions.

提供 ETag 创建、解析和比较功能,遵循 RFC 7232 语义, 包括强比较和弱比较函数。

Features | 主要功能:

  • Strong and weak ETag creation - 强/弱 ETag 创建
  • Content-based ETag generation (SHA-256) - 基于内容的 ETag 生成(SHA-256)
  • If-None-Match header matching (multi-value, wildcard) - If-None-Match 头部匹配
  • Strong and weak comparison functions per RFC 7232 - RFC 7232 强/弱比较函数

Usage Examples | 使用示例:

// Create ETags
ETag strong = ETag.strong("abc123");
ETag weak = ETag.weak("abc123");
ETag fromContent = ETag.fromContent("Hello, World!");

// Parse from header
ETag parsed = ETag.parse("W/\"abc123\"");

// Match against If-None-Match
boolean match = etag.matches("\"abc123\", W/\"def456\"");

// Comparison
boolean strongMatch = etag1.strongMatches(etag2);
boolean weakMatch = etag1.weakMatches(etag2);

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Yes (factory methods reject null) - 空值安全: 是(工厂方法拒绝 null)
Since:
JDK 25, opencode-base-web V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    ETag(String value, boolean weak)
    Compact constructor: validates that value is not null or empty.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    static ETag
    fromContent(byte[] content)
    Create a strong ETag from content bytes using SHA-256 digest.
    static ETag
    Create a strong ETag from content string using SHA-256 digest (UTF-8).
    final int
    Returns a hash code value for this object.
    Return the ETag header value (e.g., "abc" or W/"abc").
    boolean
    matches(String ifNoneMatch)
    Check if this ETag matches an If-None-Match header value.
    static ETag
    parse(String headerValue)
    Parse an ETag header value (e.g., "abc" or W/"abc").
    static ETag
    strong(String value)
    Create a strong ETag.
    boolean
    Strong comparison function (RFC 7232 Section 2.3.2).
    Returns the header value representation.
    Returns the value of the value record component.
    boolean
    Returns the value of the weak record component.
    static ETag
    weak(String value)
    Create a weak ETag.
    boolean
    Weak comparison function (RFC 7232 Section 2.3.2).

    Methods inherited from class Object

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

    • ETag

      public ETag(String value, boolean weak)
      Compact constructor: validates that value is not null or empty. 紧凑构造器:验证 value 不为 null 且不为空。
  • Method Details

    • strong

      public static ETag strong(String value)
      Create a strong ETag. 创建强 ETag。
      Parameters:
      value - the tag value | 标签值
      Returns:
      the strong ETag | 强 ETag
    • weak

      public static ETag weak(String value)
      Create a weak ETag. 创建弱 ETag。
      Parameters:
      value - the tag value | 标签值
      Returns:
      the weak ETag | 弱 ETag
    • fromContent

      public static ETag fromContent(byte[] content)
      Create a strong ETag from content bytes using SHA-256 digest. 使用 SHA-256 摘要从内容字节创建强 ETag。
      Parameters:
      content - the content bytes | 内容字节
      Returns:
      the strong ETag with hex digest value | 使用十六进制摘要值的强 ETag
      Throws:
      NullPointerException - if content is null | 如果内容为 null
    • fromContent

      public static ETag fromContent(String content)
      Create a strong ETag from content string using SHA-256 digest (UTF-8). 使用 SHA-256 摘要从内容字符串创建强 ETag(UTF-8)。
      Parameters:
      content - the content string | 内容字符串
      Returns:
      the strong ETag with hex digest value | 使用十六进制摘要值的强 ETag
      Throws:
      NullPointerException - if content is null | 如果内容为 null
    • parse

      public static ETag parse(String headerValue)
      Parse an ETag header value (e.g., "abc" or W/"abc"). 解析 ETag 头部值。
      Parameters:
      headerValue - the header value | 头部值
      Returns:
      the parsed ETag | 解析后的 ETag
      Throws:
      IllegalArgumentException - if the header value is malformed | 如果头部值格式错误
    • headerValue

      public String headerValue()
      Return the ETag header value (e.g., "abc" or W/"abc"). 返回 ETag 头部值。
      Returns:
      the header value | 头部值
    • matches

      public boolean matches(String ifNoneMatch)
      Check if this ETag matches an If-None-Match header value. 检查此 ETag 是否匹配 If-None-Match 头部值。

      Supports wildcard ("*") and comma-separated multi-value headers. Uses weak comparison as defined in RFC 7232 Section 3.2.

      Parameters:
      ifNoneMatch - the If-None-Match header value | If-None-Match 头部值
      Returns:
      true if this ETag matches | 如果匹配返回 true
    • strongMatches

      public boolean strongMatches(ETag other)
      Strong comparison function (RFC 7232 Section 2.3.2). 强比较函数(RFC 7232 第 2.3.2 节)。

      Both ETags must be strong (not weak) and have the same opaque-tag value.

      Parameters:
      other - the other ETag | 另一个 ETag
      Returns:
      true if both are strong and equal | 如果两者都是强且相等返回 true
    • weakMatches

      public boolean weakMatches(ETag other)
      Weak comparison function (RFC 7232 Section 2.3.2). 弱比较函数(RFC 7232 第 2.3.2 节)。

      Only the opaque-tag values need to match; weakness is ignored.

      Parameters:
      other - the other ETag | 另一个 ETag
      Returns:
      true if values are equal (ignoring weakness) | 如果值相等(忽略弱标记)返回 true
    • toString

      public String toString()
      Returns the header value representation. 返回头部值表示。
      Specified by:
      toString in class Record
      Returns:
      the header value | 头部值
    • hashCode

      public final 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
    • equals

      public final 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. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      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.
    • value

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

      public boolean weak()
      Returns the value of the weak record component.
      Returns:
      the value of the weak record component