Record Class Captcha
java.lang.Object
java.lang.Record
cloud.opencode.base.captcha.Captcha
- Record Components:
id- the unique identifier | 唯一标识符type- the CAPTCHA type | 验证码类型imageData- the image data as bytes | 图像数据(字节数组)answer- the correct answer | 正确答案metadata- additional metadata | 附加元数据createdAt- the creation timestamp | 创建时间戳expiresAt- the expiration timestamp | 过期时间戳
public record Captcha(String id, CaptchaType type, byte[] imageData, String answer, Map<String,Object> metadata, Instant createdAt, Instant expiresAt)
extends Record
Captcha - CAPTCHA data container
验证码 - 验证码数据容器
This record holds the generated CAPTCHA data including the image, answer, and metadata.
此记录保存生成的验证码数据,包括图像、答案和元数据。
Features | 主要功能:
- Immutable CAPTCHA data record - 不可变验证码数据记录
- Base64 and data URL encoding - Base64 和数据 URL 编码
- Expiration tracking - 过期跟踪
- Metadata support for extended attributes - 元数据支持扩展属性
Usage Examples | 使用示例:
Captcha captcha = OpenCaptcha.create();
String id = captcha.id();
String base64Image = captcha.toBase64();
Security | 安全性:
- Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
- Null-safe: Yes (null imageData/metadata normalized on construction) - 空值安全: 是(构造时对 null imageData/metadata 做归一化处理)
- Since:
- JDK 25, opencode-base-captcha V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionanswer()Returns the value of theanswerrecord component.Returns the value of thecreatedAtrecord component.final booleanIndicates whether some other object is "equal to" this one.Returns the value of theexpiresAtrecord component.intGets the image height from metadata.<T> TgetMetadata(String key) Gets a metadata value.Gets the MIME type based on CAPTCHA type.intgetWidth()Gets the image width from metadata.final inthashCode()Returns a hash code value for this object.id()Returns the value of theidrecord component.byte[]Returns the value of theimageDatarecord component.booleanChecks if this CAPTCHA has expired.metadata()Returns the value of themetadatarecord component.toBase64()Converts the image data to Base64 string.Converts the image data to Base64 data URL.toString()Returns a string representation that redacts the answer to prevent accidental exposure in logs.type()Returns the value of thetyperecord component.
-
Constructor Details
-
Captcha
public Captcha(String id, CaptchaType type, byte[] imageData, String answer, Map<String, Object> metadata, Instant createdAt, Instant expiresAt) Compact constructor that makes defensive copies to ensure immutability. 紧凑构造器,通过防御性复制确保不可变性。imageDatais cloned andmetadatais wrapped viaMap.copyOf(Map)so that external mutation cannot affect this record.imageData会被克隆,metadata会通过Map.copyOf(Map)包装,使得外部修改不会影响此记录。
-
-
Method Details
-
toBase64
Converts the image data to Base64 string. 将图像数据转换为 Base64 字符串。- Returns:
- the Base64 encoded image | Base64 编码的图像
-
toBase64DataUrl
Converts the image data to Base64 data URL. 将图像数据转换为 Base64 数据 URL。- Returns:
- the Base64 data URL | Base64 数据 URL
-
getMimeType
Gets the MIME type based on CAPTCHA type. 根据验证码类型获取 MIME 类型。- Returns:
- the MIME type | MIME 类型
-
isExpired
public boolean isExpired()Checks if this CAPTCHA has expired. 检查此验证码是否已过期。- Returns:
- true if expired | 如果已过期则返回 true
-
getMetadata
Gets a metadata value. 获取元数据值。- Type Parameters:
T- the value type | 值类型- Parameters:
key- the metadata key | 元数据键- Returns:
- the metadata value or null | 元数据值或 null
-
getWidth
public int getWidth()Gets the image width from metadata. 从元数据获取图像宽度。- Returns:
- the width | 宽度
-
getHeight
public int getHeight()Gets the image height from metadata. 从元数据获取图像高度。- Returns:
- the height | 高度
-
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. All components in this record class are compared withObjects::equals(Object,Object). -
id
-
type
-
imageData
-
answer
-
metadata
-
createdAt
-
expiresAt
-