/* * This code is from the book: * * Winder, R and Roberts, G (2000) Developing Java * Software, second edition, John Wiley & Sons. * * It is copyright (c) 2000 Russel Winder and Graham Roberts. */ package SimFrameWork ; import java.util.List ; import java.util.ArrayList ; /** * This class represents the abstract properties of a patch in a * simulation. * * @version 2.0 March 1999 * @author Graham Roberts */ public abstract class Patch { /** * An array is used to store references to patch neighbours * (for efficiency) and a List collection to store the turtles * currently on the patch. * The neighbours are fixed and once connected do not change. * The turtles do change so an ArrayList is used for easy * manipulation. Only this class should know the actual * implementation type is ArrayList. All other class should * use the List interface. */ public Patch() { neighbours = new Patch[8] ; turtles = new ArrayList() ; } /** * Update patch one time step. */ public abstract void update() ; /** * Add a turtle to the collection already on patch. * * @param t turtle to be added to those on the patch. */ public final void addTurtle(Turtle t) { turtles.add(t) ; } /** * Return number of turtles on patch. * * @return the number of turtles on the patch. */ public final int turtleCount() { return turtles.size() ; } /** * Remove turtle from patch. * * @param t turtle to be removed from the patch, */ public final void removeTurtle(Turtle t) { turtles.remove(t) ; } /** * Return list of turtles on patch. * * @return a List of the turtles on the patch. */ public final List getTurtles() { return turtles ; } /** * Return reference to selected neighbour patch. * * @param i direction of neighbour. */ public final Patch getNeighbour(int i) { return neighbours[i] ; } /** * Return patch value. This can be overridden to provide a * more sophisticated kind of value or values. * * @return integer value of patch. */ public int value() { return patchValue ; } /** * Increment value of patch. */ public void incrValue() { patchValue += 1 ; } /** * Decrement value of patch. */ public void decrValue() { patchValue -= 1 ; } /** * Connect patch to a neighbour. Protected so that the method is * only accessible inside package. * * @param nbour direction of neighbour to connect. * @param p reference to neighbouring patch. */ protected final void setNeighbour(int nbour, Patch p) { neighbours[nbour] = p ; } /* These constants are used to provide directions for neighbouring * patches. */ /** * Constant to represent North */ public final static int N = 0 ; /** * Constant to represent North East */ public final static int NE = 1 ; /** * Constant to represent East */ public final static int E = 2 ; /** * Constant to represent South East */ public final static int SE = 3 ; /** * Constant to represent South */ public final static int S = 4 ; /** * Constant to represent South West */ public final static int SW = 5 ; /** * Constant to represent West */ public final static int W = 6 ; /** * Constant to represent North West */ public final static int NW = 7 ; /** * Generic value of patch, available to all patches. * Subclasses can add more. */ protected int patchValue; /** * References to neighbouring patches */ private Patch[] neighbours ; /** * Collection of turtles currently on patch. */ private List turtles ; }