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

java.lang.Object
util.AbstractChecker<T,C>
Type Parameters:
T - the type of object to check
C - the type of the concrete checker (for fluent API)
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, 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

    • 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, 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.
    • showNotThrownException

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

      public void show()
      Displays all tracked exceptions.
    • 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
    • getObject

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