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 checkC- the concrete checker type (must extendAbstractChecker<T, C>). This ensures the fluent API returns the correct subtypeExample:
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 Summary
FieldsModifier and TypeFieldDescriptionprotected AbstractChecker<T, C> The parent checker of the object being checked.protected ExceptionTrackerTracks exceptions thrown or not thrown during checks.protected StringThe name or label for the checked object (for error messages).protected TThe object being checked.protected booleanIf true, errors are saved in the exception tracker instead of being thrown immediately.protected booleanIf 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 TypeMethodDescriptionChecker<?> checkProperty(String propertyPath, Object... args) Caution: Be careful with class types when using this method.Checker<?> Ensures that the methods are invoked using the desired classes.Checker<?> checkProperty(String propertyPath, Map<String, Object> args) Checks a property or method using a map of named arguments.<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 aCheckerfor further validation.Checker<?> end()Ends the current checker and returns the previous checker in the chain.static ObjectRetrieves a method result from an object recursively.Returns the object being checked by this checker.static ObjectgetProperty(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.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 Cself()Returns the concrete checker instance (for fluent API).setBackObject(AbstractChecker<T, C> backObject) Sets the parent checker.setExceptionTracker(ExceptionTracker exceptionTracker) Sets the exception tracker.Sets the name or label for the checked object.Sets the object being checked.setSaveErrors(boolean saveErrors) Sets whether errors should be saved instead of thrown.setStop(boolean stop) Sets whether further checks should stop.voidshow()Displays all tracked exceptions.voidDisplays exceptions that were not thrown.voidDisplays exceptions that were thrown.voidstop()Stops further checks in the current checker.Converts the current checker instance to aCheckerof the same type.updateChecker(AbstractChecker<?, ?> checker) Copies the key state from another checker to this instance.
-
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
-
setObject
-
setName
-
setExceptionTracker
Sets the exception tracker.- Parameters:
exceptionTracker- the tracker to set- Returns:
- this checker instance
-
setSaveErrors
Sets whether errors should be saved instead of thrown.- Parameters:
saveErrors- true to save errors, false to throw immediately- Returns:
- this checker instance
-
setStop
Sets whether further checks should stop.- Parameters:
stop- true to stop further checks, false otherwise- Returns:
- this checker instance
-
updateChecker
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
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
Returns the concrete checker instance (for fluent API).- Returns:
- The concrete checker instance
-
is
-
is
Validates the object with a custom condition and message.- Specified by:
isin 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:
isin 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:
isNotin 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:
isNotin 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. -
showPassedChecks
public void showPassedChecks()Displays exceptions that were not thrown. -
show
public void show()Displays all tracked exceptions. -
checkProperty
Evaluates a property of the current object using the provided extractor function and returns aCheckerfor 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 innull.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 objectpropertyName- logical name of the property, used for error reporting- Returns:
- a
Checkerfor the extracted property, ornullif extraction fails
-
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
-
toChecker
-
getObject
Returns the object being checked by this checker.- Returns:
- The object under validation
-