Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2007 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.collect;
     18 
     19 import com.google.common.annotations.GwtCompatible;
     20 
     21 import java.util.Collection;
     22 import java.util.Map;
     23 import java.util.Set;
     24 
     25 import javax.annotation.Nullable;
     26 
     27 /**
     28  * Basic implementation of the {@link SetMultimap} interface. It's a wrapper
     29  * around {@link AbstractMultimap} that converts the returned collections into
     30  * {@code Sets}. The {@link #createCollection} method must return a {@code Set}.
     31  *
     32  * @author Jared Levy
     33  */
     34 @GwtCompatible
     35 abstract class AbstractSetMultimap<K, V>
     36     extends AbstractMultimap<K, V> implements SetMultimap<K, V> {
     37   /**
     38    * Creates a new multimap that uses the provided map.
     39    *
     40    * @param map place to store the mapping from each key to its corresponding
     41    *     values
     42    */
     43   protected AbstractSetMultimap(Map<K, Collection<V>> map) {
     44     super(map);
     45   }
     46 
     47   @Override abstract Set<V> createCollection();
     48 
     49   @Override public Set<V> get(@Nullable K key) {
     50     return (Set<V>) super.get(key);
     51   }
     52 
     53   @Override public Set<Map.Entry<K, V>> entries() {
     54     return (Set<Map.Entry<K, V>>) super.entries();
     55   }
     56 
     57   @Override public Set<V> removeAll(@Nullable Object key) {
     58     return (Set<V>) super.removeAll(key);
     59   }
     60 
     61   /**
     62    * {@inheritDoc}
     63    *
     64    * <p>Any duplicates in {@code values} will be stored in the multimap once.
     65    */
     66   @Override public Set<V> replaceValues(
     67       @Nullable K key, Iterable<? extends V> values) {
     68     return (Set<V>) super.replaceValues(key, values);
     69   }
     70 
     71   /**
     72    * Stores a key-value pair in the multimap.
     73    *
     74    * @param key key to store in the multimap
     75    * @param value value to store in the multimap
     76    * @return {@code true} if the method increased the size of the multimap, or
     77    *     {@code false} if the multimap already contained the key-value pair
     78    */
     79   @Override public boolean put(K key, V value) {
     80     return super.put(key, value);
     81   }
     82 
     83   /**
     84    * Compares the specified object to this multimap for equality.
     85    *
     86    * <p>Two {@code SetMultimap} instances are equal if, for each key, they
     87    * contain the same values. Equality does not depend on the ordering of keys
     88    * or values.
     89    */
     90   @Override public boolean equals(@Nullable Object object) {
     91     return super.equals(object);
     92   }
     93 
     94   private static final long serialVersionUID = 7431625294878419160L;
     95 }
     96