Class ValidatorChain
- 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.static voidnotifyRefused(CommandContext context, List<CommandValidator> passedValidators) CallsCommandValidator.onRefused(CommandContext)on each ofpassedValidators, in order, for a dispatch refused after they passed and before the mapped method ran (#568).private static RuntimeExceptionnotifyRefused(CommandContext context, List<CommandValidator> passedValidators, RuntimeException pending) Runs every refusal hook; returns the first exception a hook threw, or attaches it topending(an exception already on its way out) and returnsnull.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.A runtime exception thrown from a validator's
CommandValidator.shouldValidate(com.ultikits.ultitools.abstracts.command.CommandContext)orCommandValidator.validate(com.ultikits.ultitools.abstracts.command.CommandContext)refuses the dispatch: every validator that already passed receivesCommandValidator.onRefused(com.ultikits.ultitools.abstracts.command.CommandContext)once, in chain order, and then the original exception propagates unchanged, with any exception a refusal hook threw attached to it as suppressed. The throwing validator itself, and every validator after it, receives nothing (#568 forvalidate; case 49 forshouldValidate).- Parameters:
context- the command context to validate- Returns:
- the result of the validation
-
notifyRefused
@Internal public static void notifyRefused(CommandContext context, List<CommandValidator> passedValidators) CallsCommandValidator.onRefused(CommandContext)on each ofpassedValidators, in order, for a dispatch refused after they passed and before the mapped method ran (#568). Every hook runs even when an earlier one throws, so one validator's failing hook cannot keep a later validator -- aUsageLockValidator-- from releasing what it holds; the first exception a hook threw is rethrown afterwards, with any later ones suppressed on it.- Parameters:
context- the command contextpassedValidators- the validators that passed for this dispatch, in chain order; may benull- Since:
- 6.3.0
-
notifyRefused
private static RuntimeException notifyRefused(CommandContext context, List<CommandValidator> passedValidators, RuntimeException pending) Runs every refusal hook; returns the first exception a hook threw, or attaches it topending(an exception already on its way out) and returnsnull. -
validateAll
Validates the context through all validators, collecting all failures. Does not stop at the first failure.A returned result, valid or not, calls no refusal hook: the caller owns refusal for it. A runtime exception thrown from a validator's
CommandValidator.shouldValidate(com.ultikits.ultitools.abstracts.command.CommandContext)orCommandValidator.validate(com.ultikits.ultitools.abstracts.command.CommandContext)is handled as invalidate(com.ultikits.ultitools.abstracts.command.CommandContext): every validator that already passed receivesCommandValidator.onRefused(com.ultikits.ultitools.abstracts.command.CommandContext)once, in chain order, and then the original exception propagates unchanged, with any exception a refusal hook threw attached to it as suppressed (case 49).- 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
-