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

org.joda.collect.grid.SingletonGrid 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;

import java.io.Serializable;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

/**
 * Immutable implementation of the {@code Grid} data structure storing one cell.
 * 
 * @param  the type of the value
 * @author Stephen Colebourne
 */
final class SingletonGrid extends ImmutableGrid implements Serializable {

    /** Serialization version. */
    private static final long serialVersionUID = 1L;

    /**
     * The row count.
     */
    private final int rowCount;
    /**
     * The column count.
     */
    private final int columnCount;
    /**
     * The cell.
     */
    private final ImmutableCell cell;

    /**
     * Restricted constructor.
     */
    SingletonGrid(int rowCount, int columnCount, Cell cell) {
        validateCounts(rowCount, columnCount);
        this.rowCount = rowCount;
        this.columnCount = columnCount;
        this.cell = ImmutableCell.copyOf(cell).validateCounts(rowCount, columnCount);
    }

    /**
     * Restricted constructor.
     */
    SingletonGrid(int rowCount, int columnCount, int row, int column, V value) {
        validateCounts(rowCount, columnCount);
        this.rowCount = rowCount;
        this.columnCount = columnCount;
        this.cell = ImmutableCell.of(row, column, value).validateCounts(rowCount, columnCount);
    }

    //-----------------------------------------------------------------------
    @Override
    public int rowCount() {
        return rowCount;
    }

    @Override
    public int columnCount() {
        return columnCount;
    }

    //-----------------------------------------------------------------------
    @Override
    public int size() {
        return 1;
    }

    @Override
    public boolean contains(int row, int column) {
        return cell.equalRowColumn(row, column);
    }

    @Override
    public boolean containsValue(Object valueToFind) {
        return cell.equalValue(valueToFind);
    }

    @Override
    public V get(int row, int column) {
        return cell.equalRowColumn(row, column) ? cell.getValue() : null;
    }

    @Override
    public Cell cell(int row, int column) {
        return cell.equalRowColumn(row, column) ? cell : null;
    }

    //-----------------------------------------------------------------------
    @Override
    public ImmutableSet> cells() {
        return ImmutableSet.>of(cell);
    }

    @Override
    public ImmutableList values() {
        return ImmutableList.of(cell.getValue());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy