Package microsim.space
Class DenseObjectSpace
- All Implemented Interfaces:
- ObjectSpace
- Direct Known Subclasses:
- MultiObjectSpace
A bi-dimensional grid containing one object per cell only.
- 
Field SummaryFieldsFields inherited from class microsim.space.AbstractSpacemodCount, xSize, ySize
- 
Constructor SummaryConstructorsConstructorDescriptionDenseObjectSpace(int xSize, int ySize) Creates an empty grid with the given size.DenseObjectSpace(@NonNull AbstractSpace<Object> grid) Creates a copy of the given grid.
- 
Method SummaryModifier and TypeMethodDescriptionbooleanAdds an object implementingSpacePositioninterface to the grid.booleanaddGridPosition(@Nullable SpacePosition position) Add an object implementingSpacePositioninterface to the grid.voidclear()Set all cells to the null value.intcountObjectsAt(int x, int y) Return the number of objects currently on the given position.@Nullable Objectget(int x, int y) Returns the object stored at the given position.@NonNull Object[][]Return the matrix of objects representing the grid.@NonNull Object[]getMooreNeighbors(int x, int y) Gets the Moore neighbors of the specified coordinate.@NonNull Object[]getMooreNeighbors(int x, int y, int xExtent, int yExtent) Gets the extended Moore neighbors of the specified coordinate.@NonNull Object[]getVonNeumannNeighbors(int x, int y) Gets the von Neumann neighbors of the specified coordinate.@NonNull Object[]getVonNeumannNeighbors(int x, int y, int xExtent, int yExtent) Gets the extended von Neumann neighbors of the specified coordinate.booleanmoveGridPosition(@Nullable SpacePosition position, int destinationX, int destinationY) Move aSpacePositionobject from its current position to the specified destination, only if the destination cell is empty.booleanremoveGridPosition(@Nullable SpacePosition position) Remove theSpacePositionobject from the grid.voidPut the given object at the given position.intsize()Return the number of objects stored into the grid.voidswapPositions(int x1, int y1, int x2, int y2) Swap the content of the(x1, y1)and(x2, y2)cells of the grid.Methods inherited from class microsim.space.AbstractSpaceboundX, boundY, equals, getMooreNeighborsPositions, getVonNeumannNeighborsPositions, reflectX, reflectY, torusX, torusY, toString
- 
Field Details- 
m
- 
objectsprotected int objects
 
- 
- 
Constructor Details- 
DenseObjectSpaceCreates a copy of the given grid.- Parameters:
- grid- The source grid.
 
- 
DenseObjectSpacepublic DenseObjectSpace(int xSize, int ySize) Creates an empty grid with the given size.- Parameters:
- xSize- The width of the grid.
- ySize- The height of the grid.
 
 
- 
- 
Method Details- 
getReturns the object stored at the given position.- Specified by:
- getin interface- ObjectSpace
- Specified by:
- getin class- AbstractSpace<Object>
- Parameters:
- x- The x coordinate.
- y- The y coordinate.
- Returns:
- The requested object.
 
- 
addAdds an object implementingSpacePositioninterface to the grid. If object implementsSpacePositionit stored in the right position of the grid.- Parameters:
- o- The- SpacePositionobject to be added.
- Returns:
- True if object was added. If odoes not implementSpacePositioninterface it will not be added and method will return false.
 
- 
swapPositionspublic void swapPositions(int x1, int y1, int x2, int y2) Swap the content of the(x1, y1)and(x2, y2)cells of the grid.- Specified by:
- swapPositionsin class- AbstractSpace<Object>
- 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.
 
- 
setPut the given object at the given position.- Specified by:
- setin interface- ObjectSpace
- Specified by:
- setin class- AbstractSpace<Object>
- Parameters:
- x- The- xcoordinate.
- y- The- ycoordinate.
- obj- The object to be stored at the- (x, y)cell.
 
- 
getMooreNeighborsGets the Moore neighbors of the specified coordinate. Objects are returned by row starting with the "NW corner" and ending with the "SE corner." The object at(x,y)is not returned.- Parameters:
- x- The- xcoordinate of the object
- y- The- ycoordinate of the object
- Returns:
- an array of objects ordered by row starting with the "NW corner" and ending with the "SE corner."
 
- 
getMooreNeighborsGets the extended Moore neighbors of the specified coordinate. The extensions in thexandydirections are specified byxExtentandyExtent. Objects are returned by row starting with the "NW corner" and ending with the "SE corner." The object atx,yis not returned.- Parameters:
- x- The- xcoordinate of the object
- y- The- ycoordinate of the object
- xExtent- The extension of the neighborhood in the- xdirection
- yExtent- The extension of the neighborhood in the- ydirection
- Returns:
- an array of doubles ordered by row starting with the "NW corner" and ending with the "SE corner."
 
- 
getVonNeumannNeighborsGets the von Neumann neighbors of the specified coordinate. Objects are returned in west, east, north, south order. The double atx,yis not returned.- Parameters:
- x- The- xcoordinate of the object.
- y- The- ycoordinate of the object.
- Returns:
- an array of doubles in west, east, north, south order.
 
- 
getVonNeumannNeighborsGets the extended von Neumann neighbors of the specified coordinate. The extensions in thexandydirections are specified byxExtentandyExtent. Objects are returned in west, east, north, south order with the most distant object first. The object atx,yis not returned.- Parameters:
- x- The x coordinate of the object.
- y- The y coordinate of the object.
- xExtent- The extension of the neighborhood in the x direction.
- yExtent- The extension of the neighborhood in the y direction.
- Returns:
- an array of doubles in west, east, north, south order with the most distant object first.
 
- 
moveGridPositionpublic boolean moveGridPosition(@Nullable @Nullable SpacePosition position, int destinationX, int destinationY) Move aSpacePositionobject from its current position to the specified destination, only if the destination cell is empty.- Specified by:
- moveGridPositionin interface- ObjectSpace
- Parameters:
- destinationX- The x destination coordinate.
- destinationY- The y destination coordinate.
- position- An object implementing- SpacePositioninterface.
- Returns:
- true if object has been moved, false otherwise.
 
- 
getMatrixReturn the matrix of objects representing the grid.- Returns:
- A matrix of Objectwith the same dimensions of the grid.
 
- 
sizepublic int size()Return the number of objects stored into the grid.- Specified by:
- sizein class- AbstractSpace<Object>
- Returns:
- The sum of cells containing an object.
 
- 
clearpublic void clear()Set all cells to the null value.- Specified by:
- clearin class- AbstractSpace<Object>
 
- 
addGridPositionAdd an object implementingSpacePositioninterface to the grid. If the destination cell is already occupied the method return false and the object is not added.- Specified by:
- addGridPositionin interface- ObjectSpace
- Parameters:
- position- The- SpacePositionobject to be added.
- Returns:
- True if object has been added. False if destination cell is already occupied or if argument object is null.
 
- 
removeGridPositionRemove theSpacePositionobject from the grid.- Specified by:
- removeGridPositionin interface- ObjectSpace
- Parameters:
- position- The- SpacePositionobject to be removed.
- Returns:
- true if object has been removed. False if object is null or is not present on the grid.
 
- 
countObjectsAtpublic int countObjectsAt(int x, int y) Return the number of objects currently on the given position.- Specified by:
- countObjectsAtin interface- ObjectSpace
- Specified by:
- countObjectsAtin class- AbstractSpace<Object>
- Parameters:
- x- The x coordinate.
- y- The y coordinate.
- Returns:
- the number of object in the cell(x,y). Must be only 0 or 1.
 
 
-