public class GridObject
extends java.lang.Object
GridObject object is an object that can be contained
in a grid. If it is in a grid, it keeps track of its location
there. When it moves, it notifies the grid. This ensures that the
object and the grid in which it is located always agree about where
the object is.
Object invariant:
this.grid() == null && this.location() == null ||
this.grid().objectAt(this.location()) == this
| Constructor and Description |
|---|
GridObject()
Constructs an instance of a GridObject that is not yet in a grid.
|
GridObject(Grid grid,
Location loc)
Constructs an instance of a GridObject and places it in the specified
grid.
|
| Modifier and Type | Method and Description |
|---|---|
void |
act()
Acts.
|
protected void |
addToGrid(Grid grid,
Location loc)
Adds this object to the specified grid at the specified location.
|
protected void |
changeLocation(Location newLoc)
Modifies this grid object's location and notifies the grid.
|
Grid |
grid()
Returns the grid in which this grid object exists.
|
boolean |
isInAGrid()
Checks whether this object is in a grid.
|
Location |
location()
Gets this grid object's location.
|
protected void |
removeFromGrid()
Removes this object from the grid in which it was located.
|
protected boolean |
theGridObjectInvariantHolds()
Verifies that the object invariant holds.
|
java.lang.String |
toString()
Returns a string representation of this grid object.
|
public GridObject()
public GridObject(Grid grid, Location loc)
loc and grid are
null or neither is null; if loc
is not null, it is a valid empty location in
grid.)grid - the grid in which this object should be placedloc - the location of this grid objectjava.lang.IllegalArgumentException - if the precondition is not metpublic final boolean isInAGrid()
true if the object is in a grid;
false otherwisepublic Grid grid()
GridObjectpublic Location location()
protected final boolean theGridObjectInvariantHolds()
true if the object invariant holds;
false if it has been violatedpublic java.lang.String toString()
toString in class java.lang.Objectpublic void act()
act does not, in fact, do
anything, but subclasses may redefine it to have specific,
application-dependent behavior.protected void addToGrid(Grid grid, Location loc)
grid nor loc
is null; loc is a valid
empty location in grid.)grid - the grid in which this object should be placedloc - the location of this grid objectjava.lang.IllegalArgumentException - if the precondition is not metprotected void changeLocation(Location newLoc)
newLoc
is a valid, empty location in the grid.)newLoc - new location valuejava.lang.IllegalArgumentException - if the precondition is not metprotected void removeFromGrid()