public class Filter extends Object
Represents a filtering condition which may be used against a query to filter results based on the expressions set into this filter. A Filter
instance may consist of either an Expression
or two filters operated on by a BinaryLogicalOperator
, but not both. Setting an expression will reset the operands and the logical operator to null
. Setting a filter operand or the logical operator will reset the Expression
to null
.
A composite filter can be created by invoking Filter.and(Filter)
and Filter.or(Filter)
methods on a filter instance. In such cases, the current filter instance is the left operand, whereas the filter instance passed into the argument is the right operand for the connecting BinaryLogicalOperator
.
A filter can be complemented by invoking Filter.complement()
method on a filter instance.
Examples:
To create the composite filter filter1 AND filter2
:
filter1.and(filter2)
.
To create the composite filter filter1 AND (filter2 OR filter3)
:
filter1.and(filter2.or(filter3))
.
To create the complement equivalent of the composite filter1 AND filter2
:
filter1.and(filter2).complement()
.
To create the composite filter of filter1 AND (complement of filter2)
:
filter1.and(filter2.complement())
.
A Filter
instance overrides Filter.hashCode()
and Filter.equals(Object)
and hence can be used in sets.
Constructor and Description |
---|
Filter()
Default constructor does not instantiate any fields.
|
Filter(Filter source)
Copy constructor builds a new instance from the source.
|
Modifier and Type | Method and Description |
---|---|
Filter |
and(Filter other)
Returns a composite filter by setting a logical
AND operator on this filter and the other filter. |
Filter |
complement()
Complements and returns this filter.
|
boolean |
equals(Object obj)
Compares the specified object with this filter for equality.
|
Expression |
getExpression()
Returns the expression set in this filter.
|
Filter |
getLeft()
Returns the left operand of this filter.
|
BinaryLogicalOperator |
getLogicalOperator()
Returns the binary logical operator for this filter.
|
Filter |
getRight()
Returns the right operand of this filter.
|
int |
hashCode()
Returns the hash code value for this filter.
|
Filter |
or(Filter other)
Returns a composite filter by setting a logical
OR operator on this filter and the other filter. |
void |
setExpression(Expression expression)
Sets an expression into this filter.
|
void |
setLeft(Filter left)
Sets the left operand of this filter.
|
void |
setLogicalOperator(BinaryLogicalOperator logicalOperator)
Sets the binary logical operator for this filter.
|
void |
setRight(Filter right)
Sets the right operand of this filter.
|
String |
toString()
Returns a string representation of this filter.
|
public Filter()
public Filter(Filter source)
source
- the source instance from which the state is replicated in the new instance.public Expression getExpression()
public void setExpression(Expression expression)
expression
- the expression to setpublic Filter getLeft()
public void setLeft(Filter left)
left
- the left operand filter to setpublic Filter getRight()
public void setRight(Filter right)
right
- the right operand filter to setpublic BinaryLogicalOperator getLogicalOperator()
public void setLogicalOperator(BinaryLogicalOperator logicalOperator)
logicalOperator
- the logical operator to setpublic Filter complement()
Expression
only, returns a new filter with an expression which complements the expression in this filter. If the filter is a composite filter, returns a new filter in which the sub expressions are complemented as per DeMorgan's law.public Filter and(Filter other)
AND
operator on this filter and the other
filter. This filter is treated as the left operand in the AND
expression.other
- another filter which is treated as the right operand in the AND
expression.this AND other
.public Filter or(Filter other)
OR
operator on this filter and the other
filter. This filter is treated as the left operand in the OR
expression.other
- another filter which is treated as the right operand in the OR
expression.this OR other
.public String toString()
The string representation consists of the expression if the expression is set. If the expression is not set, but this filter represents a logical operation, then the string representation is: Left-Filter Binary-Logical-Operator Right-Filter
toString
in class Object
Object.toString()
public int hashCode()
The hash code of a filter is defined to be the sum of the hash codes of its expression, its logical operator and its logical operands. This ensures that f1.equals(f2)
implies that f1.hashCode() == f2.hashCode()
for any two filters f1
and f2
, as required by the general contract of Object.hashCode()
. Since the operands are also filters, this method will recurse into each filter and call Filter.hashCode()
on each of the operands in the set, and adding up the results.
hashCode
in class Object
Object.hashCode()
public boolean equals(Object obj)
Returns true
if the given object is also a filter and the two filters represent the same filter condition or operation. Two filters f1
and f2
represent the same filter condition if f1.getExpression().equals(f2.getExpression())
is true
. Two filters f1
and f2
represent the same filter operation if f1.getLeft().equals(f2.getLeft())
, f1.getRight().equals(f2.getRight())
, f1.getLogicalOperator().equals(f2.getLogicalOperator())
are true
.
This implementation first checks if the specified object is this filter; if so it returns true
. Then, it checks if the specified object is a filter whose expressions are equal; if not, it returns false
. If the expressions are both null
or equal, it checks if the specified object is a filter whose binary operation (the left and right operands, and the binary logical operator are also equal). Since the operands are also filters, this method will recurse into each filter until it hits a filter which only has an expression, but no binary operation. If the recursion completes, true
is returned.
equals
in class Object
obj
- object to be compared for equality with this filtertrue
if the specified object is equal to this filterObject.equals(java.lang.Object)