All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.joda.collect.grid.ImmutableGrid Maven / Gradle / Ivy

The newest version!
/*
 *  Copyright 2014-present Stephen Colebourne
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.joda.collect.grid;

/**
 * Immutable implementation of the {@code Grid} data structure.
 * 
 * @param  the type of the value
 * @author Stephen Colebourne
 */
public abstract class ImmutableGrid extends AbstractGrid {

    /**
     * Obtains an empty immutable grid with zero row-column count.
     * 
     * @param  the type of the value
     * @return the empty immutable grid, not null
     */
    public static  ImmutableGrid of() {
        return new EmptyGrid();
    }

    /**
     * Obtains an empty immutable grid of the specified row-column count.
     * 
     * @param  the type of the value
     * @param rowCount  the number of rows, zero or greater
     * @param columnCount  the number of columns, zero or greater
     * @return the empty immutable grid, not null
     */
    public static  ImmutableGrid of(int rowCount, int columnCount) {
        return new EmptyGrid(rowCount, columnCount);
    }

    /**
     * Obtains an immutable grid with row-column count 1x1 and a single cell.
     * 

* The single cell is at row zero column zero. * * @param the type of the value * @param value the value of the single cell, not null * @return the empty immutable grid, not null */ public static ImmutableGrid of(R value) { return new SingletonGrid(1, 1, 0, 0, value); } /** * Obtains an immutable grid of the specified row-column count with a single cell. * * @param the type of the value * @param rowCount the number of rows, zero or greater * @param columnCount the number of columns, zero or greater * @param row the row of the single cell, zero or greater * @param column the column of the single cell, zero or greater * @param value the value of the single cell, not null * @return the empty immutable grid, not null */ public static ImmutableGrid of(int rowCount, int columnCount, int row, int column, R value) { return new SingletonGrid(rowCount, columnCount, row, column, value); } //----------------------------------------------------------------------- /** * Obtains an immutable grid with one cell. * * @param the type of the value * @param rowCount the number of rows, zero or greater * @param columnCount the number of columns, zero or greater * @param cell the cell that the grid should contain, not null * @return the immutable grid, not null * @throws IndexOutOfBoundsException if either index is less than zero */ public static ImmutableGrid copyOf(int rowCount, int columnCount, Cell cell) { if (cell == null) { throw new IllegalArgumentException("Cell must not be null"); } return new SingletonGrid(rowCount, columnCount, cell); } /** * Obtains an immutable grid by copying a set of cells. * * @param the type of the value * @param rowCount the number of rows, zero or greater * @param columnCount the number of columns, zero or greater * @param cells the cells to copy, not null * @return the immutable grid, not null * @throws IndexOutOfBoundsException if either index is less than zero */ public static ImmutableGrid copyOf(int rowCount, int columnCount, Iterable> cells) { if (cells == null) { throw new IllegalArgumentException("Cells must not be null"); } if (!cells.iterator().hasNext()) { return new EmptyGrid(rowCount, columnCount); } return new SparseImmutableGrid(rowCount, columnCount, cells); } /** * Obtains an immutable grid by copying a set of cells, deriving the row and column count. *

* The row and column counts are calculated as the maximum row and column specified. * * @param the type of the value * @param cells the cells to copy, not null * @return the immutable grid, not null * @throws IndexOutOfBoundsException if either index is less than zero */ public static ImmutableGrid copyOfDeriveCounts(Iterable> cells) { if (cells == null) { throw new IllegalArgumentException("Cells must not be null"); } if (!cells.iterator().hasNext()) { return new EmptyGrid(); } int rowCount = 0; int columnCount = 0; for (Cell cell : cells) { rowCount = Math.max(rowCount, cell.getRow()); columnCount = Math.max(columnCount, cell.getColumn()); } return new SparseImmutableGrid(rowCount + 1, columnCount + 1, cells); } //----------------------------------------------------------------------- /** * Obtains an immutable grid by copying another grid. *

* If you need to change the row-column count, use {@link #copyOf(int, int, Iterable)} * passing in the set of cells from the grid. * * @param the type of the value * @param grid the grid to copy, not null * @return the immutable grid, not null * @throws IndexOutOfBoundsException if either index is less than zero */ public static ImmutableGrid copyOf(Grid grid) { if (grid == null) { throw new IllegalArgumentException("Grid must not be null"); } if (grid instanceof ImmutableGrid) { return (ImmutableGrid) grid; } validateCounts(grid.rowCount(), grid.columnCount()); if (grid.size() == 0) { return new EmptyGrid(grid.rowCount(), grid.columnCount()); } if (grid.size() == 1) { Cell cell = grid.cells().iterator().next(); return new SingletonGrid(grid.rowCount(), grid.columnCount(), cell); } if (grid.size() >= (grid.rowCount() * grid.columnCount() / 2)) { return DenseImmutableGrid.create(grid); } return new SparseImmutableGrid(grid); } //----------------------------------------------------------------------- /** * Restricted constructor. */ ImmutableGrid() { } //----------------------------------------------------------------------- /** * {@inheritDoc} * @deprecated Grid is read-only */ @Deprecated @Override public void clear() { throw new UnsupportedOperationException("Grid is read-only"); } /** * {@inheritDoc} * @deprecated Grid is read-only */ @Deprecated @Override public void put(int row, int column, V value) { throw new UnsupportedOperationException("Grid is read-only"); } /** * {@inheritDoc} * @deprecated Grid is read-only */ @Deprecated @Override public void putAll(Grid grid) { throw new UnsupportedOperationException("Grid is read-only"); } /** * {@inheritDoc} * @deprecated Grid is read-only */ @Deprecated @Override public boolean remove(int row, int column) { throw new UnsupportedOperationException("Grid is read-only"); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy