java.lang.Object
com.ultikits.ultitools.abstracts.command.validation.ValidatorChain

public final class ValidatorChain extends Object
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
  • Field Details

    • validators

      private final List<CommandValidator> validators
    • sorted

      private volatile boolean sorted
  • Constructor Details

    • ValidatorChain

      public ValidatorChain()
      Creates an empty validator chain.
    • ValidatorChain

      public ValidatorChain(Collection<CommandValidator> validators)
      Creates a validator chain with the specified validators.
      Parameters:
      validators - the validators to add
  • Method Details

    • addValidator

      public ValidatorChain addValidator(CommandValidator validator)
      Adds a validator to the chain.
      Parameters:
      validator - the validator to add
      Returns:
      this chain for fluent chaining
    • removeValidator

      public ValidatorChain removeValidator(CommandValidator validator)
      Removes a validator from the chain.
      Parameters:
      validator - the validator to remove
      Returns:
      this chain for fluent chaining
    • removeValidators

      public ValidatorChain removeValidators(Class<? extends CommandValidator> validatorClass)
      Removes all validators of the specified type.
      Parameters:
      validatorClass - the validator class to remove
      Returns:
      this chain for fluent chaining
    • clear

      public ValidatorChain clear()
      Clears all validators from the chain.
      Returns:
      this chain for fluent chaining
    • isApplicable

      @Internal public static boolean isApplicable(CommandValidator validator, CommandContext context)
      The single applicability check every caller must use before asking a validator to CommandValidator.validate(com.ultikits.ultitools.abstracts.command.CommandContext). validate(com.ultikits.ultitools.abstracts.command.CommandContext) and validateAll(com.ultikits.ultitools.abstracts.command.CommandContext) both call this (rather than validator.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 check
      context - the command context
      Returns:
      true iff validator should 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

      public List<CommandValidator> getValidators()
      Gets an unmodifiable view of the validators.
      Returns:
      unmodifiable list of validators
    • ensureSorted

      private void ensureSorted()
    • sort

      private void sort()
    • builder

      public static ValidatorChain.Builder builder()
      Creates a new builder for validator chains.
      Returns:
      a new builder