/** * Verifies that {@code rowKey}, {@code columnKey} and {@code 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, "rowKey"), checkNotNull(columnKey, "columnKey"), checkNotNull(value, "value")); }
@Override public SampleElements<Cell<String, Integer, Character>> samples() { return new SampleElements<>( Tables.immutableCell("bar", 1, 'a'), Tables.immutableCell("bar", 2, 'b'), Tables.immutableCell("foo", 3, 'c'), Tables.immutableCell("bar", 1, 'b'), Tables.immutableCell("cat", 2, 'b')); }
@Override public SampleElements<Cell<String, Integer, Character>> samples() { return new SampleElements<>( Tables.immutableCell("bar", 1, 'a'), Tables.immutableCell("bar", 2, 'b'), Tables.immutableCell("bar", 3, (Character) null), Tables.immutableCell("bar", 4, 'b'), Tables.immutableCell("bar", 5, 'b')); }
@Override Spliterator<Cell<R, C, V>> cellSpliterator() { return CollectSpliterators.flatMap( backingMap.entrySet().spliterator(), (Entry<R, Map<C, V>> rowEntry) -> CollectSpliterators.map( rowEntry.getValue().entrySet().spliterator(), (Entry<C, V> columnEntry) -> Tables.immutableCell( rowEntry.getKey(), columnEntry.getKey(), columnEntry.getValue())), Spliterator.DISTINCT | Spliterator.SIZED, size()); }
@Override public Cell<R, C, V2> apply(Cell<R, C, V1> cell) { return immutableCell( cell.getRowKey(), cell.getColumnKey(), function.apply(cell.getValue())); } };
public void testToImmutableTableMergingNullMerge() { Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>> collector = ImmutableTable.toImmutableTable( Cell::getRowKey, Cell::getColumnKey, Cell::getValue, (v1, v2) -> null); try { Stream.of(Tables.immutableCell("one", "uno", 1), Tables.immutableCell("one", "uno", 2)) .collect(collector); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } }
public void testToImmutableTableConflict() { Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>> collector = ImmutableTable.toImmutableTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue); try { Stream.of(Tables.immutableCell("one", "uno", 1), Tables.immutableCell("one", "uno", 2)) .collect(collector); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException 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 testImmutableEntryToString() { Cell<String, Integer, Character> entry = Tables.immutableCell("foo", 1, 'a'); assertEquals("(foo,1)=a", entry.toString()); Cell<String, Integer, Character> nullEntry = Tables.immutableCell(null, null, null); assertEquals("(null,null)=null", nullEntry.toString()); }
@Override public Cell<?, ?, ?> apply(Cell<?, ?, ?> cell) { return immutableCell(cell.getColumnKey(), cell.getRowKey(), cell.getValue()); } };
public void testEntryEqualsNull() { Cell<String, Integer, Character> entry = Tables.immutableCell(null, null, null); new EqualsTester() .addEqualityGroup(entry, Tables.immutableCell(null, null, null)) .addEqualityGroup(Tables.immutableCell("bar", null, null)) .addEqualityGroup(Tables.immutableCell(null, 2, null)) .addEqualityGroup(Tables.immutableCell(null, null, 'b')) .addEqualityGroup(Tables.immutableCell("foo", 1, 'a')) .testEquals(); } }
public void testEntryEquals() { Cell<String, Integer, Character> entry = Tables.immutableCell("foo", 1, 'a'); new EqualsTester() .addEqualityGroup(entry, Tables.immutableCell("foo", 1, 'a')) .addEqualityGroup(Tables.immutableCell("bar", 1, 'a')) .addEqualityGroup(Tables.immutableCell("foo", 2, 'a')) .addEqualityGroup(Tables.immutableCell("foo", 1, 'b')) .addEqualityGroup(Tables.immutableCell(null, null, null)) .testEquals(); }
public void testForCells() { assertTrue(RegularImmutableTable.forCells(CELLS) instanceof DenseImmutableTable<?, ?, ?>); assertTrue( RegularImmutableTable.forCells( ImmutableSet.of( Tables.immutableCell('a', 1, "blah"), Tables.immutableCell('b', 2, "blah"), Tables.immutableCell('c', 3, "blah"))) instanceof SparseImmutableTable<?, ?, ?>); }
public void testToImmutableTableNullColumnKey() { Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>> collector = ImmutableTable.toImmutableTable(Cell::getRowKey, t -> null, Cell::getValue); try { Stream.of(Tables.immutableCell("one", "uno", 1)).collect(collector); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } }
public void testToImmutableTableMergingNullRowKey() { Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>> collector = ImmutableTable.toImmutableTable( t -> null, Cell::getColumnKey, Cell::getValue, Integer::sum); try { Stream.of(Tables.immutableCell("one", "uno", 1)).collect(collector); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } }
public void testToImmutableTableMergingNullColumnKey() { Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>> collector = ImmutableTable.toImmutableTable(Cell::getRowKey, t -> null, Cell::getValue, Integer::sum); try { Stream.of(Tables.immutableCell("one", "uno", 1)).collect(collector); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } }
public void testToImmutableTableNullRowKey() { Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>> collector = ImmutableTable.toImmutableTable(t -> null, Cell::getColumnKey, Cell::getValue); try { Stream.of(Tables.immutableCell("one", "uno", 1)).collect(collector); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } }
public void testCellReflectsChanges() { table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); Cell<String, Integer, Character> cell = table.cellSet().iterator().next(); assertEquals(Tables.immutableCell("foo", 1, 'a'), cell); assertEquals((Character) 'a', table.put("foo", 1, 'd')); assertEquals(Tables.immutableCell("foo", 1, 'd'), cell); }
@GwtIncompatible // SerializableTester public void testImmutableEntrySerialization() { Cell<String, Integer, Character> entry = Tables.immutableCell("foo", 1, 'a'); SerializableTester.reserializeAndAssert(entry); }
public void testBuilder_withImmutableCell() { ImmutableTable.Builder<Character, Integer, String> builder = new ImmutableTable.Builder<>(); assertEquals( ImmutableTable.of('a', 1, "foo"), builder.put(Tables.immutableCell('a', 1, "foo")).build()); }