Class ValidatorChain
java.lang.Object
com.ultikits.ultitools.abstracts.command.validation.ValidatorChain
Manages a chain of validators that are executed in order.
Thread-safe implementation supporting dynamic validator registration.
- Since:
- 6.2.0
- Version:
- 2.0.0
- Author:
- wisdomme
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for creating validator chains.static final classResult of chain validation. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty validator chain.ValidatorChain(Collection<CommandValidator> validators) Creates a validator chain with the specified validators. -
Method Summary
Modifier and TypeMethodDescriptionaddValidator(CommandValidator validator) Adds a validator to the chain.static ValidatorChain.Builderbuilder()Creates a new builder for validator chains.clear()Clears all validators from the chain.private voidGets an unmodifiable view of the validators.static booleanisApplicable(CommandValidator validator, CommandContext context) The single applicability check every caller must use before asking a validator toCommandValidator.validate(com.ultikits.ultitools.abstracts.command.CommandContext).booleanisEmpty()Checks if the chain is empty.removeValidator(CommandValidator validator) Removes a validator from the chain.removeValidators(Class<? extends CommandValidator> validatorClass) Removes all validators of the specified type.intsize()Gets the number of validators in the chain.private voidsort()validate(CommandContext context) Validates the context through all validators in the chain.validateAll(CommandContext context) Validates the context through all validators, collecting all failures.
-
Field Details
-
validators
-
sorted
private volatile boolean sorted
-
-
Constructor Details
-
ValidatorChain
public ValidatorChain()Creates an empty validator chain. -
ValidatorChain
Creates a validator chain with the specified validators.- Parameters:
validators- the validators to add
-
-
Method Details
-
addValidator
Adds a validator to the chain.- Parameters:
validator- the validator to add- Returns:
- this chain for fluent chaining
-
removeValidator
Removes a validator from the chain.- Parameters:
validator- the validator to remove- Returns:
- this chain for fluent chaining
-
removeValidators
Removes all validators of the specified type.- Parameters:
validatorClass- the validator class to remove- Returns:
- this chain for fluent chaining
-
clear
Clears all validators from the chain.- Returns:
- this chain for fluent chaining
-
isApplicable
The single applicability check every caller must use before asking a validator toCommandValidator.validate(com.ultikits.ultitools.abstracts.command.CommandContext).validate(com.ultikits.ultitools.abstracts.command.CommandContext)andvalidateAll(com.ultikits.ultitools.abstracts.command.CommandContext)both call this (rather thanvalidator.shouldValidate(context)directly) so there is exactly one place this decision lives; a caller OUTSIDE this class that needs the same decision --BaseCommandExecutor's gated help path, which invokes select validators directly without running the full chain -- calls this too, instead of re-declaring the same one-line condition a second time (WR-02, #413). This method is framework-internal and is not a supported extension point for module authors.- Parameters:
validator- the validator to checkcontext- the command context- Returns:
trueiffvalidatorshould be asked to validate this context- Since:
- 6.3.0
-
validate
Validates the context through all validators in the chain. Stops at the first failure.- Parameters:
context- the command context to validate- Returns:
- the result of the validation
-
validateAll
Validates the context through all validators, collecting all failures. Does not stop at the first failure.- Parameters:
context- the command context to validate- Returns:
- the result containing all validation results
-
size
public int size()Gets the number of validators in the chain.- Returns:
- the number of validators
-
isEmpty
public boolean isEmpty()Checks if the chain is empty.- Returns:
- true if empty
-
getValidators
Gets an unmodifiable view of the validators.- Returns:
- unmodifiable list of validators
-
ensureSorted
private void ensureSorted() -
sort
private void sort() -
builder
Creates a new builder for validator chains.- Returns:
- a new builder
-