Class CompositeValidator
- All Implemented Interfaces:
CaptchaValidator
This validator chains multiple CaptchaValidator instances, executing them
in order. If any validator returns a non-success result, the chain short-circuits
and returns that failure immediately. If all validators succeed, the last success
result is returned.
此验证器将多个 CaptchaValidator 实例链式组合,按顺序执行。
如果任何验证器返回非成功结果,链会立即短路并返回该失败结果。
如果所有验证器都成功,则返回最后一个成功结果。
Features | 主要功能:
- Short-circuit evaluation on first failure - 首次失败时短路求值
- Immutable validator list after construction - 构造后验证器列表不可变
- Supports both pre-check validators and primary answer validator - 支持预检验证器和主答案验证器
- Builder pattern for flexible construction - 构建器模式灵活构造
Usage Examples | 使用示例:
// Chain a rate limiter check before the actual answer validator
CaptchaValidator composite = CompositeValidator.of(
CaptchaValidator.simple(store),
rateLimiterValidator
);
ValidationResult result = composite.validate(id, answer);
// Using the builder
CaptchaValidator composite = CompositeValidator.builder()
.addValidator(rateLimiterValidator)
.addValidator(CaptchaValidator.simple(store))
.build();
Performance | 性能特性:
- Time complexity: O(n) where n is the number of validators - 时间复杂度: O(n),n 为验证器数量
- Space complexity: O(1) per validation call - 空间复杂度: 每次验证调用 O(1)
Security | 安全性:
- Thread-safe: Yes if all contained validators are thread-safe - 线程安全: 如果所有包含的验证器都是线程安全的则是
- Null-safe: No (id and answer must not be null) - 空值安全: 否(ID 和答案不能为 null)
- Since:
- JDK 25, opencode-base-captcha V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for CompositeValidator 组合验证器构建器 -
Method Summary
Modifier and TypeMethodDescriptionstatic CompositeValidator.Builderbuilder()Creates a builder for constructing a CompositeValidator.Returns an unmodifiable view of the validators in this composite.static CompositeValidatorof(CaptchaValidator first, CaptchaValidator... rest) Creates a CompositeValidator with the given validators.static CompositeValidatorofList(List<CaptchaValidator> validators) Creates a CompositeValidator from a list of validators.intsize()Returns the number of validators in this composite.Validates a CAPTCHA answer by chaining all validators.Validates a CAPTCHA answer with case sensitivity option by chaining all validators.
-
Method Details
-
of
Creates a CompositeValidator with the given validators. 使用给定的验证器创建组合验证器。The first validator is executed first, followed by the rest in order. Typically the first validator does the actual store lookup and answer check, while subsequent validators perform additional checks (rate limiting, behavior analysis, etc.).
第一个验证器最先执行,其余按顺序执行。通常第一个验证器进行实际的存储查找和答案检查, 后续验证器执行附加检查(速率限制、行为分析等)。
- Parameters:
first- the first validator | 第一个验证器rest- additional validators | 附加验证器- Returns:
- a new CompositeValidator | 新的组合验证器
- Throws:
NullPointerException- if first or any element in rest is null | 如果 first 或 rest 中任何元素为 null
-
ofList
Creates a CompositeValidator from a list of validators. 从验证器列表创建组合验证器。- Parameters:
validators- the validators (must not be null or empty) | 验证器列表(不能为 null 或空)- Returns:
- a new CompositeValidator | 新的组合验证器
- Throws:
NullPointerException- if validators is null or contains null | 如果 validators 为 null 或包含 nullIllegalArgumentException- if validators is empty | 如果 validators 为空
-
builder
Creates a builder for constructing a CompositeValidator. 创建用于构造组合验证器的构建器。- Returns:
- a new builder | 新的构建器
-
validate
Validates a CAPTCHA answer by chaining all validators. 通过链接所有验证器来验证验证码答案。Validators are executed in order. The first non-success result causes immediate return (short-circuit). If all succeed, the last success result is returned.
验证器按顺序执行。第一个非成功结果导致立即返回(短路)。 如果全部成功,则返回最后一个成功结果。
- Specified by:
validatein interfaceCaptchaValidator- Parameters:
id- the CAPTCHA ID | 验证码 IDanswer- the provided answer | 提供的答案- Returns:
- the validation result | 验证结果
-
validate
Validates a CAPTCHA answer with case sensitivity option by chaining all validators. 通过链接所有验证器来验证验证码答案(带大小写敏感选项)。- Specified by:
validatein interfaceCaptchaValidator- Parameters:
id- the CAPTCHA ID | 验证码 IDanswer- the provided answer | 提供的答案caseSensitive- whether case sensitive | 是否区分大小写- Returns:
- the validation result | 验证结果
-
size
public int size()Returns the number of validators in this composite. 返回此组合中的验证器数量。- Returns:
- the validator count | 验证器数量
-
getValidators
Returns an unmodifiable view of the validators in this composite. 返回此组合中验证器的不可修改视图。- Returns:
- the validators | 验证器列表
-