Refine search
void put(R row, C column, V value, BinaryOperator<V> merger) { MutableCell<R, C, V> oldCell = table.get(row, column); if (oldCell == null) { MutableCell<R, C, V> cell = new MutableCell<>(row, column, value); insertionOrder.add(cell); table.put(row, column, cell); } else { oldCell.merge(value, merger); } }
@Override public boolean contains(Object rowKey, Object columnKey) { return fromTable.contains(rowKey, columnKey); }
private static <R, C, V> void merge( Table<R, C, V> table, R row, C column, V value, BinaryOperator<V> mergeFunction) { checkNotNull(value); V oldValue = table.get(row, column); if (oldValue == null) { table.put(row, column, value); } else { V newValue = mergeFunction.apply(oldValue, value); if (newValue == null) { table.remove(row, column); } else { table.put(row, column, newValue); } } }
private ArrayTable(Table<R, C, V> table) { this(table.rowKeySet(), table.columnKeySet()); putAll(table); }
public void testIterationOrder() { Table<String, String, String> table = HashBasedTable.create(); for (int i = 0; i < 5; i++) { table.put("r" + i, "c" + i, "v" + i); } assertThat(table.rowKeySet()).containsExactly("r0", "r1", "r2", "r3", "r4").inOrder(); assertThat(table.columnKeySet()).containsExactly("c0", "c1", "c2", "c3", "c4").inOrder(); assertThat(table.values()).containsExactly("v0", "v1", "v2", "v3", "v4").inOrder(); }
public void testCreateCopyArrayTable() { Table<String, Integer, Character> original = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); Table<String, Integer, Character> copy = ArrayTable.create(original); assertEquals(original, copy); original.put("foo", 1, 'd'); assertEquals((Character) 'd', original.get("foo", 1)); assertEquals((Character) 'a', copy.get("foo", 1)); assertEquals(copy.rowKeySet(), original.rowKeySet()); assertEquals(copy.columnKeySet(), original.columnKeySet()); }
public void testRowClearAndPut() { if (supportsRemove()) { table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); Map<Integer, Character> row = table.row("foo"); assertEquals(ImmutableMap.of(1, 'a', 3, 'c'), row); table.remove("foo", 3); assertEquals(ImmutableMap.of(1, 'a'), row); table.remove("foo", 1); assertEquals(ImmutableMap.of(), row); table.put("foo", 2, 'b'); assertEquals(ImmutableMap.of(2, 'b'), row); row.clear(); assertEquals(ImmutableMap.of(), row); table.put("foo", 5, 'x'); assertEquals(ImmutableMap.of(5, 'x'), row); } } }
@Override protected Table<String, Integer, Character> create(Object... data) { Table<String, Integer, String> table = HashBasedTable.create(); checkArgument(data.length % 3 == 0); for (int i = 0; i < data.length; i += 3) { String value = (data[i + 2] == null) ? null : (data[i + 2] + "transformed"); table.put((String) data[i], (Integer) data[i + 1], value); } return Tables.transformValues(table, FIRST_CHARACTER); }
@Override public Set<Cell<String, Integer, Character>> create(Object... elements) { Table<String, Integer, Character> table = createTable(); for (Object element : elements) { @SuppressWarnings("unchecked") Cell<String, Integer, Character> cell = (Cell<String, Integer, Character>) element; table.put(cell.getRowKey(), cell.getColumnKey(), cell.getValue()); } return table.cellSet(); }
public void testCreateExplicitComparators() { table = TreeBasedTable.create(Collections.reverseOrder(), Ordering.usingToString()); table.put("foo", 3, 'a'); table.put("foo", 12, 'b'); table.put("bar", 5, 'c'); table.put("cat", 8, 'd'); assertThat(table.rowKeySet()).containsExactly("foo", "cat", "bar").inOrder(); assertThat(table.row("foo").keySet()).containsExactly(12, 3).inOrder(); }
@Override public Set<Cell<String, Integer, Character>> create(Object... elements) { Table<String, Integer, Character> table = HashBasedTable.create(); for (Object element : elements) { @SuppressWarnings("unchecked") Cell<String, Integer, Character> cell = (Cell<String, Integer, Character>) element; table.put(cell.getRowKey(), cell.getColumnKey(), cell.getValue()); } return Tables.unmodifiableTable(table).cellSet(); } })
@Override public V2 get(Object rowKey, Object columnKey) { // The function is passed a null input only when the table contains a null // value. return contains(rowKey, columnKey) ? function.apply(fromTable.get(rowKey, columnKey)) : null; }