Package microsim.space
Class AbstractSpace<E>
- java.lang.Object
-
- microsim.space.AbstractSpace<E>
-
- Direct Known Subclasses:
DenseObjectSpace
,DoubleSpace
,IntSpace
,SparseObjectSpace
public abstract class AbstractSpace<E> extends java.lang.Object
An abstract class representing bidimensional grid container. The different implementations of this class will manage the type of the storable values. It extends the standard jdk java.util.AbstractCollection to give each grid implementation a collection behaviour. So each grid can be iterated like an ArrayList. The specific GridIterator has been implemented to iterate elements storing their position on the grid.Title: JAS
Description: Java Agent-based Simulation library
Copyright (C) 2002 Michele Sonnessa
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.- Author:
- Michele Sonnessa
-
-
Constructor Summary
Constructors Constructor Description AbstractSpace(int xSize, int ySize)
Create a grid of given size.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description int
boundX(int x)
Check the given x coordinate.int
boundY(int y)
Check the given y coordinate.abstract void
clear()
Empty the content of the grid.abstract int
countObjectsAt(int x, int y)
Returns the number of objects allocated in cell (x,y).boolean
equals(java.lang.Object o)
Test if the passed object is equal.abstract E
get(int x, int y)
Return the object contained at given position.SpacePosition[]
getMooreNeighborsPositions(int x, int y)
Gets the extended Moore neighbors of the specified coordinate.SpacePosition[]
getVonNeumannNeighborsPositions(int x, int y)
Gets the extended von Neumann neighbors of the specified coordinate.int
getXSize()
Return the width of the grid.int
getYSize()
Return the height of the grid.int
reflectX(int x)
Check the given x coordinate considering the grid as a walled space.int
reflectY(int y)
Check the given y coordinate considering the grid as a walled space.abstract void
set(int x, int y, java.lang.Object obj)
Set the given Object at given position.abstract int
size()
Return the size of the grid.abstract void
swapPositions(int x1, int y1, int x2, int y2)
Swap the content of the (x1, y1) and (x2, y2) cells of the grid.int
torusX(int x)
Check the given x coordinate considering the grid as a toroid.int
torusY(int y)
Check the given y coordinate considering the grid as a toroid.java.lang.String
toString()
Return a string representing the content of the grid.
-
-
-
Method Detail
-
get
public abstract E get(int x, int y)
Return the object contained at given position. The type depends on the specific implementation of the Grid.- Parameters:
x
- The x coordinate. WARNING: No bounds checking for fast access.y
- The y coordinate. WARNING: No bounds checking for fast access.- Returns:
- The Double wrapper for value stored at x,y position of the grid.
-
set
public abstract void set(int x, int y, java.lang.Object obj)
Set the given Object at given position.- Parameters:
x
- The x coordinate. WARNING: No bounds checking for fast access.y
- The y coordinate. WARNING: No bounds checking for fast access.obj
- An object to be stored. The type depends on the specific implementation of the Grid.
-
swapPositions
public abstract void swapPositions(int x1, int y1, int x2, int y2)
Swap the content of the (x1, y1) and (x2, y2) cells of the grid.- Parameters:
x1
- The x coordinate for the first cell.y1
- The y coordinate for the first cell.x2
- The x coordinate for the second cell.y2
- The y coordinate for the second cell.
-
getXSize
public int getXSize()
Return the width of the grid.- Returns:
- The width.
-
getYSize
public int getYSize()
Return the height of the grid.- Returns:
- The height.
-
boundX
public int boundX(int x)
Check the given x coordinate. If it is out of grid bounds the returning value is truncated to the bound. For instance, if the grid is (100, 100) the instruction boundX(130) returns 100.- Parameters:
x
- The x coordinate to be tested.- Returns:
- A safe value representing the x value if inbound, the bound otherwise.
-
getMooreNeighborsPositions
public SpacePosition[] getMooreNeighborsPositions(int x, int y)
Gets the extended Moore neighbors of the specified coordinate. Points are returned by row starting with the "NW corner" and ending with the "SE corner." The Point at x,y is not returned.- Parameters:
x
- the x coordinate of the objecty
- the y coordinate of the object- Returns:
- an array of Point ordered by row starting with the "NW corner" and ending with the "SE corner."
-
getVonNeumannNeighborsPositions
public SpacePosition[] getVonNeumannNeighborsPositions(int x, int y)
Gets the extended von Neumann neighbors of the specified coordinate. Points are returned in west, east, north, south order with the most distant object first. The Point at x,y is not returned.- Parameters:
x
- the x coordinate of the objecty
- the y coordinate of the object- Returns:
- an array of Point in west, east, north, south order with the most distant object first.
-
countObjectsAt
public abstract int countObjectsAt(int x, int y)
Returns the number of objects allocated in cell (x,y).- Parameters:
x
- The x coordinate.y
- The y coordinate.
-
boundY
public int boundY(int y)
Check the given y coordinate. If it is out of grid bounds the returning value is truncated to the bound. For instance, if the grid is (100, 100) the instruction boundY(150) returns 100.- Parameters:
y
- The y coordinate to be tested.- Returns:
- A safe value representing the y value if inbound, the bound itself otherwise.
-
torusX
public int torusX(int x)
Check the given x coordinate considering the grid as a toroid. If x is out of grid bounds the returning coordinate is computed starting from the opposite bound. For instance, if the grid is (100, 100) the instruction torusX(130) returns 30, because over 100 the counter starts again from 0.- Parameters:
x
- The x coordinate to be tested.- Returns:
- A safe value representing the x value on the toroid.
-
torusY
public int torusY(int y)
Check the given y coordinate considering the grid as a toroid. If y is out of grid bounds the returning coordinate is computed starting from the opposite bound. For instance, if the grid is (100, 100) the instruction torusY(150) returns 50, because over 100 the counter starts again from 0.- Parameters:
y
- The y coordinate to be tested.- Returns:
- A safe value representing the y value on the toroid.
-
reflectX
public int reflectX(int x)
Check the given x coordinate considering the grid as a walled space. If x goes out of grid bounds the returning coordinate is computed mirroring the path in front of the bound. For instance, if the grid is (100, 100) the instruction reflectX(130) returns 70, because over 100 the path bounce on the wall and comes back.- Parameters:
x
- The x coordinate to be tested.- Returns:
- A safe value representing the x value on the walled grid.
-
reflectY
public int reflectY(int y)
Check the given y coordinate considering the grid as a walled space. If y goes out of grid bounds the returning coordinate is computed mirroring the path in front of the bound. For instance, if the grid is (100, 100) the instruction reflectY(140) returns 60, because over 100 the path bounce on the wall and comes back.- Parameters:
y
- The y coordinate to be tested.- Returns:
- A safe value representing the y value on the walled grid.
-
size
public abstract int size()
Return the size of the grid. It is width * height.- Returns:
- The number of cells in the grid.
-
clear
public abstract void clear()
Empty the content of the grid.
-
equals
public boolean equals(java.lang.Object o)
Test if the passed object is equal. The comparison is made at pointer level. WARNING If grids contains exactly the same values they are not equals. Only the same implementation of the grid is considered equals to itself.- Overrides:
equals
in classjava.lang.Object
- Parameters:
o
- The object to be compared.- Returns:
- True if objects are the same, false otherwise.
-
toString
public java.lang.String toString()
Return a string representing the content of the grid.- Overrides:
toString
in classjava.lang.Object
- Returns:
- The string representation of the grid like [elem1, elem2, .. ].
-
-