/** * Returns a {@link Collector} that accumulates elements into a {@code Table} created using the * specified supplier, whose cells are generated by applying the provided mapping functions to the * input elements. Cells are inserted into the generated {@code Table} in encounter order. * * <p>If multiple input elements map to the same row and column, an {@code IllegalStateException} * is thrown when the collection operation is performed. * * @since 21.0 */ @Beta public static <T, R, C, V, I extends Table<R, C, V>> Collector<T, ?, I> toTable( java.util.function.Function<? super T, ? extends R> rowFunction, java.util.function.Function<? super T, ? extends C> columnFunction, java.util.function.Function<? super T, ? extends V> valueFunction, java.util.function.Supplier<I> tableSupplier) { return toTable( rowFunction, columnFunction, valueFunction, (v1, v2) -> { throw new IllegalStateException("Conflicting values " + v1 + " and " + v2); }, tableSupplier); }
/** * Returns a {@link Collector} that accumulates elements into a {@code Table} created using the * specified supplier, whose cells are generated by applying the provided mapping functions to the * input elements. Cells are inserted into the generated {@code Table} in encounter order. * * <p>If multiple input elements map to the same row and column, an {@code IllegalStateException} * is thrown when the collection operation is performed. * * @since 21.0 */ @Beta public static <T, R, C, V, I extends Table<R, C, V>> Collector<T, ?, I> toTable( java.util.function.Function<? super T, ? extends R> rowFunction, java.util.function.Function<? super T, ? extends C> columnFunction, java.util.function.Function<? super T, ? extends V> valueFunction, java.util.function.Supplier<I> tableSupplier) { return toTable( rowFunction, columnFunction, valueFunction, (v1, v2) -> { throw new IllegalStateException("Conflicting values " + v1 + " and " + v2); }, tableSupplier); }
public void testToTableNullValues() { Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = Tables.toTable( Cell::getRowKey, Cell::getColumnKey, Cell::getValue, () -> ArrayTable.create(ImmutableList.of("one"), ImmutableList.of("uno"))); try { Stream.of(Tables.immutableCell("one", "uno", (Integer) null)).collect(collector); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } }
public void testToTableConflict() { Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = Tables.toTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue, HashBasedTable::create); try { Stream.of(Tables.immutableCell("one", "uno", 1), Tables.immutableCell("one", "uno", 2)) .collect(collector); fail("Expected IllegalStateException"); } catch (IllegalStateException expected) { } }
public void testToTableNullMerge() { Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = Tables.toTable( Cell::getRowKey, Cell::getColumnKey, Cell::getValue, (Integer v1, Integer v2) -> null, HashBasedTable::create); Equivalence<Table<String, String, Integer>> equivalence = Equivalence.equals().<Cell<String, String, Integer>>pairwise().onResultOf(Table::cellSet); CollectorTester.of(collector, equivalence) .expectCollects( ImmutableTable.of(), Tables.immutableCell("one", "uno", 1), Tables.immutableCell("one", "uno", 2)); }
public void testToTable() { Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = Tables.toTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue, HashBasedTable::create); Equivalence<Table<String, String, Integer>> equivalence = Equivalence.equals().<Cell<String, String, Integer>>pairwise().onResultOf(Table::cellSet); CollectorTester.of(collector, equivalence) .expectCollects( new ImmutableTable.Builder<String, String, Integer>() .put("one", "uno", 1) .put("two", "dos", 2) .put("three", "tres", 3) .build(), Tables.immutableCell("one", "uno", 1), Tables.immutableCell("two", "dos", 2), Tables.immutableCell("three", "tres", 3)); }
public void testToTableMerging() { Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = Tables.toTable( Cell::getRowKey, Cell::getColumnKey, Cell::getValue, Integer::sum, HashBasedTable::create); Equivalence<Table<String, String, Integer>> equivalence = Equivalence.equals().<Cell<String, String, Integer>>pairwise().onResultOf(Table::cellSet); CollectorTester.of(collector, equivalence) .expectCollects( new ImmutableTable.Builder<String, String, Integer>() .put("one", "uno", 1) .put("two", "dos", 6) .put("three", "tres", 3) .build(), Tables.immutableCell("one", "uno", 1), Tables.immutableCell("two", "dos", 2), Tables.immutableCell("three", "tres", 3), Tables.immutableCell("two", "dos", 4)); }
/** * Returns a {@link Collector} that accumulates elements into a {@code Table} created using the * specified supplier, whose cells are generated by applying the provided mapping functions to the * input elements. Cells are inserted into the generated {@code Table} in encounter order. * * <p>If multiple input elements map to the same row and column, an {@code IllegalStateException} * is thrown when the collection operation is performed. * * @since 21.0 */ @Beta public static <T, R, C, V, I extends Table<R, C, V>> Collector<T, ?, I> toTable( java.util.function.Function<? super T, ? extends R> rowFunction, java.util.function.Function<? super T, ? extends C> columnFunction, java.util.function.Function<? super T, ? extends V> valueFunction, java.util.function.Supplier<I> tableSupplier) { return toTable( rowFunction, columnFunction, valueFunction, (v1, v2) -> { throw new IllegalStateException("Conflicting values " + v1 + " and " + v2); }, tableSupplier); }
/** * Returns a {@link Collector} that accumulates elements into a {@code Table} created using the * specified supplier, whose cells are generated by applying the provided mapping functions to the * input elements. Cells are inserted into the generated {@code Table} in encounter order. * * <p>If multiple input elements map to the same row and column, an {@code IllegalStateException} * is thrown when the collection operation is performed. * * @since 21.0 */ @Beta public static <T, R, C, V, I extends Table<R, C, V>> Collector<T, ?, I> toTable( java.util.function.Function<? super T, ? extends R> rowFunction, java.util.function.Function<? super T, ? extends C> columnFunction, java.util.function.Function<? super T, ? extends V> valueFunction, java.util.function.Supplier<I> tableSupplier) { return toTable( rowFunction, columnFunction, valueFunction, (v1, v2) -> { throw new IllegalStateException("Conflicting values " + v1 + " and " + v2); }, tableSupplier); }
public void testToTableNullValues() { Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = Tables.toTable( Cell::getRowKey, Cell::getColumnKey, Cell::getValue, () -> ArrayTable.create(ImmutableList.of("one"), ImmutableList.of("uno"))); try { Stream.of(Tables.immutableCell("one", "uno", (Integer) null)).collect(collector); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } }
public void testToTableConflict() { Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = Tables.toTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue, HashBasedTable::create); try { Stream.of(Tables.immutableCell("one", "uno", 1), Tables.immutableCell("one", "uno", 2)) .collect(collector); fail("Expected IllegalStateException"); } catch (IllegalStateException expected) { } }
public void testToTableNullMerge() { Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = Tables.toTable( Cell::getRowKey, Cell::getColumnKey, Cell::getValue, (Integer v1, Integer v2) -> null, HashBasedTable::create); Equivalence<Table<String, String, Integer>> equivalence = Equivalence.equals().<Cell<String, String, Integer>>pairwise().onResultOf(Table::cellSet); CollectorTester.of(collector, equivalence) .expectCollects( ImmutableTable.of(), Tables.immutableCell("one", "uno", 1), Tables.immutableCell("one", "uno", 2)); }
public void testToTable() { Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = Tables.toTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue, HashBasedTable::create); Equivalence<Table<String, String, Integer>> equivalence = Equivalence.equals().<Cell<String, String, Integer>>pairwise().onResultOf(Table::cellSet); CollectorTester.of(collector, equivalence) .expectCollects( new ImmutableTable.Builder<String, String, Integer>() .put("one", "uno", 1) .put("two", "dos", 2) .put("three", "tres", 3) .build(), Tables.immutableCell("one", "uno", 1), Tables.immutableCell("two", "dos", 2), Tables.immutableCell("three", "tres", 3)); }
public void testToTableMerging() { Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = Tables.toTable( Cell::getRowKey, Cell::getColumnKey, Cell::getValue, Integer::sum, HashBasedTable::create); Equivalence<Table<String, String, Integer>> equivalence = Equivalence.equals().<Cell<String, String, Integer>>pairwise().onResultOf(Table::cellSet); CollectorTester.of(collector, equivalence) .expectCollects( new ImmutableTable.Builder<String, String, Integer>() .put("one", "uno", 1) .put("two", "dos", 6) .put("three", "tres", 3) .build(), Tables.immutableCell("one", "uno", 1), Tables.immutableCell("two", "dos", 2), Tables.immutableCell("three", "tres", 3), Tables.immutableCell("two", "dos", 4)); }