1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors 3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License. 6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at 7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software 11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and 14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License. 15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect; 18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible; 20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection; 22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Map; 23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Set; 24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable; 26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/** 28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Basic implementation of the {@link SetMultimap} interface. It's a wrapper 29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * around {@link AbstractMultimap} that converts the returned collections into 30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code Sets}. The {@link #createCollection} method must return a {@code Set}. 31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy 33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@GwtCompatible 35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonabstract class AbstractSetMultimap<K, V> 36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson extends AbstractMultimap<K, V> implements SetMultimap<K, V> { 37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Creates a new multimap that uses the provided map. 39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param map place to store the mapping from each key to its corresponding 41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * values 42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson protected AbstractSetMultimap(Map<K, Collection<V>> map) { 44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson super(map); 45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override abstract Set<V> createCollection(); 48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Following Javadoc copied from SetMultimap. 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@inheritDoc} 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Because a {@code SetMultimap} has unique values for a given key, this 551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * method returns a {@link Set}, instead of the {@link Collection} specified 561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * in the {@link Multimap} interface. 571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public Set<V> get(@Nullable K key) { 59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (Set<V>) super.get(key); 60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@inheritDoc} 641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Because a {@code SetMultimap} has unique values for a given key, this 661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * method returns a {@link Set}, instead of the {@link Collection} specified 671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * in the {@link Multimap} interface. 681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public Set<Map.Entry<K, V>> entries() { 70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (Set<Map.Entry<K, V>>) super.entries(); 71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@inheritDoc} 751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Because a {@code SetMultimap} has unique values for a given key, this 771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * method returns a {@link Set}, instead of the {@link Collection} specified 781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * in the {@link Multimap} interface. 791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public Set<V> removeAll(@Nullable Object key) { 81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (Set<V>) super.removeAll(key); 82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@inheritDoc} 86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Because a {@code SetMultimap} has unique values for a given key, this 881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * method returns a {@link Set}, instead of the {@link Collection} specified 891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * in the {@link Multimap} interface. 901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Any duplicates in {@code values} will be stored in the multimap once. 92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public Set<V> replaceValues( 94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Nullable K key, Iterable<? extends V> values) { 95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (Set<V>) super.replaceValues(key, values); 96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@inheritDoc} 1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Though the method signature doesn't say so explicitly, the returned map 1021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * has {@link Set} values. 1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 1041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public Map<K, Collection<V>> asMap() { 1051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return super.asMap(); 1061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Stores a key-value pair in the multimap. 110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param key key to store in the multimap 112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param value value to store in the multimap 113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return {@code true} if the method increased the size of the multimap, or 114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code false} if the multimap already contained the key-value pair 115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public boolean put(K key, V value) { 117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return super.put(key, value); 118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Compares the specified object to this multimap for equality. 122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Two {@code SetMultimap} instances are equal if, for each key, they 124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * contain the same values. Equality does not depend on the ordering of keys 125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * or values. 126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public boolean equals(@Nullable Object object) { 128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return super.equals(object); 129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static final long serialVersionUID = 7431625294878419160L; 132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson} 133