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
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 -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.static ETagfromContent(byte[] content) Create a strong ETag from content bytes using SHA-256 digest.static ETagfromContent(String content) Create a strong ETag from content string using SHA-256 digest (UTF-8).final inthashCode()Returns a hash code value for this object.Return the ETag header value (e.g.,"abc"orW/"abc").booleanCheck if this ETag matches an If-None-Match header value.static ETagParse an ETag header value (e.g.,"abc"orW/"abc").static ETagCreate a strong ETag.booleanstrongMatches(ETag other) Strong comparison function (RFC 7232 Section 2.3.2).toString()Returns the header value representation.value()Returns the value of thevaluerecord component.booleanweak()Returns the value of theweakrecord component.static ETagCreate a weak ETag.booleanweakMatches(ETag other) Weak comparison function (RFC 7232 Section 2.3.2).
-
Constructor Details
-
ETag
Compact constructor: validates that value is not null or empty. 紧凑构造器:验证 value 不为 null 且不为空。
-
-
Method Details
-
strong
-
weak
-
fromContent
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
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
Parse an ETag header value (e.g.,"abc"orW/"abc"). 解析 ETag 头部值。- Parameters:
headerValue- the header value | 头部值- Returns:
- the parsed ETag | 解析后的 ETag
- Throws:
IllegalArgumentException- if the header value is malformed | 如果头部值格式错误
-
headerValue
Return the ETag header value (e.g.,"abc"orW/"abc"). 返回 ETag 头部值。- Returns:
- the header value | 头部值
-
matches
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
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
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
-
hashCode
-
equals
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 withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
value
-
weak
-