Class MultiObjectSpace

All Implemented Interfaces:
ObjectSpace

public class MultiObjectSpace extends DenseObjectSpace
A bi-dimensional grid capable of containing many objects into each cell.
  • Constructor Details

    • MultiObjectSpace

      public MultiObjectSpace(@NonNull @NonNull AbstractSpace<Object> grid, int cellCapacity)
      Create a copy of the given grid with a given initial 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 initial 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(@NonNull @NonNull AbstractSpace<Object> grid)
      Create a copy of the given grid with a default initial capacity.
      Parameters:
      grid - The source grid.
    • MultiObjectSpace

      public MultiObjectSpace(int xSize, int ySize)
      Create a grid with given size and a default initial capacity.
      Parameters:
      xSize - The width of the grid.
      ySize - The height of the grid.
  • Method Details

    • countObjectsAt

      public int countObjectsAt(int x, int y)
      Return the number of objects stored at the given position.
      Specified by:
      countObjectsAt in interface ObjectSpace
      Overrides:
      countObjectsAt in class DenseObjectSpace
      Parameters:
      x - The x coordinate.
      y - The y coordinate.
      Returns:
      The number of objects at (x, y).
    • get

      public Object get(int x, int y, int z)
      Return the object stored at the given tri-dimensional position.
      Parameters:
      x - The x coordinate.
      y - The y coordinate.
      z - The index of the vector of object stored at the (x, y) cell.
      Returns:
      The requested object.
    • set

      public void set(int x, int y, @Nullable @Nullable Object obj)
      Enqueue the given object at the given position.
      Specified by:
      set in interface ObjectSpace
      Overrides:
      set in class DenseObjectSpace
      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(@Nullable @Nullable SpacePosition position, int destinationX, int destinationY)
      Move a SpacePosition object from its current position to the specified destination.
      Specified by:
      moveGridPosition in interface ObjectSpace
      Overrides:
      moveGridPosition in class DenseObjectSpace
      Parameters:
      destinationX - The x destination coordinate.
      destinationY - The y destination coordinate.
      position - An object implementing SpacePosition 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, @Nullable @Nullable Object obj)
      Put the given object at the given tri-dimensional 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(@NonNull @NonNull Object o)
      Add an object implementing SpacePosition interface to the grid. If object implements SpacePosition it stored in the right position of the grid.
      Overrides:
      add in class DenseObjectSpace
      Parameters:
      o - The SpacePosition object to be added.
      Returns:
      True if object was added. If o does not implement SpacePosition interface it will not be added and method will return false.
    • addGridPosition

      public boolean addGridPosition(@Nullable @Nullable SpacePosition position)
      Add an object implementing SpacePosition 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 interface ObjectSpace
      Overrides:
      addGridPosition in class DenseObjectSpace
      Parameters:
      position - The SpacePosition 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(@NonNull @NonNull 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(@Nullable @Nullable SpacePosition position)
      Remove the SpacePosition object from the grid.
      Specified by:
      removeGridPosition in interface ObjectSpace
      Overrides:
      removeGridPosition in class DenseObjectSpace
      Parameters:
      position - The SpacePosition 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, @NonNull @NonNull Object o)
      Remove the given object from the grid at given position. If the object position is known this method is better than remove(Object) 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.