Package microsim.space
Class AbstractSpace<E>
java.lang.Object
microsim.space.AbstractSpace<E>
- Direct Known Subclasses:
DenseObjectSpace
,DoubleSpace
,IntSpace
,SparseObjectSpace
An abstract class representing bi-dimensional grid container. It extends the standard JDK
AbstractCollection
to let every implementation behave like a collection.-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
boundX
(int x) Check the givenx
coordinate.int
boundY
(int y) Check the giveny
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
Test if the passed object is equal.abstract E
get
(int x, int y) Return the object contained at a given position.@NonNull SpacePosition[]
getMooreNeighborsPositions
(int x, int y) Gets the extended Moore neighbors of the specified coordinate.@NonNull SpacePosition[]
getVonNeumannNeighborsPositions
(int x, int y) Gets the extended von Neumann neighbors of the specified coordinate.int
reflectX
(int x) Check the givenx
coordinate considering the grid as a walled space.int
reflectY
(int y) Check the giveny
coordinate considering the grid as a walled space.abstract void
Store an object at a given position.abstract int
size()
Return the total size of the grid, i.e.,width * height
.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 givenx
coordinate considering the grid as a toroid.int
torusY
(int y) Check the giveny
coordinate considering the grid as a toroid.@NonNull String
toString()
Return a string representing the content of the grid.
-
Field Details
-
xSize
protected int xSize -
ySize
protected int ySize -
modCount
protected transient int modCount
-
-
Constructor Details
-
AbstractSpace
public AbstractSpace(int xSize, int ySize) Creates a grid of given size.- Parameters:
xSize
- The width of the grid.ySize
- The height of the grid.
-
-
Method Details
-
get
Return the object contained at a given position. The type depends on the specific implementation of the Grid.- Parameters:
x
- The x coordinate.y
- The y coordinate.- Returns:
- a generic object.
-
set
Store an object at a given position.- Parameters:
x
- The x coordinate.y
- The y coordinate.obj
- The object to be stored.
-
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.
-
boundX
public int boundX(int x) Check the givenx
coordinate. If it is out of grid bounds the returning value is truncated to stay within the borders. For instance, if the grid is(100, 100)
the instruction boundX(130) returns 99.- Parameters:
x
- Thex
coordinate to be tested.- Returns:
- A safe value representing the
x
value if inbound, the bound otherwise.
-
boundY
public int boundY(int y) Check the giveny
coordinate. If it is out of grid bounds the returning value is truncated to stay within the borders. For instance, if the grid is(100, 100)
the instruction boundY(130) returns 99.- Parameters:
y
- They
coordinate to be tested.- Returns:
- A safe value representing the
y
value if inbound, the bound otherwise.
-
getMooreNeighborsPositions
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 atx,y
is not returned.- Parameters:
x
- The x coordinate of the object.y
- The y coordinate of the object.- Returns:
- an array of points ordered by row starting with the "NW corner" and ending with the "SE corner."
-
getVonNeumannNeighborsPositions
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 atx,y
is not returned.- Parameters:
x
- The x coordinate of the objecty
- The y coordinate of the object- Returns:
- an array of points 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.- Returns:
- the number of "entities" contained in the specified cell.
-
torusX
public int torusX(int x) Check the givenx
coordinate considering the grid as a toroid. Ifx
is out of grid bounds the returning coordinate is computed starting from the opposite bound. For instance, if the grid is(100, 100)
the instructiontorusX(130)
returns 30, because over 100 the counter starts again from 0.- Parameters:
x
- Thex
coordinate to be tested.- Returns:
- A safe value representing the
x
value on the toroid.
-
torusY
public int torusY(int y) Check the giveny
coordinate considering the grid as a toroid. Ify
is out of grid bounds the returning coordinate is computed starting from the opposite bound. For instance, if the grid is(100, 100)
the instructiontorusY(150)
returns 50, because over 100 the counter starts again from 0.- Parameters:
y
- They
coordinate to be tested.- Returns:
- A safe value representing the
y
value on the toroid.
-
reflectX
public int reflectX(int x) Check the givenx
coordinate considering the grid as a walled space. Ifx
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 instructionreflectX(130)
returns 70, because over 100 the path bounce on the wall and comes back.- Parameters:
x
- Thex
coordinate to be tested.- Returns:
- A safe value representing the
x
value on the walled grid.
-
reflectY
public int reflectY(int y) Check the giveny
coordinate considering the grid as a walled space. Ify
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 instructionreflectY(140)
returns 60, because over 100 the path bounce on the wall and comes back.- Parameters:
y
- They
coordinate to be tested.- Returns:
- A safe value representing the
y
value on the walled grid.
-
size
public abstract int size()Return the total size of the grid, i.e.,width * height
.- Returns:
- The number of cells in the grid.
-
clear
public abstract void clear()Empty the content of the grid. -
equals
Test if the passed object is equal. The comparison is made at pointer level. -
toString
Return a string representing the content of the grid.
-