Class AbstractChecker<T,C extends AbstractChecker<T,C>>
java.lang.Object
util.AbstractChecker<T,C>
- Type Parameters:
T
- the type of object to checkC
- 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 Summary
FieldsModifier and TypeFieldDescriptionprotected AbstractChecker
<T, C> The parent checker of the object being checked.protected ExceptionTracker
Tracks exceptions thrown or not thrown during checks.protected String
The name or label for the checked object (for error messages).protected T
The object being checked.protected boolean
If true, errors are saved in the exception tracker instead of being thrown immediately.protected boolean
If true, further checks are stopped (e.g., if the object is null). -
Constructor Summary
ConstructorsConstructorDescriptionAbstractChecker
(String name) Constructor initializing the checker with a name.AbstractChecker
(String name, ExceptionTracker exceptionTracker) Constructor initializing the checker with a name and an existing exception tracker.AbstractChecker
(T object, String name) Constructor initializing the checker with an object and its name.AbstractChecker
(T object, String name, ExceptionTracker exceptionTracker) Constructor initializing the checker with an object, its name, and an exception tracker. -
Method Summary
Modifier and TypeMethodDescriptioncheckProperty
(String propertyPath, Object... args) Caution: Be careful with class types when using this method.Ensures that the methods are invoked using the desired classes.checkProperty
(String propertyPath, Map<String, Object> args) Checks a property or method using a map of named arguments.end()
Ends the current checker and returns the previous checker in the chain.static Object
Retrieves a method result from an object recursively.Returns the object being checked by this checker.static Object
getProperty
(Object object, Queue<String> properties, Object args) Retrieves a nested property or method result using reflection.Checks if any errors are recorded in the exception tracker.Checks if there are no errors recorded.Validates the object with a custom condition using a default message.Validates the object with a custom condition and message.Checks if the object equals another object.Checks if the object is not null.Validates that the condition is NOT true using a default message.Validates that the condition is NOT true.isNull()
Checks if the object is null.Disables saving errors; exceptions will be thrown immediately.Enables saving errors in the exception tracker instead of throwing immediately.protected abstract C
self()
Returns the concrete checker instance (for fluent API).void
show()
Displays all tracked exceptions.void
Displays exceptions that were not thrown.void
Displays exceptions that were thrown.void
stop()
Stops further checks in the current checker.
-
Field Details
-
object
The object being checked. -
name
The name or label for the checked object (for error messages). -
exceptionTracker
Tracks exceptions thrown or not thrown during checks. -
saveErrors
protected boolean saveErrorsIf true, errors are saved in the exception tracker instead of being thrown immediately. -
stop
protected boolean stopIf true, further checks are stopped (e.g., if the object is null). -
backObject
The parent checker of the object being checked.
-
-
Constructor Details
-
AbstractChecker
Constructor initializing the checker with a name.- Parameters:
name
- Name of the object for reporting purposes
-
AbstractChecker
-
AbstractChecker
Constructor initializing the checker with a name and an existing exception tracker.- Parameters:
name
- Name of the objectexceptionTracker
- Tracker for exceptions
-
AbstractChecker
Constructor initializing the checker with an object, its name, and an exception tracker.- Parameters:
object
- Object to checkname
- Name of the objectexceptionTracker
- Tracker for exceptions
-
-
Method Details
-
stop
public void stop()Stops further checks in the current checker. -
self
Returns the concrete checker instance (for fluent API).- Returns:
- The concrete checker instance
-
is
Validates the object with a custom condition and message.- Specified by:
is
in interfaceInterfaceChecker<T,
C extends AbstractChecker<T, C>> - Parameters:
condition
- Condition to validatemessage
- Message to use if the check fails- Returns:
- The current checker instance
-
is
Validates the object with a custom condition using a default message.- Specified by:
is
in interfaceInterfaceChecker<T,
C extends AbstractChecker<T, C>> - Parameters:
condition
- Condition to validate- Returns:
- The current checker instance
-
isNot
Validates that the condition is NOT true.- Specified by:
isNot
in interfaceInterfaceChecker<T,
C extends AbstractChecker<T, C>> - Parameters:
condition
- Condition to negatemessage
- Message to use if the check fails- Returns:
- The current checker instance
-
isNot
Validates that the condition is NOT true using a default message.- Specified by:
isNot
in interfaceInterfaceChecker<T,
C extends AbstractChecker<T, C>> - Parameters:
condition
- Condition to negate- Returns:
- The current checker instance
-
isNull
-
isNonNull
-
isEqual
-
saveErrors
Enables saving errors in the exception tracker instead of throwing immediately.- Returns:
- The current checker instance
-
notSaveErrors
Disables saving errors; exceptions will be thrown immediately.- Returns:
- The current checker instance
-
hasErrors
Checks if any errors are recorded in the exception tracker.- Returns:
- true if errors exist
-
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
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 methodargs
- 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 ExceptionEnsures 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 checkargs
- 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
Checks a property or method using a map of named arguments.- Parameters:
propertyPath
- Path to property or methodargs
- 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 inspectproperties
- Queue of nested property names or method callsargs
- 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 inspectpropertyPath
- Property path queueparams
- Arguments- Returns:
- Method result
- Throws:
Exception
- In case of failure to obtain method
-
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
Returns the object being checked by this checker.- Returns:
- The object under validation
-