Class Expression
java.lang.Object
org.apache.cayenne.exp.Expression
- All Implemented Interfaces:
XMLSerializable
- Direct Known Subclasses:
AggregateConditionExp, AsteriskExp, CaseWhenExp, ConditionExp, CustomOperatorExp, ElseExp, EnclosingObjectExp, FullObjectExp, ListExp, NegateExp, PathExp, ScalarExp, SubqueryExp, ThenExp, ValueExp
public abstract sealed class Expression
extends Object
implements XMLSerializable
permits AggregateConditionExp, AsteriskExp, CaseWhenExp, ConditionExp, CustomOperatorExp, ElseExp, EnclosingObjectExp, FullObjectExp, ListExp, NegateExp, PathExp, ScalarExp, SubqueryExp, ThenExp, ValueExp
Superclass of Cayenne expressions: a tree node holding its operands. An operand is either a nested expression or a
plain value. A node does not know its parent, so it can be shared between trees.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionandExp(Expression exp) Chains this expression with another expression using "and".andExp(Expression exp, Expression... expressions) Chains this expression with other expressions using "and".voidappendAsString(Appendable out) Appends own content as a String to the provided Appendable: the operands separated by the operator.protected voidappendOperandAsString(int index, Appendable out) Appends the operand at the given index to the output: a nested node as itself, wrapped in parentheses if it asks for it viaparenthesizeAsOperand(), a plain value as a literal.deepCopy()Clones this expression.voidencodeAsXML(XMLEncoder encoder, ProjectNodeVisitor delegate) Encodes itself, wrapping the string into XML CDATA section.booleanCalculates expression value with object as a context for path expressions.protected abstract ObjectEvaluates itself with object, pushing result on the stack.protected ObjectevaluateOperand(int index, Object o) Evaluates the operand at the given index: a nested node is evaluated against the object, a plain value is returned as is.exists()Returns expression that will be dynamically resolved to proper subqueries based on a relationships used (if no relationships are present in the original expression no subqueries will be used).expName()Returns String label for this expression: the class name without the "Exp" suffix.<T> Collection<?> filter(Collection<T> source, Collection<T> target) Adds objects matching this expression from the source collection to the target collection.<T> List<T> filterObjects(Collection<T> objects) Returns a list of objects that match the expression.<T> TReturns the first object in the list that matches the expression.protected voidFlattens the tree under this node by eliminating any operands that are nodes of the same class as this node and copying their operands to this node.protected abstract StringgetExpressionOperator(int index) Returns the operator printed between the operands, before the operand at the given index.getOperand(int index) Returns a value of operand atindex: a nested expression or a plain value.intReturns a count of operands of this expression.Returns a map of path aliases for this expression.inthashCode()protected booleanisValidParent(Expression parent) Whether this node can be an operand of the given node.booleanCalculates expression boolean value with object as a context for path expressions.Returns expression that will be dynamically resolved to proper subqueries based on a relationships used (if no relationships are present in the original expression no subqueries will be used).notExp()Returns a logical NOT of current expression.orExp(Expression exp) Chains this expression with another expression using "or".orExp(Expression exp, Expression... expressions) Chains this expression with other expressions using "or".Creates and returns a new Expression instance based on this expression, but with named parameters substituted with provided values.Creates and returns a new Expression instance based on this expression, but with named parameters substituted with provided values.If any subexpressions containing parameters not matching the "name" argument are found, the behavior depends on "pruneMissing" argument.paramsArray(Object... parameters) Creates and returns a new Expression instance based on this expression, but with parameters substituted with provided values.protected booleanWhether this node is wrapped in parentheses when printed as an operand of another node.protected booleanpruneNodeForPrunedChild(Object prunedChild) Returns true if this node should be pruned from expression tree in the event a child is removed.voidsetOperand(int index, Object value) Sets a value of operand atindex, growing the operands array if needed.voidsetOperands(Object... operands) Replaces all the operands of this node with the given ones.abstract ExpressionCreates a copy of this expression node, without copying children.toString()Creates a transformed copy of this expression, applying transformation provided by Transformer to all its nodes.protected ObjecttransformExpression(Function<Object, Object> transformer) A recursive method called from "transform" to do the actual transformation.protected voidtraverse(Expression parentExp, TraversalHandler visitor) Traverses itself and child expressions, notifying visitor via callback methods as it goes.voidtraverse(TraversalHandler visitor) Traverses itself and child expressions, notifying visitor via callback methods as it goes.
-
Field Details
-
PRUNED_NODE
A value that a Transformer might return to indicate that a node has to be pruned from the expression during the transformation.- Since:
- 1.2
-
operands
-
-
Constructor Details
-
Expression
-
-
Method Details
-
getPathAliases
-
expName
Returns String label for this expression: the class name without the "Exp" suffix. Used for debugging. -
equals
-
hashCode
-
paramsArray
Creates and returns a new Expression instance based on this expression, but with parameters substituted with provided values. This is a positional style of binding. If a given parameter name is used more than once, only the first occurrence is treated as "position", subsequent occurrences are bound with the same value as the first one. If expression parameters count is different from the array parameter count, an exception will be thrown.positional style would not allow subexpression pruning.
- Since:
- 4.0
-
params
Creates and returns a new Expression instance based on this expression, but with named parameters substituted with provided values. Any subexpressions containing parameters not matching the "name" argument will be pruned.Note that if you want matching against nulls to be preserved, you must place NULL values for the corresponding keys in the map.
- Since:
- 4.0
-
params
Creates and returns a new Expression instance based on this expression, but with named parameters substituted with provided values.If any subexpressions containing parameters not matching the "name" argument are found, the behavior depends on "pruneMissing" argument. If it is false an Exception will be thrown, otherwise subexpressions with missing parameters will be pruned from the resulting expression.Note that if you want matching against nulls to be preserved, you must place NULL values for the corresponding keys in the map.
- Since:
- 4.0
-
andExp
Chains this expression with another expression using "and". -
andExp
Chains this expression with other expressions using "and".- Since:
- 4.0
-
orExp
Chains this expression with another expression using "or". -
orExp
Chains this expression with other expressions using "or".- Since:
- 4.0
-
notExp
-
exists
Returns expression that will be dynamically resolved to proper subqueries based on a relationships used (if no relationships are present in the original expression no subqueries will be used).- Returns:
- exists expression
- Since:
- 5.0
- See Also:
-
notExists
Returns expression that will be dynamically resolved to proper subqueries based on a relationships used (if no relationships are present in the original expression no subqueries will be used).- Returns:
- not exists expression
- Since:
- 5.0
- See Also:
-
getOperandCount
public int getOperandCount()Returns a count of operands of this expression. In real life there are unary (count == 1), binary (count == 2) and ternary (count == 3) expressions. -
getOperand
Returns a value of operand atindex: a nested expression or a plain value. Operand indexing starts at 0. -
setOperand
Sets a value of operand atindex, growing the operands array if needed. Operand indexing starts at 0. APersistentis replaced with its ObjectId. A nested expression is asked first whether this node is a valid parent for it, seeisValidParent(Expression). -
setOperands
Replaces all the operands of this node with the given ones. Called by the constructor and by the parser once all the operands of a node are known, so a node that post-processes its operands overrides this method.- Since:
- 5.0
-
isValidParent
Whether this node can be an operand of the given node. Called by the parent when the operand is set, to check what the grammar can't: e.g. a condition can only be an operand of a condition aggregate. By default any parent is valid.- Since:
- 5.0
-
evaluate
-
evaluateNode
-
evaluateOperand
-
match
Calculates expression boolean value with object as a context for path expressions.- Since:
- 1.1
-
first
Returns the first object in the list that matches the expression.- Since:
- 3.1
-
filterObjects
Returns a list of objects that match the expression. -
filter
Adds objects matching this expression from the source collection to the target collection.- Since:
- 1.1
-
deepCopy
-
shallowCopy
Creates a copy of this expression node, without copying children.- Since:
- 1.1
-
pruneNodeForPrunedChild
Returns true if this node should be pruned from expression tree in the event a child is removed.- Since:
- 1.1
-
flattenTree
protected void flattenTree()Flattens the tree under this node by eliminating any operands that are nodes of the same class as this node and copying their operands to this node.- Since:
- 1.1
-
traverse
Traverses itself and child expressions, notifying visitor via callback methods as it goes. This is an Expression-specific implementation of the "Visitor" design pattern.- Since:
- 1.1
-
traverse
Traverses itself and child expressions, notifying visitor via callback methods as it goes.- Since:
- 1.1
-
transform
Creates a transformed copy of this expression, applying transformation provided by Transformer to all its nodes. Null transformer will result in an identical deep copy of this expression.To force a node and its children to be pruned from the copy, Transformer should return Expression.PRUNED_NODE. Otherwise an expectation is that if a node is an Expression it must be transformed to null or another Expression. Any other object type would result in a ExpressionException.
- Since:
- 1.1
-
transformExpression
-
encodeAsXML
Encodes itself, wrapping the string into XML CDATA section.- Specified by:
encodeAsXMLin interfaceXMLSerializable- Since:
- 1.1
-
appendAsString
Appends own content as a String to the provided Appendable: the operands separated by the operator.- Throws:
IOException- Since:
- 4.0
-
getExpressionOperator
Returns the operator printed between the operands, before the operand at the given index. -
parenthesizeAsOperand
protected boolean parenthesizeAsOperand()Whether this node is wrapped in parentheses when printed as an operand of another node. Compound nodes are, leaves and nodes with their own delimiters (function calls, lists) are not. -
appendOperandAsString
Appends the operand at the given index to the output: a nested node as itself, wrapped in parentheses if it asks for it viaparenthesizeAsOperand(), a plain value as a literal.- Throws:
IOException
-
toString
-