Class DenseGrid<V>

  • Type Parameters:
    V - the type of the value
    All Implemented Interfaces:
    java.io.Serializable, Grid<V>

    public final class DenseGrid<V>
    extends java.lang.Object
    implements java.io.Serializable
    Mutable implementation of the Grid data structure based on an array.

    This uses one item of memory for each possible combination of row and column.

    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface org.joda.collect.grid.Grid

        Grid.Cell<V>
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Grid.Cell<V> cell​(int row, int column)
      Gets the cell at the specified row-column.
      java.util.Set<Grid.Cell<V>> cells()
      Gets the complete set of cells.
      void clear()
      Clears the grid.
      java.util.List<V> column​(int column)
      Gets the rows of a single column.
      int columnCount()
      Gets the number of columns in the grid.
      java.util.List<java.util.List<V>> columns()
      Gets the entire grid of values, by column then row.
      boolean contains​(int row, int column)
      Checks if a value is present at the specified row-column.
      boolean containsValue​(java.lang.Object valueToFind)
      Checks if the specified value is contained in the grid.
      static <V> DenseGrid<V> create​(int rowCount, int columnCount)
      Creates an empty DenseGrid of the specified size.
      static <V> DenseGrid<V> create​(Grid<? extends V> grid)
      Creates a DenseGrid copying from another grid.
      static <V> DenseGrid<V> create​(V[][] array)
      Creates a DenseGrid copying from an array.
      boolean equals​(java.lang.Object obj)
      Checks if this grid equals another grid.
      boolean exists​(int row, int column)
      Checks if the specified row-column exists.
      V get​(int row, int column)
      Gets the value at the specified row-column.
      int hashCode()
      Gets a suitable hash code.
      boolean isEmpty()
      Checks if the grid is empty.
      boolean isFull()
      Checks if the grid is full.
      void put​(int row, int column, V value)
      Puts a value into this grid.
      void putAll​(Grid<? extends V> grid)
      Puts all cells from a grid into this grid.
      boolean remove​(int row, int column)
      Removes the value at the specified row-column.
      java.util.List<V> row​(int row)
      Gets the columns of a single row.
      int rowCount()
      Gets the number of rows in the grid.
      java.util.List<java.util.List<V>> rows()
      Gets the entire grid of values, by row then column.
      int size()
      Gets the number of cells that are present.
      java.lang.String toString()  
      com.google.common.collect.ImmutableCollection<V> values()
      Gets the values in order through rows, then columns.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • create

        public static <V> DenseGrid<V> create​(int rowCount,
                                              int columnCount)
        Creates an empty DenseGrid of the specified size.
        Type Parameters:
        V - the type of the value
        Parameters:
        rowCount - the number of rows, zero or greater
        columnCount - the number of rows, zero or greater
        Returns:
        the mutable grid, not null
      • create

        public static <V> DenseGrid<V> create​(Grid<? extends V> grid)
        Creates a DenseGrid copying from another grid.
        Type Parameters:
        V - the type of the value
        Parameters:
        grid - the grid to copy, not null
        Returns:
        the mutable grid, not null
      • create

        public static <V> DenseGrid<V> create​(V[][] array)
        Creates a DenseGrid copying from an array.

        The row count and column count are derived from the maximum size of the array. The grid is initialized from the non-null values.

        Type Parameters:
        V - the type of the value
        Parameters:
        array - the array, first by row, then by column
        Returns:
        the mutable grid, not null
      • rowCount

        public int rowCount()
        Description copied from interface: Grid
        Gets the number of rows in the grid.

        A grid has a fixed number of rows and columns, but not all cells must be occupied. This returns the row capacity, not the number of occupied rows. It is guaranteed that Grid.contains(int, int) will return false for indices larger than the row count.

        Specified by:
        rowCount in interface Grid<V>
        Returns:
        the number of rows, zero or greater
      • columnCount

        public int columnCount()
        Description copied from interface: Grid
        Gets the number of columns in the grid.

        A grid has a fixed number of rows and columns, but not all cells must be occupied. This returns the column capacity, not the number of occupied columns. It is guaranteed that Grid.contains(int, int) will return false for indices larger than the column count.

        Specified by:
        columnCount in interface Grid<V>
        Returns:
        the number of columns, zero or greater
      • size

        public int size()
        Description copied from interface: Grid
        Gets the number of cells that are present.
        Specified by:
        size in interface Grid<V>
        Returns:
        the size of the set of cells
      • contains

        public boolean contains​(int row,
                                int column)
        Description copied from interface: Grid
        Checks if a value is present at the specified row-column.

        If either index does not exist, false is returned.

        Specified by:
        contains in interface Grid<V>
        Parameters:
        row - the row
        column - the column
        Returns:
        true if there is a value at the row-column
      • containsValue

        public boolean containsValue​(java.lang.Object valueToFind)
        Description copied from interface: Grid
        Checks if the specified value is contained in the grid.
        Specified by:
        containsValue in interface Grid<V>
        Parameters:
        valueToFind - the value to find, null returns false
        Returns:
        true if the grid contains the value
      • get

        public V get​(int row,
                     int column)
        Description copied from interface: Grid
        Gets the value at the specified row-column.

        If either index does not exist, null is returned.

        Specified by:
        get in interface Grid<V>
        Parameters:
        row - the row
        column - the column
        Returns:
        the value at the row-column, null if not found
      • cell

        public Grid.Cell<V> cell​(int row,
                                 int column)
        Description copied from interface: Grid
        Gets the cell at the specified row-column.

        If either index does not exist, null is returned.

        Specified by:
        cell in interface Grid<V>
        Parameters:
        row - the row
        column - the column
        Returns:
        the cell at the row-column, null if not found
      • cells

        public java.util.Set<Grid.Cell<V>> cells()
        Description copied from interface: Grid
        Gets the complete set of cells.

        If the grid is mutable then cells may be added or removed from the set. The cells are returned in order, looping around rows, then columns.

        The cell returned from the set iterator may be a mutable Cell implementation that cannot be stored beyond the lifetime of an iteration.

        Specified by:
        cells in interface Grid<V>
        Returns:
        the set of all cells, not null
      • values

        public com.google.common.collect.ImmutableCollection<V> values()
        Description copied from interface: Grid
        Gets the values in order through rows, then columns.

        The returned data structure is an ordered collection. The values are returned in order, looping around rows, then columns.

        Specified by:
        values in interface Grid<V>
        Returns:
        the collection of all values, not null
      • row

        public java.util.List<V> row​(int row)
        Description copied from interface: Grid
        Gets the columns of a single row.

        The list will contain all columns from zero to columnCount. Where there is no value for a cell, the list will contain null.

        The returned list is immutable, except for List.set(int, Object), which adds, updates or deletes from the underlying grid.

        Specified by:
        row in interface Grid<V>
        Parameters:
        row - the row, zero or greater
        Returns:
        the columns of the specified row, not null
      • rows

        public java.util.List<java.util.List<V>> rows()
        Description copied from interface: Grid
        Gets the entire grid of values, by row then column.

        The outer list contains all rows from zero to rowCount. Each inner list contains all columns from zero to columnCount. Where there is no value for a cell, the value is null.

        The returned list is immutable, except for the List.set(int, Object) method on the inner list, which adds, updates or deletes from the underlying grid.

        Specified by:
        rows in interface Grid<V>
        Returns:
        the entire grid, by row then column, not null
      • column

        public java.util.List<V> column​(int column)
        Description copied from interface: Grid
        Gets the rows of a single column.

        The list will contain all rows from zero to rowCount. Where data is not present, the list will contain null.

        The returned list is immutable, except for List.set(int, Object), which adds, updates or deletes from the underlying grid.

        Specified by:
        column in interface Grid<V>
        Parameters:
        column - the column, zero or greater
        Returns:
        the rows of the specified column, not null
      • columns

        public java.util.List<java.util.List<V>> columns()
        Description copied from interface: Grid
        Gets the entire grid of values, by column then row.

        The outer list contains all columns from zero to columnCount. Each inner list contains all rows from zero to rowCount. Where there is no value for a cell, the value is null.

        The returned list is immutable, except for the List.set(int, Object) method on the inner list, which adds, updates or deletes from the underlying grid.

        Specified by:
        columns in interface Grid<V>
        Returns:
        the entire grid, by row then column, not null
      • clear

        public void clear()
        Description copied from interface: Grid
        Clears the grid.

        The grid will be empty after calling this method.

        Specified by:
        clear in interface Grid<V>
      • put

        public void put​(int row,
                        int column,
                        V value)
        Description copied from interface: Grid
        Puts a value into this grid.

        The value at the specified row-column is set. Any previous value at the row-column is replaced.

        If either index does not exist, IndexOutOfBoundsException is thrown.

        Specified by:
        put in interface Grid<V>
        Parameters:
        row - the row, zero or greater
        column - the column, zero or greater
        value - the value to put into the grid, not null
      • putAll

        public void putAll​(Grid<? extends V> grid)
        Description copied from interface: Grid
        Puts all cells from a grid into this grid.

        The value at the specified row-column is set. Any previous value at the row-column is replaced.

        Specified by:
        putAll in interface Grid<V>
        Parameters:
        grid - the grid to put into this grid, not null
      • remove

        public boolean remove​(int row,
                              int column)
        Description copied from interface: Grid
        Removes the value at the specified row-column.

        If either index does not exist, no action occurs and false is returned.

        Specified by:
        remove in interface Grid<V>
        Parameters:
        row - the row
        column - the column
        Returns:
        true if the grid is altered
      • equals

        public boolean equals​(java.lang.Object obj)
        Description copied from interface: Grid
        Checks if this grid equals another grid.

        Two grids are equal if they are the same size and contain the same set of cells.

        Specified by:
        equals in interface Grid<V>
        Parameters:
        obj - the object to compare to, null returns false
        Returns:
        true if equal
      • hashCode

        public int hashCode()
        Description copied from interface: Grid
        Gets a suitable hash code.

        The hash code is rowCount ^ Integer.rotateLeft(columnCount, 16) ^ cells.hashCode().

        Specified by:
        hashCode in interface Grid<V>
        Returns:
        the hash code
      • exists

        public boolean exists​(int row,
                              int column)
        Description copied from interface: Grid
        Checks if the specified row-column exists.

        This simply checks that the row and column indices are between zero and the row and column counts.

        Specified by:
        exists in interface Grid<V>
        Parameters:
        row - the row
        column - the column
        Returns:
        true if the row-column exists
      • isFull

        public boolean isFull()
        Description copied from interface: Grid
        Checks if the grid is full.

        A full grid has a cell at every combination of row and column.

        Specified by:
        isFull in interface Grid<V>
        Returns:
        true if full
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: Grid
        Checks if the grid is empty.
        Specified by:
        isEmpty in interface Grid<V>
        Returns:
        true if empty
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object