Class ImmutableTable.Builder<R,C,V>
- java.lang.Object
-
- com.google.common.collect.ImmutableTable.Builder<R,C,V>
-
- Enclosing class:
- ImmutableTable<R,C,V>
@DoNotMock public static final class ImmutableTable.Builder<R,C,V> extends Object
A builder for creating immutable table instances, especiallypublic 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") .buildOrThrow();
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(com.google.common.collect.Table<? extends R, ? extends C, ? extends V>)
following theTable.cellSet()
iteration order. However, iforderRowsBy(java.util.Comparator<? super R>)
ororderColumnsBy(java.util.Comparator<? super C>)
is called, the views are sorted by the supplied comparators.For empty or single-cell immutable tables,
ImmutableTable.of()
andImmutableTable.of(Object, Object, Object)
are even more convenient.Builder instances can be reused - it is safe to call
buildOrThrow()
multiple times to build multiple tables in series. Each table is a superset of the tables created before it.- Since:
- 11.0
-
-
Constructor Summary
Constructors Constructor Description Builder()
Creates a new builder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ImmutableTable<R,C,V>
build()
Returns a newly-created immutable table.ImmutableTable<R,C,V>
buildOrThrow()
Returns a newly-created immutable table, or throws an exception if duplicate key pairs were added.ImmutableTable.Builder<R,C,V>
orderColumnsBy(Comparator<? super C> columnComparator)
Specifies the ordering of the generated table's columns.ImmutableTable.Builder<R,C,V>
orderRowsBy(Comparator<? super R> rowComparator)
Specifies the ordering of the generated table's rows.ImmutableTable.Builder<R,C,V>
put(Table.Cell<? extends R,? extends C,? extends V> cell)
Adds the givencell
to the table, making it immutable if necessary.ImmutableTable.Builder<R,C,V>
put(R rowKey, C columnKey, V value)
Associates the (rowKey
,columnKey
) pair withvalue
in the built table.ImmutableTable.Builder<R,C,V>
putAll(Table<? extends R,? extends C,? extends V> table)
Associates all of the given table's keys and values in the built table.
-
-
-
Constructor Detail
-
Builder
public Builder()
Creates a new builder. The returned builder is equivalent to the builder generated byImmutableTable.builder()
.
-
-
Method Detail
-
orderRowsBy
@CanIgnoreReturnValue public ImmutableTable.Builder<R,C,V> orderRowsBy(Comparator<? super R> rowComparator)
Specifies the ordering of the generated table's rows.
-
orderColumnsBy
@CanIgnoreReturnValue public ImmutableTable.Builder<R,C,V> orderColumnsBy(Comparator<? super C> columnComparator)
Specifies the ordering of the generated table's columns.
-
put
@CanIgnoreReturnValue public ImmutableTable.Builder<R,C,V> put(R rowKey, C columnKey, V value)
Associates the (rowKey
,columnKey
) pair withvalue
in the built table. Duplicate key pairs are not allowed and will causebuild()
to fail.
-
put
@CanIgnoreReturnValue public ImmutableTable.Builder<R,C,V> put(Table.Cell<? extends R,? extends C,? extends V> cell)
Adds the givencell
to the table, making it immutable if necessary. Duplicate key pairs are not allowed and will causebuild()
to fail.
-
putAll
@CanIgnoreReturnValue public ImmutableTable.Builder<R,C,V> putAll(Table<? extends R,? extends C,? extends V> table)
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 causebuild()
to fail.- Throws:
NullPointerException
- if any key or value intable
is null
-
build
public ImmutableTable<R,C,V> build()
Returns a newly-created immutable table.Prefer the equivalent method
buildOrThrow()
to make it explicit that the method will throw an exception if there are duplicate key pairs. Thebuild()
method will soon be deprecated.- Throws:
IllegalArgumentException
- if duplicate key pairs were added
-
buildOrThrow
public ImmutableTable<R,C,V> buildOrThrow()
Returns a newly-created immutable table, or throws an exception if duplicate key pairs were added.- Throws:
IllegalArgumentException
- if duplicate key pairs were added- Since:
- 31.0
-
-