Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 The Guava Authors
   *
   * 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 com.google.common.collect;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
 import java.util.List;
 import java.util.Map;
 
 import  javax.annotation.Nullable;

An immutable Table with reliable user-specified iteration order. Does not permit null keys or values.

Note: Although this class is not final, it cannot be subclassed as it has no public or protected constructors. Thus, instances of this class are guaranteed to be immutable.

See the Guava User Guide article on immutable collections.

Author(s):
Gregory Kick
Since:
11.0
 
 // TODO(gak): make serializable
 public abstract class ImmutableTable<R, C, V> implements Table<R, C, V> {
  
Returns an empty immutable table.
 
   @SuppressWarnings("unchecked")
   public static final <R, C, V> ImmutableTable<R, C, V> of() {
     return (ImmutableTable<R, C, V>) .;
   }

  
Returns an immutable table containing a single cell.
 
   public static final <R, C, V> ImmutableTable<R, C, V> of(R rowKey,
       C columnKey, V value) {
     return new SingletonImmutableTable<R, C, V>(rowKeycolumnKeyvalue);
   }

  
Returns an immutable copy of the provided table.

The Table.cellSet() iteration order of the provided table determines the iteration ordering of all views in the returned table. Note that some views of the original table and the copied table may have different iteration orders. For more control over the ordering, create a Builder and call Builder.orderRowsBy, Builder.orderColumnsBy, and Builder.putAll

Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. The exact circumstances under which a copy will or will not be performed are undocumented and subject to change.

 
   public static final <R, C, V> ImmutableTable<R, C, V> copyOf(
       Table<? extends R, ? extends C, ? extends V> table) {
     if (table instanceof ImmutableTable) {
       @SuppressWarnings("unchecked")
       ImmutableTable<R, C, V> parameterizedTable
           = (ImmutableTable<R, C, V>) table;
       return parameterizedTable;
     } else {
       int size = table.size();
       switch (size) {
         case 0:
           return of();
         case 1:
           Cell<? extends R, ? extends C, ? extends V> onlyCell
               = Iterables.getOnlyElement(table.cellSet());
           return ImmutableTable.<R, C, V>of(onlyCell.getRowKey(),
               onlyCell.getColumnKey(), onlyCell.getValue());
         default:
           ImmutableSet.Builder<Cell<R, C, V>> cellSetBuilder
               = ImmutableSet.builder();
           for (Cell<? extends R, ? extends C, ? extends V> cell :
               table.cellSet()) {
             /*
              * Must cast to be able to create a Cell<R, C, V> rather than a
              * Cell<? extends R, ? extends C, ? extends V>
              */
             cellSetBuilder.add(cellOf((R) cell.getRowKey(),
                (C) cell.getColumnKey(), (V) cell.getValue()));
          }
          return RegularImmutableTable.forCells(cellSetBuilder.build());
      }
    }
  }

  
Returns a new builder. The generated builder is equivalent to the builder created by the Builder.ImmutableTable.Builder() constructor.
  public static final <R, C, V> Builder<R, C, V> builder() {
    return new Builder<R, C, V>();
  }

  
Verifies that rowKey, columnKey and value are non-null, and returns a new entry with those values.
  static <R, C, V> Cell<R, C, V> cellOf(R rowKey, C columnKey, V value) {
    return Tables.immutableCell(checkNotNull(rowKey), checkNotNull(columnKey),
        checkNotNull(value));
  }

  
A builder for creating immutable table instances, especially public static final tables ("constant tables"). Example:
   static final ImmutableTable<Integer, Character, String> SPREADSHEET =
       new ImmutableTable.Builder<Integer, Character, String>()
           .put(1, 'A', "foo")
           .put(1, 'B', "bar")
           .put(2, 'A', "baz")
           .build();

By default, the order in which cells are added to the builder determines the iteration ordering of all views in the returned table, with putAll following the Table.cellSet() iteration order. However, if orderRowsBy or orderColumnsBy is called, the views are sorted by the supplied comparators. For empty or single-cell immutable tables, of() and of(Object, Object, Object) are even more convenient.

Builder instances can be reused - it is safe to call build multiple times to build multiple tables in series. Each table is a superset of the tables created before it.

Since:
11.0
  public static final class Builder<R, C, V> {
    private final List<Cell<R, C, V>> cells = Lists.newArrayList();
    private Comparator<? super R> rowComparator;
    private Comparator<? super C> columnComparator;

    
Creates a new builder. The returned builder is equivalent to the builder generated by ImmutableTable.builder.
    public Builder() {}

    
Specifies the ordering of the generated table's rows.
    public Builder<R, C, V> orderRowsBy(Comparator<? super R> rowComparator) {
      this. = checkNotNull(rowComparator);
      return this;
    }

    
Specifies the ordering of the generated table's columns.
    public Builder<R, C, V> orderColumnsBy(
        Comparator<? super C> columnComparator) {
      this. = checkNotNull(columnComparator);
      return this;
    }

    
Associates the (rowKey, columnKey) pair with value in the built table. Duplicate key pairs are not allowed and will cause build to fail.
    public Builder<R, C, V> put(R rowKey, C columnKey, V value) {
      .add(cellOf(rowKeycolumnKeyvalue));
      return this;
    }

    
Adds the given cell to the table, making it immutable if necessary. Duplicate key pairs are not allowed and will cause build to fail.
    public Builder<R, C, V> put(
        Cell<? extends R, ? extends C, ? extends V> cell) {
      if (cell instanceof Tables.ImmutableCell) {
        checkNotNull(cell.getRowKey());
        checkNotNull(cell.getColumnKey());
        checkNotNull(cell.getValue());
        @SuppressWarnings("unchecked"// all supported methods are covariant
        Cell<R, C, V> immutableCell = (Cell<R, C, V>) cell;
        .add(immutableCell);
      } else {
        put(cell.getRowKey(), cell.getColumnKey(), cell.getValue());
      }
      return this;
    }

    
Associates all of the given table's keys and values in the built table. Duplicate row key column key pairs are not allowed, and will cause build to fail.

Throws:
NullPointerException if any key or value in table is null
    public Builder<R, C, V> putAll(
        Table<? extends R, ? extends C, ? extends V> table) {
      for (Cell<? extends R, ? extends C, ? extends V> cell : table.cellSet()) {
        put(cell);
      }
      return this;
    }

    
Returns a newly-created immutable table.

Throws:
IllegalArgumentException if duplicate key pairs were added
    public ImmutableTable<R, C, V> build() {
      int size = .size();
      switch (size) {
        case 0:
          return of();
        case 1:
          return new SingletonImmutableTable<R, C, V>(
              Iterables.getOnlyElement());
        default:
         return RegularImmutableTable.forCells(
             );
      }
    }
  }
  @Override public abstract ImmutableSet<Cell<R, C, V>> cellSet();

  

Throws:
NullPointerException if columnKey is null
  @Override public abstract ImmutableMap<R, V> column(C columnKey);
  @Override public abstract ImmutableSet<C> columnKeySet();

  

The value Map<R, V> instances in the returned map are ImmutableMap instances as well.

  @Override public abstract ImmutableMap<C, Map<R, V>> columnMap();

  

Throws:
NullPointerException if rowKey is null
  @Override public abstract ImmutableMap<C, V> row(R rowKey);
  @Override public abstract ImmutableSet<R> rowKeySet();

  

The value Map<C, V> instances in the returned map are ImmutableMap instances as well.

  @Override public abstract ImmutableMap<R, Map<C, V>> rowMap();

  
Guaranteed to throw an exception and leave the table unmodified.

Deprecated:
Unsupported operation.
Throws:
UnsupportedOperationException always
  @Deprecated @Override public final void clear() {
    throw new UnsupportedOperationException();
  }

  
Guaranteed to throw an exception and leave the table unmodified.

Deprecated:
Unsupported operation.
Throws:
UnsupportedOperationException always
  @Deprecated @Override public final V put(R rowKey, C columnKey, V value) {
    throw new UnsupportedOperationException();
  }

  
Guaranteed to throw an exception and leave the table unmodified.

Deprecated:
Unsupported operation.
Throws:
UnsupportedOperationException always
  @Deprecated @Override public final void putAll(
      Table<? extends R, ? extends C, ? extends V> table) {
    throw new UnsupportedOperationException();
  }

  
Guaranteed to throw an exception and leave the table unmodified.

Deprecated:
Unsupported operation.
Throws:
UnsupportedOperationException always
  @Deprecated @Override public final V remove(Object rowKeyObject columnKey) {
    throw new UnsupportedOperationException();
  }
  @Override public boolean equals(@Nullable Object obj) {
    if (obj == this) {
      return true;
    } else if (obj instanceof Table) {
      Table<?, ?, ?> that = (Table<?, ?, ?>) obj;
      return this.cellSet().equals(that.cellSet());
    } else {
      return false;
    }
  }
  @Override public int hashCode() {
    return cellSet().hashCode();
  }
  @Override public String toString() {
    return rowMap().toString();
  }
New to GrepCode? Check out our FAQ X