edu.kzoo.grid
Class GridObject

java.lang.Object
  |
  +--edu.kzoo.grid.GridObject
Direct Known Subclasses:
ColorBlock, PictureBlock, TextCell

public class GridObject
extends java.lang.Object

Grid Container Package:
A 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
  

Version:
13 December 2003
Author:
Alyce Brady
See Also:
Grid, Location

Constructor Summary
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.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GridObject

public GridObject()
Constructs an instance of a GridObject that is not yet in a grid.


GridObject

public GridObject(Grid grid,
                  Location loc)
Constructs an instance of a GridObject and places it in the specified grid. (Precondition: either both loc and grid are null or neither is null; if loc is not null, it is a valid empty location in grid.)

Parameters:
grid - the grid in which this object should be placed
loc - the location of this grid object
Throws:
java.lang.IllegalArgumentException - if the precondition is not met
Method Detail

isInAGrid

public final boolean isInAGrid()
Checks whether this object is in a grid.

Returns:
true if the object is in a grid; false otherwise

grid

public Grid grid()
Returns the grid in which this grid object exists.

Returns:
the grid containing this GridObject

location

public Location location()
Gets this grid object's location.

Returns:
the grid object's location

theGridObjectInvariantHolds

protected final boolean theGridObjectInvariantHolds()
Verifies that the object invariant holds.

Returns:
true if the object invariant holds; false if it has been violated

toString

public java.lang.String toString()
Returns a string representation of this grid object.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of this grid object

act

public void act()
Acts. For example, acts for one step in an animation or simulation. This implementation of act does not, in fact, do anything, but subclasses may redefine it to have specific, application-dependent behavior.


addToGrid

protected void addToGrid(Grid grid,
                         Location loc)
Adds this object to the specified grid at the specified location. (Precondition: this object is not currently in a grid; neither grid nor loc is null; loc is a valid empty location in grid.)

Parameters:
grid - the grid in which this object should be placed
loc - the location of this grid object
Throws:
java.lang.IllegalArgumentException - if the precondition is not met

changeLocation

protected void changeLocation(Location newLoc)
Modifies this grid object's location and notifies the grid. (Precondition: this object is in a grid and newLoc is a valid, empty location in the grid.)

Parameters:
newLoc - new location value
Throws:
java.lang.IllegalArgumentException - if the precondition is not met

removeFromGrid

protected void removeFromGrid()
Removes this object from the grid in which it was located.