Esempio n. 1
0
 public void testConstrainedMultimapQueue() {
   Multimap<String, Integer> multimap =
       Multimaps.newMultimap(new HashMap<String, Collection<Integer>>(), new QueueSupplier());
   Multimap<String, Integer> constrained =
       MapConstraints.constrainedMultimap(multimap, TEST_CONSTRAINT);
   constrained.put("foo", 1);
   assertTrue(constrained.get("foo").contains(1));
   assertTrue(multimap.get("foo").contains(1));
   try {
     constrained.put(TEST_KEY, 1);
     fail("TestKeyException expected");
   } catch (TestKeyException expected) {
   }
   try {
     constrained.put("foo", TEST_VALUE);
     fail("TestValueException expected");
   } catch (TestValueException expected) {
   }
   try {
     constrained.get("foo").add(TEST_VALUE);
     fail("TestKeyException expected");
   } catch (TestValueException expected) {
   }
   try {
     constrained.get(TEST_KEY).add(1);
     fail("TestValueException expected");
   } catch (TestKeyException expected) {
   }
   assertEquals(1, constrained.size());
   assertEquals(1, multimap.size());
 }
Esempio n. 2
0
 public void testNefariousMultimapPutAllMultimap() {
   Multimap<String, Integer> multimap = LinkedListMultimap.create();
   Multimap<String, Integer> constrained =
       MapConstraints.constrainedMultimap(multimap, TEST_CONSTRAINT);
   Multimap<String, Integer> onceIterable = Multimaps.forMap(onceIterableMap("foo", 1));
   constrained.putAll(onceIterable);
   assertEquals(ImmutableList.of(1), constrained.get("foo"));
 }
Esempio n. 3
0
  public String toGraphviz() {
    StringBuilder builder = new StringBuilder();

    builder.append("digraph QuantileDigest {\n").append("\tgraph [ordering=\"out\"];");

    final List<Node> nodes = new ArrayList<>();
    postOrderTraversal(
        root,
        new Callback() {
          @Override
          public boolean process(Node node) {
            nodes.add(node);
            return true;
          }
        });

    Multimap<Integer, Node> nodesByLevel =
        Multimaps.index(
            nodes,
            new Function<Node, Integer>() {
              @Override
              public Integer apply(Node input) {
                return input.level;
              }
            });

    for (Map.Entry<Integer, Collection<Node>> entry : nodesByLevel.asMap().entrySet()) {
      builder.append("\tsubgraph level_" + entry.getKey() + " {\n").append("\t\trank = same;\n");

      for (Node node : entry.getValue()) {
        builder.append(
            String.format(
                "\t\t%s [label=\"[%s..%s]@%s\\n%s\", shape=rect, style=filled,color=%s];\n",
                idFor(node),
                node.getLowerBound(),
                node.getUpperBound(),
                node.level,
                node.weightedCount,
                node.weightedCount > 0 ? "salmon2" : "white"));
      }

      builder.append("\t}\n");
    }

    for (Node node : nodes) {
      if (node.left != null) {
        builder.append(format("\t%s -> %s;\n", idFor(node), idFor(node.left)));
      }
      if (node.right != null) {
        builder.append(format("\t%s -> %s;\n", idFor(node), idFor(node.right)));
      }
    }

    builder.append("}\n");

    return builder.toString();
  }
Esempio n. 4
0
 SplitDocument(
     int id,
     @Nonnull DocStructure structure,
     @Nonnull Table<SubDocType, Integer, SubDocument> subDocuments,
     @Nonnull Multimap<SubDocType, DocStructure> structures) {
   this.id = id;
   this.root = structure;
   this.subDocuments = Tables.unmodifiableTable(subDocuments);
   this.structures = Multimaps.unmodifiableMultimap(structures);
 }
  @Test
  public void whenGroupListUsingMultimap_thenGrouped() {
    final List<String> names = Lists.newArrayList("John", "Adam", "Tom");
    final Function<String, Integer> function =
        new Function<String, Integer>() {
          @Override
          public final Integer apply(final String input) {
            return input.length();
          }
        };
    final Multimap<Integer, String> groups = Multimaps.index(names, function);

    assertThat(groups.get(3), containsInAnyOrder("Tom"));
    assertThat(groups.get(4), containsInAnyOrder("John", "Adam"));
  }
 @Override
 public boolean equals(@Nullable Object object) {
   return Multimaps.equalsImpl(this, object);
 }
 public void testOrderingSynchronized() {
   Multimap<String, Integer> multimap = initializeMultimap5();
   assertOrderingReadOnly(Multimaps.synchronizedMultimap(multimap));
 }
 public void testOrderingUnmodifiable() {
   Multimap<String, Integer> multimap = initializeMultimap5();
   assertOrderingReadOnly(Multimaps.unmodifiableMultimap(multimap));
 }
Esempio n. 9
0
 public OptiqSchema(OptiqSchema parent, final Schema schema, String name) {
   this.parent = parent;
   this.schema = schema;
   this.name = name;
   assert (parent == null) == (this instanceof OptiqRootSchema);
   //noinspection unchecked
   this.compositeTableMap =
       CompositeMap.of(
           Maps.transformValues(
               tableMap,
               new com.google.common.base.Function<TableEntry, Table>() {
                 public Table apply(TableEntry input) {
                   return input.getTable();
                 }
               }),
           Maps.transformValues(
               Multimaps.filterEntries(
                       functionMap,
                       new Predicate<Map.Entry<String, FunctionEntry>>() {
                         public boolean apply(Map.Entry<String, FunctionEntry> entry) {
                           final Function function = entry.getValue().getFunction();
                           return function instanceof TableMacro
                               && function.getParameters().isEmpty();
                         }
                       })
                   .asMap(),
               new com.google.common.base.Function<Collection<FunctionEntry>, Table>() {
                 public Table apply(Collection<FunctionEntry> input) {
                   // At most one function with zero parameters.
                   final TableMacro tableMacro =
                       (TableMacro) input.iterator().next().getFunction();
                   return tableMacro.apply(ImmutableList.of());
                 }
               }),
           Compatible.INSTANCE.asMap(
               schema.getTableNames(),
               new com.google.common.base.Function<String, Table>() {
                 public Table apply(String input) {
                   return schema.getTable(input);
                 }
               }));
   // TODO: include schema's functions in this map.
   this.compositeFunctionMap =
       Multimaps.transformValues(
           functionMap,
           new com.google.common.base.Function<FunctionEntry, Function>() {
             public Function apply(FunctionEntry input) {
               return input.getFunction();
             }
           });
   //noinspection unchecked
   this.compositeSubSchemaMap =
       CompositeMap.of(
           subSchemaMap,
           Compatible.INSTANCE.asMap(
               schema.getSubSchemaNames(),
               new com.google.common.base.Function<String, OptiqSchema>() {
                 public OptiqSchema apply(String name) {
                   return add(name, schema.getSubSchema(name));
                 }
               }));
 }