public abstract class ObjectdrawFilter extends Object
DrawableInterface
when searching. Note that the methods and
fields in this class are designed specifically to support a natural,
readable, boolean expression "mini-language" for use in describing a
single DrawableInterface
(or group of DrawableInterface
s)
by its (or their) properties. As a result, it does violate some
conventions regarding the use of public fields (although note that all
here are immutable) and occasionally even the naming conventions for
constants (e.g., where
). However, breaking these
conventions is necessary in this class in order to support the more
natural syntax for filter expressions, and so was deemed a better
design choice.
Client classes that wish to use these filters should add the following static import directive:
import static student.testingsupport.ObjectdrawFilter.ClientImports.*;
Note that the ObjectdrawTestCase
class already re-exports the
items defined in the ObjectdrawFilter.ClientImports
nested class, so Objectdraw test
cases should not include the static import.
The expressions that you can create with this class are designed to represent "filters" or boolean predicates that can be applied to a DrawableInterface, returning true if the object "matches" the filter or false if the object does not match.
Often, a filter object is created solely for the purpose of passing
the filter into some other operation, such as a search operation. For
example, the student.ODTestCase class provides a
getDrawable()
method that takes a filter as a parameter. For the examples below, we
will use getDrawable()
as the context, specifying each
filter as an argument value in a call to that method.
The basic principles for using this class are as follows:
Never try to create a ODFilter object directly. Instead, always
write something that looks like a boolean expression, and that
starts with the operator where
:
FilledRect rect = getDrawable(FilledRect.class, where.heightIs(10));
The basic properties you can check with filters include:
sizeIs
, textIs()
,
locationIs()
, hiddenIs()
,
and typeIs()
. They are
all used the same way:
FilledRect rect = getDrawable(FilledRect.class, where.widthIs(10); FramedOval oval = getDrawable(FramedOval.class, where.hiddenIs(true));
You can combine filters using logical "and" as necessary:
FilledRect rect = getDrawable(FilledRect.class, where.heightIs(10).and.widthIs(20).and.hiddenIs(false));
You can also use "or":
FilledRect rect = getDrawable(FilledRect.class, where.heightIs(10).or.widthIs(20).or.hiddenIs(false));
Operators like "and" and "or" are interpreted strictly left to right. There is no precedence, because of the way Java interprets dot notation.
FilledRect rect = getDrawable(FilledRect.class, where.heightIs(10).or.widthIs(20).and.hiddenIs(false)); // means ((height = 10 or width = 20) and hidden = false) // note that the left operator is always evaluated first!
If you want to force a different order of evaluation
than strictly left-to-right, then use parentheses by writing the
appropriate operator as and()
or or()
. Just
be sure to start the new expression inside the parentheses with
where
:
FilledRect rect = getDrawable(FilledRect.class, where.heightIs(10).or(where.widthIs(20).and.hiddenIs(false))); // now means (height = 10 or (width = 20 and hidden = false)) // because of the extra parentheses used
Finally, you can even use "not" (logical negation), but it is
called like a method, so parentheses (and thus a leading where)
are always required to make the intended extent of the negation
clear:
FilledRect rect = getDrawable(FilledRect.class, where.heightIs(10).and.not(where.widthIs(20).or.hiddenIs(true)));
Modifier and Type | Class and Description |
---|---|
class |
ObjectdrawFilter.BinaryOperator
A non-static subclass for binary operators that implicitly
captures the outer filter to which it belongs, using it as
the first/left argument to the operator.
|
static class |
ObjectdrawFilter.ClientImports
This class represents the "where" operator that is used to begin
a filter expression.
|
static class |
ObjectdrawFilter.Operator
This base class represents an operator used to create a query.
|
Modifier and Type | Field and Description |
---|---|
ObjectdrawFilter.BinaryOperator |
and
The "and" operator for combining filters, designed to be used in
expressions like
where.heightIs(10).and.hiddenIs(true) . |
ObjectdrawFilter.BinaryOperator |
or
The "or" operator for combining filters, designed to be used in
expressions like
where.nameIs("abc").or.nameIs("def") . |
Modifier | Constructor and Description |
---|---|
protected |
ObjectdrawFilter(String description)
Creates a new filter object.
|
Modifier and Type | Method and Description |
---|---|
ObjectdrawFilter |
and(ObjectdrawFilter otherFilter)
The "and" operator for combining filters, when you want to use
parentheses to group its righthand argument.
|
ObjectdrawFilter |
or(ObjectdrawFilter otherFilter)
The "or" operator for combining filters, when you want to use
parentheses to group its righthand argument.
|
abstract boolean |
test(objectdraw.DrawableInterface shape)
Evaluate whether a DrawableInterface object matches this filter.
|
String |
toString()
Get a string representation of this filter.
|
public final ObjectdrawFilter.BinaryOperator and
where.heightIs(10).and.hiddenIs(true)
.
This operator is implemented as a public field so that the simple
.and.
notation can be used as a connective between
filters. If you want to use parentheses for grouping to define
the right argument, see and(ObjectdrawFilter)
instead.public final ObjectdrawFilter.BinaryOperator or
where.nameIs("abc").or.nameIs("def")
.
This operator is implemented as a public field so that the simple
.or.
notation can be used as a connective between
filters. If you want to use parentheses for grouping to define
the right argument, see or(ObjectdrawFilter)
instead.protected ObjectdrawFilter(String description)
description
- A string description of this filter, used in
toString()
.public String toString()
public final ObjectdrawFilter and(ObjectdrawFilter otherFilter)
where.nameIs("abc").and(enabledIs(true).or.hasFocusIs(true))
.
If you wish to use the .and.
notation instead, leaving
off the parentheses, see and(ObjectdrawFilter)
.otherFilter
- The second argument to "and".public final ObjectdrawFilter or(ObjectdrawFilter otherFilter)
where.heightIs(10).or(hiddenIs(true).and.widthIs(20))
.
If you wish to use the .or.
notation instead, leaving
off the parentheses, see or(ObjectdrawFilter)
.otherFilter
- The second argument to "or".public abstract boolean test(objectdraw.DrawableInterface shape)
shape
- The DrawableInterface object to check.