Overview
The predicates module provides a framework for building complex logical conditions using composable predicates. It extends Java’s standardPredicate<T> interface with logical operators and rich composition capabilities.
Core Interfaces
LogicalPredicate Generic Type
Marker interface extendingPredicate for reflection and type safety.
- Marker for predicates that can be composed with logical operators
- Enables reflection-based rule engines to identify composable predicates
- Maintains type safety in complex predicate hierarchies
BinaryLogicalPredicate
Abstract base class for predicates that combine two predicates with a logical operator.Left operand predicate
Right operand predicate
Logical Operators
AndPredicate
Combines two predicates with logical AND.- Returns
trueonly if both left and right predicates returntrue - Short-circuits: if left is
false, right is not evaluated - Truth table:
trueANDtrue=truetrueANDfalse=falsefalseANDtrue=falsefalseANDfalse=false
OrPredicate
Combines two predicates with logical OR.- Returns
trueif either left or right predicate returnstrue - Short-circuits: if left is
true, right is not evaluated - Truth table:
trueORtrue=truetrueORfalse=truefalseORtrue=truefalseORfalse=false
NotPredicate
Negates a predicate (logical NOT).- Returns
trueif the wrapped predicate returnsfalse - Returns
falseif the wrapped predicate returnstrue - Truth table:
- NOT
true=false - NOT
false=true
- NOT
Rich Composition
RichLogicalPredicate
Enhanced predicate interface with fluent composition methods.Usage Examples
Basic Logical Operators
Customer Eligibility Rules
Product Filtering
Order Validation Rules
Fluent Composition with RichLogicalPredicate
Predicate Trees for Rule Engines
Dynamic Rule Construction
Testing Predicates
Design Patterns
Composite Pattern
Predicates form a composite tree structure:- Leaf nodes: Base predicates (lambdas)
- Composite nodes: AndPredicate, OrPredicate, NotPredicate
Strategy Pattern
Predicates encapsulate different evaluation strategies that can be swapped:Related Types
- Predicate: Java standard functional interface
- LogicalPredicate: Marker interface for composable predicates
- BinaryLogicalPredicate: Base for two-operand predicates
- AndPredicate: Logical AND operator
- OrPredicate: Logical OR operator
- NotPredicate: Logical NOT operator
- RichLogicalPredicate: Fluent composition interface
References
- Source:
/workspace/source/rules/src/main/java/com/softwarearchetypes/rules/predicates/ - LogicalPredicate:
LogicalPredicate.java:5-7 - BinaryLogicalPredicate:
BinaryLogicalPredicate.java:3-15 - AndPredicate:
AndPredicate.java:3-11 - OrPredicate:
OrPredicate.java:3-11
