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 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. 紧凑构造器,通过防御性复制确保不可变性。

      imageData is cloned and metadata is wrapped via Map.copyOf(Map) so that external mutation cannot affect this record.

      imageData 会被克隆,metadata 会通过 Map.copyOf(Map) 包装,使得外部修改不会影响此记录。

  • Method Details

    • toBase64

      public String toBase64()
      Converts the image data to Base64 string. 将图像数据转换为 Base64 字符串。
      Returns:
      the Base64 encoded image | Base64 编码的图像
    • toBase64DataUrl

      public String toBase64DataUrl()
      Converts the image data to Base64 data URL. 将图像数据转换为 Base64 数据 URL。
      Returns:
      the Base64 data URL | Base64 数据 URL
    • getMimeType

      public String 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

      public <T> T getMetadata(String key)
      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

      public String toString()
      Returns a string representation that redacts the answer to prevent accidental exposure in logs. 返回一个字符串表示,其中答案被脱敏处理,防止在日志中意外暴露。
      Specified by:
      toString in class Record
      Returns:
      a redacted string representation | 脱敏后的字符串表示
    • 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. 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.
    • id

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

      public CaptchaType type()
      Returns the value of the type record component.
      Returns:
      the value of the type record component
    • imageData

      public byte[] imageData()
      Returns the value of the imageData record component.
      Returns:
      the value of the imageData record component
    • answer

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

      public Map<String,Object> metadata()
      Returns the value of the metadata record component.
      Returns:
      the value of the metadata record component
    • createdAt

      public Instant createdAt()
      Returns the value of the createdAt record component.
      Returns:
      the value of the createdAt record component
    • expiresAt

      public Instant expiresAt()
      Returns the value of the expiresAt record component.
      Returns:
      the value of the expiresAt record component