com.google.common.collect.ForwardingSetMultimap Java Examples

The following examples show how to use com.google.common.collect.ForwardingSetMultimap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CheckedSetMultimap.java    From FreeBuilder with Apache License 2.0 4 votes vote down vote up
@Override
public void addTo(SourceBuilder code) {
  code.addLine("")
      .addLine("/**")
      .addLine(" * A multimap implementation that delegates to a provided put method")
      .addLine(" * to perform entry validation and insertion into a backing multimap.")
      .addLine(" */")
      .addLine("private static class %s<K, V> extends %s<K, V> {",
          TYPE, ForwardingSetMultimap.class)
      .addLine("")
      .addLine("  private final %s<K, V> multimap;", SetMultimap.class)
      .addLine("  private final %s<K, V> put;", BiConsumer.class)
      .addLine("")
      .addLine("  %s(%s<K, V> multimap, %s<K, V> put) {",
          TYPE, SetMultimap.class, BiConsumer.class)
      .addLine("    this.multimap = multimap;")
      .addLine("    this.put = put;")
      .addLine("  }")
      .addLine("")
      .addLine("  @Override protected %s<K, V> delegate() {", SetMultimap.class)
      .addLine("    return multimap;")
      .addLine("  }")
      .addLine("")
      .addLine("  @Override public boolean put(%1$s K key, %1$s V value) {", Jsr305.nullable())
      .addLine("    put.accept(key, value);")
      .addLine("    return true;")
      .addLine("  }")
      .addLine("")
      .addLine("  @Override public boolean putAll(%s K key, %s<? extends V> values) {",
          Jsr305.nullable(), Iterable.class)
      .addLine("    boolean anyModified = false;")
      .addLine("    for (V value : values) {")
      .addLine("      put.accept(key, value);")
      .addLine("      anyModified = true;")
      .addLine("    }")
      .addLine("    return anyModified;")
      .addLine("  }")
      .addLine("")
      .addLine("  @Override public boolean putAll(%s<? extends K, ? extends V> multimap) {",
          Multimap.class)
      .addLine("    boolean anyModified = false;")
      .addLine("    for (%s<? extends K, ? extends V> entry : multimap.entries()) {",
          Map.Entry.class)
      .addLine("      put.accept(entry.getKey(), entry.getValue());")
      .addLine("      anyModified = true;")
      .addLine("    }")
      .addLine("    return anyModified;")
      .addLine("  }")
      .addLine("")
      .addLine("  @Override")
      .addLine("  public %s<V> replaceValues(%s K key, %s<? extends V> values) {",
          Set.class, Jsr305.nullable(), Iterable.class)
      .addLine("    %s.checkNotNull(values);", Preconditions.class)
      .addLine("    %s<V> result = removeAll(key);", Set.class)
      .addLine("    putAll(key, values);")
      .addLine("    return result;")
      .addLine("  }")
      .addLine("")
      .addLine("  @Override public %s<V> get(%s K key) {", Set.class, Jsr305.nullable())
      .addLine("    return new %s<>(", CheckedSet.TYPE)
      .addLine("        multimap.get(key), value -> put.accept(key, value));")
      .addLine("  }")
      .addLine("")
      .addLine("  @Override public %s<K, %s<V>> asMap() {", Map.class, Collection.class)
      .addLine("    return %s.transformEntries(%s.asMap(multimap), (key, values) -> ",
          Maps.class, Multimaps.class)
      .addLine("        (%s<V>) new %s<>(", Collection.class, CheckedSet.TYPE)
      .addLine("            values, value -> put.accept(key, value)));")
      .addLine("  }")
      .addLine("}");
}