@SuppressWarnings("unchecked")
    static <V> DenseImmutableGrid<V> create(Grid<? extends V> grid) {
        if (grid instanceof DenseGrid) {
            return new DenseImmutableGrid<V>((DenseGrid<V>) grid);
        }
        int rowCount = grid.rowCount();
        int columnCount = grid.columnCount();
        validateCounts(rowCount, columnCount);
        V[] values = (V[]) new Object[rowCount * columnCount];
        for (Cell<? extends V> cell : grid.cells()) {
            values[cell.getRow() * columnCount + cell.getColumn()] = cell.getValue();
        }
        return new DenseImmutableGrid<V>(rowCount, columnCount, grid.size(), values);
    }