Class AbstractChecker<T,C extends AbstractChecker<T,C>>

java.lang.Object
com.luchersol.core.util.AbstractChecker<T,C>
Type Parameters:
T - the type of object to check
C - the concrete checker type (must extend AbstractChecker<T, C>). This ensures the fluent API returns the correct subtype

Example:


 class MyChecker extends AbstractChecker<MyType, MyChecker> {
     // implementation...
 }
 
All Implemented Interfaces:
InterfaceChecker<AbstractChecker<T,C>,T>
Direct Known Subclasses:
Checker, CheckerArray, CheckerBiConsumer, CheckerBiFunction, CheckerBigDecimal, CheckerBigInteger, CheckerBiPredicate, CheckerCallable, CheckerColor, CheckerConsumer, CheckerCurrency, CheckerDate, CheckerDouble, CheckerDuration, CheckerEnum, CheckerFile, CheckerFloat, CheckerFunction, CheckerGraph, CheckerInteger, CheckerJson, CheckerList, CheckerLocalDate, CheckerLocalDateTime, CheckerLocalTime, CheckerLong, CheckerMap, CheckerMatrix, CheckerPeriod, CheckerPolygon, CheckerPredicate, CheckerRunnable, CheckerSet, CheckerString, CheckerSupplier, CheckerTree, CheckerURI

public abstract class AbstractChecker<T,C extends AbstractChecker<T,C>> extends Object implements InterfaceChecker<AbstractChecker<T,C>,T>
Abstract base class for implementing checkers that validate objects of type T. Provides a fluent API for chaining validation methods and tracking exceptions.
  • Field Details

    • object

      protected T object
      The object being checked.
    • name

      protected String name
      The name or label for the checked object (for error messages).
    • exceptionTracker

      protected ExceptionTracker exceptionTracker
      Tracks exceptions thrown or not thrown during checks.
    • saveErrors

      protected boolean saveErrors
      If true, errors are saved in the exception tracker instead of being thrown immediately.
    • stop

      protected boolean stop
      If true, further checks are stopped (e.g., if the object is null).
    • backObject

      protected AbstractChecker<T,C extends AbstractChecker<T,C>> backObject
      The parent checker of the object being checked.
  • Constructor Details

    • AbstractChecker

      public AbstractChecker(String name)
      Constructor initializing the checker with a name.
      Parameters:
      name - Name of the object for reporting purposes
    • AbstractChecker

      public AbstractChecker(T object, String name)
      Constructor initializing the checker with an object and its name.
      Parameters:
      object - Object to check
      name - Name of the object
    • AbstractChecker

      public AbstractChecker(String name, ExceptionTracker exceptionTracker)
      Constructor initializing the checker with a name and an existing exception tracker.
      Parameters:
      name - Name of the object
      exceptionTracker - Tracker for exceptions
    • AbstractChecker

      public AbstractChecker(T object, String name, ExceptionTracker exceptionTracker)
      Constructor initializing the checker with an object, its name, and an exception tracker.
      Parameters:
      object - Object to check
      name - Name of the object
      exceptionTracker - Tracker for exceptions
  • Method Details

    • setObject

      public C setObject(T object)
      Sets the object being checked.
      Parameters:
      object - the object to set
      Returns:
      this checker instance
    • setName

      public C setName(String name)
      Sets the name or label for the checked object.
      Parameters:
      name - the name to set
      Returns:
      this checker instance
    • setExceptionTracker

      public C setExceptionTracker(ExceptionTracker exceptionTracker)
      Sets the exception tracker.
      Parameters:
      exceptionTracker - the tracker to set
      Returns:
      this checker instance
    • setSaveErrors

      public C setSaveErrors(boolean saveErrors)
      Sets whether errors should be saved instead of thrown.
      Parameters:
      saveErrors - true to save errors, false to throw immediately
      Returns:
      this checker instance
    • setStop

      public C setStop(boolean stop)
      Sets whether further checks should stop.
      Parameters:
      stop - true to stop further checks, false otherwise
      Returns:
      this checker instance
    • updateChecker

      public C updateChecker(AbstractChecker<?,?> checker)
      Copies the key state from another checker to this instance.
      Parameters:
      checker - the checker to copy state from
      Returns:
      this checker for fluent chaining
    • setBackObject

      public C setBackObject(AbstractChecker<T,C> backObject)
      Sets the parent checker.
      Parameters:
      backObject - the parent checker to set
      Returns:
      this checker instance
    • stop

      public void stop()
      Stops further checks in the current checker.
    • self

      protected abstract C self()
      Returns the concrete checker instance (for fluent API).
      Returns:
      The concrete checker instance
    • is

      public C is(Predicate<T> condition, Message message)
      Validates the object with a custom condition and message.
      Parameters:
      condition - Condition to validate
      message - Message to use if the check fails
      Returns:
      The current checker instance
    • is

      public C is(Predicate<T> condition, String message)
      Validates the object with a custom condition and message.
      Specified by:
      is in interface InterfaceChecker<T,C extends AbstractChecker<T,C>>
      Parameters:
      condition - Condition to validate
      message - Message to use if the check fails
      Returns:
      The current checker instance
    • is

      public C is(Predicate<T> condition)
      Validates the object with a custom condition using a default message.
      Specified by:
      is in interface InterfaceChecker<T,C extends AbstractChecker<T,C>>
      Parameters:
      condition - Condition to validate
      Returns:
      The current checker instance
    • isNot

      public C isNot(Predicate<T> condition, String message)
      Validates that the condition is NOT true.
      Specified by:
      isNot in interface InterfaceChecker<T,C extends AbstractChecker<T,C>>
      Parameters:
      condition - Condition to negate
      message - Message to use if the check fails
      Returns:
      The current checker instance
    • isNot

      public C isNot(Predicate<T> condition)
      Validates that the condition is NOT true using a default message.
      Specified by:
      isNot in interface InterfaceChecker<T,C extends AbstractChecker<T,C>>
      Parameters:
      condition - Condition to negate
      Returns:
      The current checker instance
    • isNull

      public C isNull()
      Checks if the object is null.
      Returns:
      The current checker instance
    • isNonNull

      public C isNonNull()
      Checks if the object is not null.
      Returns:
      The current checker instance
    • isEqual

      public C isEqual(Object other)
      Checks if the object equals another object.
      Parameters:
      other - Object to compare with
      Returns:
      The current checker instance
    • saveErrors

      public C saveErrors()
      Enables saving errors in the exception tracker instead of throwing immediately.
      Returns:
      The current checker instance
    • notSaveErrors

      public C notSaveErrors()
      Disables saving errors; exceptions will be thrown immediately.
      Returns:
      The current checker instance
    • hasErrors

      public Boolean hasErrors()
      Checks if any errors are recorded in the exception tracker.
      Returns:
      true if errors exist
    • hasNotErrors

      public Boolean hasNotErrors()
      Checks if there are no errors recorded.
      Returns:
      true if no errors exist
    • showThrownException

      public void showThrownException()
      Displays exceptions that were thrown.
    • showPassedChecks

      public void showPassedChecks()
      Displays exceptions that were not thrown.
    • show

      public void show()
      Displays all tracked exceptions.
    • checkProperty

      public <R> Checker<R> checkProperty(Function<? super T,? extends R> extractor, String propertyName)
      Evaluates a property of the current object using the provided extractor function and returns a Checker for further validation.

      The extractor is typically a method reference or lambda expression (e.g. Person::getId) that is applied to the wrapped object. Any exception thrown during extraction is caught and results in null.

      Caution: the extractor is executed eagerly and must be compatible with the runtime type of the wrapped object. Type mismatches or invalid method calls may cause runtime exceptions.

      Type Parameters:
      R - type of the extracted property
      Parameters:
      extractor - function used to extract the property value from the object
      propertyName - logical name of the property, used for error reporting
      Returns:
      a Checker for the extracted property, or null if extraction fails
    • checkProperty

      public Checker<?> checkProperty(String propertyPath, Object... args)
      Caution: Be careful with class types when using this method. For example, if a method expects a Map but the object is internally a HashMap, it may fail. This method checks a nested property using reflection and supports method calls with arguments.
      Parameters:
      propertyPath - Path to the property or method
      args - Arguments to pass to the method
      Returns:
      Checker for the property or method, or null if the property/method is not found
    • checkProperty

      public Checker<?> checkProperty(String propertyPath, List<Map.Entry<Object,Class<?>>> args) throws Exception
      Ensures that the methods are invoked using the desired classes. If not specified, the class of the object obtained via getClass() will be used. This method is useful for checking properties or invoking methods with explicit argument types.
      Parameters:
      propertyPath - Path to the property or method to check
      args - List of argument-value and class pairs
      Returns:
      Checker for the property or method, or null if not found
      Throws:
      Exception - If a reflection-related error occurs
    • checkProperty

      public Checker<?> checkProperty(String propertyPath, Map<String,Object> args) throws Exception
      Checks a property or method using a map of named arguments.
      Parameters:
      propertyPath - Path to property or method
      args - Map of argument names to values
      Returns:
      Checker for the property, or null if not found
      Throws:
      Exception - On reflection failure
    • getProperty

      public static Object getProperty(Object object, Queue<String> properties, Object args) throws Exception
      Retrieves a nested property or method result using reflection.
      Parameters:
      object - Object to inspect
      properties - Queue of nested property names or method calls
      args - Arguments for methods
      Returns:
      Resulting object
      Throws:
      Exception - In case of failure to obtain field or method
    • getMethod

      public static Object getMethod(Object object, Queue<String> propertyPath, Object params) throws Exception
      Retrieves a method result from an object recursively.
      Parameters:
      object - Object to inspect
      propertyPath - Property path queue
      params - Arguments
      Returns:
      Method result
      Throws:
      Exception - In case of failure to obtain method
    • end

      public Checker<?> end()
      Ends the current checker and returns the previous checker in the chain. Merges the current exception tracker into the previous checker's tracker. Useful for nested property checks.
      Returns:
      The previous checker in the chain
    • toChecker

      public Checker<T> toChecker()
      Converts the current checker instance to a Checker of the same type.
      Returns:
      This instance transform to Checker
    • getObject

      public T getObject()
      Returns the object being checked by this checker.
      Returns:
      The object under validation