Class SpelRuleEvaluationService
- All Implemented Interfaces:
RuleEvaluationService
Default implementation of RuleEvaluationService
that takes the configured
ExpressionParser
to parse SpEL Expression
s with caching for expressions and
EvaluationContexts
. Combined with
CachingStdEvaluationContextFactory
, supports SpEL expression evaluation with concrete
objects or maps.
Expressions are cached based on the literal expression as the key while EvaluationContexts
use the rootObject as the key.
- Author:
- Nathan Moore (nathanmoore).
- See Also:
-
- SPeL documentation
MapAccessor
MapAccessor
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final EvaluationContextFactory
protected final org.springframework.expression.ExpressionParser
-
Constructor Summary
ConstructorsConstructorDescriptionSpelRuleEvaluationService
(org.springframework.expression.ExpressionParser blSpelParser, EvaluationContextFactory blEvaluationContextFactory) -
Method Summary
Modifier and TypeMethodDescriptionboolean
allMatch
(Collection<String> rules, Object rootObject, ExpressionContext expressionContext) Use to determine if the object matches all rules provided.boolean
anyMatch
(Collection<String> rules, Object rootObject, ExpressionContext expressionContext) Use to determine if the object matches any rule provided.boolean
evaluateRule
(String rule, Object rootObject, ExpressionContext expressionContext) Uses the configured expression parser to evaluate the rule against the root object.<T> Iterable<T>
findAll
(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext) Use to find all objects matching the rules provided.<T> Optional<T>
findAny
(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext) Use to find an object that matches the rules.<T> Iterable<T>
findAnyN
(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext) Use to find a subset of all objects matching the rules provided.<T> Optional<T>
findFirst
(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext) Use to find the first object that matches the rules.<T> Iterable<T>
findFirstN
(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext) Use to find a subset of all objects matching the rules provided.protected org.springframework.expression.Expression
getExpressionFromCache
(String rule, ExpressionContext expressionContext) protected Optional<org.springframework.expression.ParserContext>
getParserContext
(ExpressionContext expressionContext) boolean
noneMatch
(Collection<String> rules, Object rootObject, ExpressionContext expressionContext) Use to determine if the object matches no provided rule.
-
Field Details
-
EXPRESSION_CACHE
-
spelParser
protected final org.springframework.expression.ExpressionParser spelParser -
evaluationContextFactory
-
-
Constructor Details
-
SpelRuleEvaluationService
public SpelRuleEvaluationService(org.springframework.expression.ExpressionParser blSpelParser, EvaluationContextFactory blEvaluationContextFactory)
-
-
Method Details
-
evaluateRule
Uses the configured expression parser to evaluate the rule against the root object. If the rule is null or blank, this method returns
true
.- Specified by:
evaluateRule
in interfaceRuleEvaluationService
- Parameters:
rule
- String representation of an expressionrootObject
- Object to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- whether the object matches the rule
- See Also:
-
anyMatch
public boolean anyMatch(Collection<String> rules, Object rootObject, ExpressionContext expressionContext) Use to determine if the object matches any rule provided.
- Specified by:
anyMatch
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObject
- Object to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- whether the object matches any rules
- See Also:
-
allMatch
public boolean allMatch(Collection<String> rules, Object rootObject, ExpressionContext expressionContext) Use to determine if the object matches all rules provided.
- Specified by:
allMatch
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObject
- Object to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- whether the object matches all rules
- See Also:
-
noneMatch
public boolean noneMatch(Collection<String> rules, Object rootObject, ExpressionContext expressionContext) Use to determine if the object matches no provided rule.
- Specified by:
noneMatch
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObject
- Object to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- whether the object matches no rules
- See Also:
-
findFirst
public <T> Optional<T> findFirst(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext) Use to find the first object that matches the rules.
ExpressionContext.isMatchAnyRule()
should be set to determine if returned object must match any or all rules.- Specified by:
findFirst
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObjects
- Objects to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- the first object provided that matches the rules
- See Also:
-
findAny
public <T> Optional<T> findAny(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext) Use to find an object that matches the rules.
ExpressionContext.isMatchAnyRule()
should be set to determine if returned object must match any or all rules.- Specified by:
findAny
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObjects
- Objects to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- an object provided that matches the rules
- See Also:
-
findAll
public <T> Iterable<T> findAll(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext) Use to find all objects matching the rules provided.
ExpressionContext.isMatchAnyRule()
should be set to determine if returned objects must match any or all rules.- Specified by:
findAll
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObjects
- Objects to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- all objects provided that match rules
- See Also:
-
findAnyN
public <T> Iterable<T> findAnyN(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext) Description copied from interface:RuleEvaluationService
Use to find a subset of all objects matching the rules provided. The subset will consist of any 'n' objects found with 'n' being determined by
ExpressionContext.getLimit()
.ExpressionContext.isMatchAnyRule()
should be set to determine if returned objects must match any or all rules.- Specified by:
findAnyN
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObjects
- Objects to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- a subset of 'n' objects matching the rules
- See Also:
-
findFirstN
public <T> Iterable<T> findFirstN(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext) Description copied from interface:RuleEvaluationService
Use to find a subset of all objects matching the rules provided. The subset will consist of the first 'n' objects found with 'n' being determined by
ExpressionContext.getLimit()
.ExpressionContext.isMatchAnyRule()
should be set to determine if returned objects must match any or all rules.- Specified by:
findFirstN
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObjects
- Objects to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- a subset of the first 'n' objects matching the rules
- See Also:
-
getExpressionFromCache
protected org.springframework.expression.Expression getExpressionFromCache(String rule, ExpressionContext expressionContext) -
getParserContext
protected Optional<org.springframework.expression.ParserContext> getParserContext(ExpressionContext expressionContext)
-