|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface IPredicate
A predicate says whether something is true or false about a
Sim
. A class implementing the IPredicate interface does
this via the isOK
method which returns true
if some condition about a Sim is true and false
otherwise.
A typical use for a predicate is to find a certain kind of thing for a robot to examine; for example, a Light. To do this, define a class implementing IPredicate as follows:
public class ALightPred implements IPredicate { //return true if the Sim passed is a Light, false otherwise public boolean isOK(Sim s) { return s instanceof Light; } }
In a subclass of Robot invoke the examineThings
method
like this:
Light t = this.examineLights(new ALightPred()).next()which will return a light from the current intersection that matches the predicate, if there is one, and throw an exception if there is not. The
isBesideThing
method in the Robot
class can be used to
determine if the specified kind of Thing
is available.
IPredicate
also defines a number of useful
predicates as constants. For example, to pick up a Thing
that is a Flasher
, one could write
karel.pickThing(IPredicate.aFlasher);
Field Summary | |
---|---|
static IPredicate |
aFlasher
A predicate to test whether something is a Flasher. |
static IPredicate |
aLight
A predicate to test whether something is a Light. |
static IPredicate |
anyFlasher
A predicate to test whether something is a Flasher or a subclass of Flasher. |
static IPredicate |
anyLight
A predicate to test whether something is a Light or a subclass of Light. |
static IPredicate |
anyRobot
A predicate to test whether something is a Robot or a subclass of Robot. |
static IPredicate |
anyStreetlight
A predicate to test whether something is a Streetlight or a subclass of Streetlight. |
static IPredicate |
anyThing
A predicate to test whether something is a Thing or a subclass of Thing. |
static IPredicate |
anyWall
A predicate to test whether something is a Wall or a subclass of Wall. |
static IPredicate |
aRobot
A predicate to test whether something is a Robot. |
static IPredicate |
aStreetlight
A predicate to test whether something is a Streetlight. |
static IPredicate |
aThing
A predicate to test whether something is a Thing. |
static IPredicate |
aWall
A predicate to test whether something is a Wall. |
static IPredicate |
canBeCarried
A predicate to test whether the Thing is something that a robot can carry. |
Method Summary | |
---|---|
boolean |
isOK(Sim theSim)
Return true if a certain condition is true about theSim; false otherwise. |
Field Detail |
---|
static final IPredicate aFlasher
karel.pickThing(IPredicate.aFlasher);
to pick
up a Flasher (but not a subclass of Flasher) from the current intersection.
static final IPredicate anyFlasher
karel.pickThing(IPredicate.anyFlasher);
to pick
up a Flasher (or a subclass of Flasher) from the current intersection.
static final IPredicate aLight
karel.pickThing(IPredicate.aLight);
to pick
up a Light (but not a subclass of Light) from the current intersection.
static final IPredicate anyLight
karel.pickThing(IPredicate.anyLight);
to pick
up a Light (or a subclass of Light) from the current intersection.
static final IPredicate aRobot
karel.pickThing(IPredicate.aRobot);
to examine
a Robot (but not a subclass of Robot) from the current intersection.
static final IPredicate anyRobot
karel.pickThing(IPredicate.anyRobot);
to examine
a Robot (or a subclass of Robot) from the current intersection.
static final IPredicate aStreetlight
karel.examineThing(IPredicate.aStreetlight);
to examine
a Streetlight (but not a subclass of Streetlight) from the current intersection.
static final IPredicate anyStreetlight
karel.examineThing(IPredicate.anyStreetlight);
to examine
a Streetlight (or a subclass of Streetlight) from the current intersection.
static final IPredicate aThing
karel.pickThing(IPredicate.aThing);
to pick
up a Thing (but not a subclass of Thing) from the current intersection.
static final IPredicate anyThing
karel.pickThing(IPredicate.anyThing);
to pick
up a Thing (or a subclass of Thing) from the current intersection.
static final IPredicate canBeCarried
karel.pickThing(IPredicate.canBeCarried);
to pick
up a Thing (or a subclass of Thing) from the current intersection.
static final IPredicate aWall
karel.examineThing(IPredicate.aWall);
to examine
a Wall (but not a subclass of Wall) from the current intersection.
static final IPredicate anyWall
karel.pickThing(IPredicate.aWallPred);
to examine
a Wall (or a subclass of Wall) from the current intersection.
Method Detail |
---|
boolean isOK(Sim theSim)
theSim
- The Sim object to test.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |