public void testCreateCopyEmptyTable() {
   Table<String, Integer, Character> original = HashBasedTable.create();
   try {
     ArrayTable.create(original);
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
Example #2
0
 @Override
 protected Map<String, Map<Integer, Character>> makePopulatedMap() {
   Table<Integer, String, Character> table = HashBasedTable.create();
   table.put(1, "foo", 'a');
   table.put(1, "bar", 'b');
   table.put(3, "foo", 'c');
   return Tables.unmodifiableTable(table).columnMap();
 }
Example #3
0
 @Override
 protected Map<String, Map<Integer, Character>> makePopulatedMap() {
   Table<Integer, String, String> table = HashBasedTable.create();
   table.put(1, "foo", "apple");
   table.put(1, "bar", "banana");
   table.put(3, "foo", "cat");
   return Tables.transformValues(table, FIRST_CHARACTER).columnMap();
 }
Example #4
0
 @Override
 protected Map<String, Map<Integer, Character>> makePopulatedMap() {
   Table<String, Integer, Character> table = HashBasedTable.create();
   table.put("foo", 1, 'a');
   table.put("bar", 1, 'b');
   table.put("foo", 3, 'c');
   return Tables.unmodifiableTable(table).rowMap();
 }
Example #5
0
 @Override
 protected Map<String, Integer> makePopulatedMap() {
   Table<String, Character, Integer> table = HashBasedTable.create();
   table.put("one", 'a', 1);
   table.put("two", 'a', 2);
   table.put("three", 'a', 3);
   table.put("four", 'b', 4);
   return Tables.unmodifiableTable(table).column('a');
 }
Example #6
0
 @Override
 protected Map<String, Integer> makePopulatedMap() {
   Table<String, Character, Integer> table = HashBasedTable.create();
   table.put("one", 'a', 1);
   table.put("two", 'a', 2);
   table.put("three", 'a', 3);
   table.put("four", 'b', 4);
   return Tables.transformValues(table, DIVIDE_BY_2).column('a');
 }
Example #7
0
 @Override
 protected Map<String, Integer> makePopulatedMap() {
   Table<Character, String, Integer> table = HashBasedTable.create();
   table.put('a', "one", 1);
   table.put('a', "two", 2);
   table.put('a', "three", 3);
   table.put('b', "four", 4);
   return Tables.unmodifiableTable(table).row('a');
 }
Example #8
0
 @Override
 protected Map<String, Integer> makePopulatedMap() {
   Table<Character, String, Integer> table = HashBasedTable.create();
   table.put('a', "one", 2);
   table.put('a', "two", 4);
   table.put('a', "three", 6);
   table.put('b', "four", 8);
   return Tables.transformValues(table, DIVIDE_BY_2).row('a');
 }
Example #9
0
 @Test
 public void testTable() {
   HashBasedTable<Integer, Integer, Integer> table = HashBasedTable.create();
   table.put(1, 1, 1);
   table.put(1, 2, 2);
   table.put(1, 3, 3);
   table.put(2, 1, 4);
   table.put(2, 2, 5);
   table.put(2, 3, 6);
   assert table.containsRow(1);
   assert table.containsColumn(2);
   assert table.row(1).get(3) == 3;
 }
  @Test
  public void whenCreateTable_thenCreated() {
    final Table<String, String, Integer> distance = HashBasedTable.create();
    distance.put("London", "Paris", 340);
    distance.put("New York", "Los Angeles", 3940);
    distance.put("London", "New York", 5576);

    assertEquals(3940, distance.get("New York", "Los Angeles").intValue());
    assertThat(distance.columnKeySet(), containsInAnyOrder("Paris", "New York", "Los Angeles"));
    assertThat(distance.rowKeySet(), containsInAnyOrder("London", "New York"));
  }
  @Test
  public void whenTransposeTable_thenCorrect() {
    final Table<String, String, Integer> distance = HashBasedTable.create();
    distance.put("London", "Paris", 340);
    distance.put("New York", "Los Angeles", 3940);
    distance.put("London", "New York", 5576);

    final Table<String, String, Integer> transposed = Tables.transpose(distance);
    assertThat(transposed.rowKeySet(), containsInAnyOrder("Paris", "New York", "Los Angeles"));
    assertThat(transposed.columnKeySet(), containsInAnyOrder("London", "New York"));
  }
  private static class ProductAssociativityGraph {
    private final Table<Vertex, Vertex, Edge> graph = HashBasedTable.create();

    public static ProductAssociativityGraph create() {
      return new ProductAssociativityGraph();
    }

    public void addAssociation(Vertex v1, Vertex v2, int edgeWeight) {
      if (containsAssociation(v1, v2)) {
        Edge edge = getAssociationWeight(v1, v2);
        edge.addWeight(edgeWeight);
      } else {
        graph.put(v1, v2, new Edge(edgeWeight));
      }
    }

    public Edge getAssociationWeight(Vertex v1, Vertex v2) {
      Edge edge = graph.get(v1, v2);
      if (edge == null) {
        edge = graph.get(v2, v1);
      }
      return edge;
    }

    public List<Vertex> getProductAssociations(Vertex v) {
      Map<Vertex, Edge> adjacent = Maps.newHashMap(graph.row(v));
      adjacent.putAll(graph.column(v));

      return adjacent
          .entrySet()
          .stream()
          .sorted((e1, e2) -> e2.getValue().weight - e1.getValue().weight)
          .map(Map.Entry::getKey)
          .collect(Collectors.toList());
    }

    public boolean containsAssociation(Vertex v1, Vertex v2) {
      return graph.contains(v1, v2) || graph.contains(v2, v1);
    }

    public void clear() {
      graph.clear();
    }

    @Override
    public String toString() {
      return "ProductAssociativityGraph{" + "graph=" + graph + '}';
    }
  }
 public void testCreateCopyHashBasedTable() {
   Table<String, Integer, Character> original = HashBasedTable.create();
   original.put("foo", 1, 'a');
   original.put("bar", 1, 'b');
   original.put("foo", 3, 'c');
   Table<String, Integer, Character> copy = ArrayTable.create(original);
   assertEquals(4, copy.size());
   assertEquals((Character) 'a', copy.get("foo", 1));
   assertEquals((Character) 'b', copy.get("bar", 1));
   assertEquals((Character) 'c', copy.get("foo", 3));
   assertNull(copy.get("bar", 3));
   original.put("foo", 1, 'd');
   assertEquals((Character) 'd', original.get("foo", 1));
   assertEquals((Character) 'a', copy.get("foo", 1));
   assertEquals(copy.rowKeySet(), ImmutableSet.of("foo", "bar"));
   assertEquals(copy.columnKeySet(), ImmutableSet.of(1, 3));
 }
  @Override
  public void testEquals() {
    table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
    Table<String, Integer, Character> hashCopy = HashBasedTable.create();
    hashCopy.put("foo", 1, 'a');
    hashCopy.put("bar", 1, 'b');
    hashCopy.put("foo", 3, 'c');
    Table<String, Integer, Character> reordered =
        create("foo", 3, 'c', "foo", 1, 'a', "bar", 1, 'b');
    Table<String, Integer, Character> smaller = create("foo", 1, 'a', "bar", 1, 'b');
    Table<String, Integer, Character> swapOuter =
        create("bar", 1, 'a', "foo", 1, 'b', "bar", 3, 'c');
    Table<String, Integer, Character> swapValues =
        create("foo", 1, 'c', "bar", 1, 'b', "foo", 3, 'a');

    new EqualsTester()
        .addEqualityGroup(table, reordered)
        .addEqualityGroup(hashCopy)
        .addEqualityGroup(smaller)
        .addEqualityGroup(swapOuter)
        .addEqualityGroup(swapValues)
        .testEquals();
  }
Example #15
0
 public Builder() {
   subDocuments = HashBasedTable.create();
   structures = HashMultimap.create();
   indexBySubDoc = Maps.newHashMap();
   build = false;
 }
Example #16
0
 @Override
 Table<String, Character, Integer> makeTable() {
   Table<String, Character, Integer> table = HashBasedTable.create();
   return Tables.transformValues(table, DIVIDE_BY_2);
 }
Example #17
0
 @Override
 Table<String, Character, Integer> makeTable() {
   Table<String, Character, Integer> table = HashBasedTable.create();
   return Tables.unmodifiableTable(table);
 }
Example #18
0
 @Override
 Table<Integer, String, Character> makeTable() {
   return HashBasedTable.create();
 }
Example #19
0
 @Override
 Table<Integer, String, Character> makeTable() {
   Table<Integer, String, String> original = HashBasedTable.create();
   return Tables.transformValues(original, FIRST_CHARACTER);
 }
Example #20
0
 @Override
 Table<Integer, String, Character> makeTable() {
   Table<Integer, String, Character> original = HashBasedTable.create();
   return Tables.unmodifiableTable(original);
 }