Package microsim.space
Class MultiObjectSpace
- java.lang.Object
-
- microsim.space.AbstractSpace<java.lang.Object>
-
- microsim.space.DenseObjectSpace
-
- microsim.space.MultiObjectSpace
-
- All Implemented Interfaces:
ObjectSpace
public class MultiObjectSpace extends DenseObjectSpace
A bidimensional grid capable of containing many objects into each cell.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 MultiObjectSpace(int xSize, int ySize)
Create a grid with given size and a default intial capacity.MultiObjectSpace(int xSize, int ySize, int cellCapacity)
Create a grid with given size and a given intial capacity.MultiObjectSpace(AbstractSpace<java.lang.Object> grid)
Create a copy of the given grid with a default intial capacity.MultiObjectSpace(AbstractSpace<java.lang.Object> grid, int cellCapacity)
Create a copy of the given grid with a given intial capacity.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
add(java.lang.Object o)
Add an object implementing IGridPosition interface to the grid.boolean
addGridPosition(SpacePosition position)
Add an object implementing IGridPosition interface to the grid.int
countAt(int x, int y)
Deprecated.Its name has been changed in countObjectsAt Alias for countObjectsAtint
countObjectsAt(int x, int y)
Return the number of objects stored at the given position.java.lang.Object
get(int x, int y, int z)
Return the object stored at the given tridimensional position.boolean
moveGridPosition(SpacePosition position, int destinationX, int destinationY)
Move a IGridPosition object from its current position to the specified destination.boolean
remove(java.lang.Object o)
Remove the given object from the grid.boolean
removeAt(int x, int y, java.lang.Object o)
Remove the given object from the grid at given position.boolean
removeGridPosition(SpacePosition position)
Remove the IGridPosition object from the grid.void
set(int x, int y, int z, java.lang.Object obj)
Put the given object at the given tridimensional position.void
set(int x, int y, java.lang.Object obj)
Enqueue the given object at the given position.-
Methods inherited from class microsim.space.DenseObjectSpace
clear, get, getMatrix, getMooreNeighbors, getMooreNeighbors, getVonNeumannNeighbors, getVonNeumannNeighbors, size, swapPositions
-
Methods inherited from class microsim.space.AbstractSpace
boundX, boundY, equals, getMooreNeighborsPositions, getVonNeumannNeighborsPositions, getXSize, getYSize, reflectX, reflectY, torusX, torusY, toString
-
-
-
-
Constructor Detail
-
MultiObjectSpace
public MultiObjectSpace(AbstractSpace<java.lang.Object> grid, int cellCapacity)
Create a copy of the given grid with a given intial capacity.- Parameters:
grid
- The source grid.cellCapacity
- The initial vector size allocated for each cell. It grows automatically.
-
MultiObjectSpace
public MultiObjectSpace(int xSize, int ySize, int cellCapacity)
Create a grid with given size and a given intial capacity.- Parameters:
xSize
- The width of the grid.ySize
- The height of the grid.cellCapacity
- The initial vector size allocated for each cell. It grows automatically.
-
MultiObjectSpace
public MultiObjectSpace(AbstractSpace<java.lang.Object> grid)
Create a copy of the given grid with a default intial capacity.- Parameters:
grid
- The source grid.
-
MultiObjectSpace
public MultiObjectSpace(int xSize, int ySize)
Create a grid with given size and a default intial capacity.- Parameters:
xSize
- The width of the grid.ySize
- The height of the grid.
-
-
Method Detail
-
countObjectsAt
public int countObjectsAt(int x, int y)
Return the number of objects stored at the given position.- Specified by:
countObjectsAt
in interfaceObjectSpace
- Overrides:
countObjectsAt
in classDenseObjectSpace
- Parameters:
x
- The x coordinate.y
- The y coordinate.- Returns:
- The number of objects at (x, y).
-
countAt
public int countAt(int x, int y)
Deprecated.Its name has been changed in countObjectsAt Alias for countObjectsAt
-
get
public java.lang.Object get(int x, int y, int z)
Return the object stored at the given tridimensional position.- Parameters:
x
- The x coordinate.y
- The y coordinate.z
- The index of the vector of object stored at the (x, y) cell. WARNING There is no bound checking. Before using this method please ensure the object requestes does exists.- Returns:
- The requested object.
-
set
public void set(int x, int y, java.lang.Object obj)
Enqueue the given object at the given position.- Specified by:
set
in interfaceObjectSpace
- Overrides:
set
in classDenseObjectSpace
- Parameters:
x
- The x coordinate.y
- The y coordinate.obj
- The object to be stored into the vector at the (x, y) cell.
-
moveGridPosition
public boolean moveGridPosition(SpacePosition position, int destinationX, int destinationY)
Move a IGridPosition object from its current position to the specified destination.- Specified by:
moveGridPosition
in interfaceObjectSpace
- Overrides:
moveGridPosition
in classDenseObjectSpace
- Parameters:
destinationX
- The x destination coordinate.destinationY
- The y destination coordinate.object
- An object implementing IGridPosition interface.- Returns:
- true. Only if the argument object is null the return value will be false.
-
set
public void set(int x, int y, int z, java.lang.Object obj)
Put the given object at the given tridimensional position.- Parameters:
x
- The x coordinate.y
- The y coordinate.z
- The index of the vector of object stored at the (x, y) cell. If z is out of vector bounds this will be automatically grown.obj
- The object to be stored into the vector at the (x, y) cell.
-
add
public boolean add(java.lang.Object o)
Add an object implementing IGridPosition interface to the grid. If object implements IGridPosition it stored in the right position of the grid.- Overrides:
add
in classDenseObjectSpace
- Parameters:
o
- The IGridPosition object to be added.- Returns:
- True if object was added. If o does not implement IGridPosition interface it will not be added and method will return false.
-
addGridPosition
public boolean addGridPosition(SpacePosition position)
Add an object implementing IGridPosition interface to the grid. If the destination cell is already occupied the method return false and the object is not added.- Specified by:
addGridPosition
in interfaceObjectSpace
- Overrides:
addGridPosition
in classDenseObjectSpace
- Parameters:
object
- The IGridPosition object to be added.- Returns:
- True if object has been added. False if destination cell is already occupied or if argument object is null.
-
remove
public boolean remove(java.lang.Object o)
Remove the given object from the grid.- Parameters:
o
- The object to be removed.- Returns:
- True if object was found and removed, false otherwise.
-
removeGridPosition
public boolean removeGridPosition(SpacePosition position)
Remove the IGridPosition object from the grid.- Specified by:
removeGridPosition
in interfaceObjectSpace
- Overrides:
removeGridPosition
in classDenseObjectSpace
- Parameters:
object
- The IGridPosition object to be removed.- Returns:
- true if object has been removed. False if object is null or is not present on the grid.
-
removeAt
public boolean removeAt(int x, int y, java.lang.Object o)
Remove the given object from the grid at given position. If the object position is known this method is better than than remove() because it works much faster.- Parameters:
x
- The x coordinate.y
- The y coordinate.o
- The object to be removed.- Returns:
- True if object was found and removed, false otherwise.
-
-