Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2007 The Guava Authors
      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.List;
     23 import java.util.Map;
     24 
     25 import javax.annotation.Nullable;
     26 
     27 /**
     28  * Basic implementation of the {@link ListMultimap} interface. It's a wrapper
     29  * around {@link AbstractMultimap} that converts the returned collections into
     30  * {@code Lists}. The {@link #createCollection} method must return a {@code
     31  * List}.
     32  *
     33  * @author Jared Levy
     34  * @since 2.0 (imported from Google Collections Library)
     35  */
     36 @GwtCompatible
     37 abstract class AbstractListMultimap<K, V>
     38     extends AbstractMultimap<K, V> implements ListMultimap<K, V> {
     39   /**
     40    * Creates a new multimap that uses the provided map.
     41    *
     42    * @param map place to store the mapping from each key to its corresponding
     43    *     values
     44    */
     45   protected AbstractListMultimap(Map<K, Collection<V>> map) {
     46     super(map);
     47   }
     48 
     49   @Override abstract List<V> createCollection();
     50 
     51   // Following Javadoc copied from ListMultimap.
     52 
     53   /**
     54    * {@inheritDoc}
     55    *
     56    * <p>Because the values for a given key may have duplicates and follow the
     57    * insertion ordering, this method returns a {@link List}, instead of the
     58    * {@link Collection} specified in the {@link Multimap} interface.
     59    */
     60   @Override public List<V> get(@Nullable K key) {
     61     return (List<V>) super.get(key);
     62   }
     63 
     64   /**
     65    * {@inheritDoc}
     66    *
     67    * <p>Because the values for a given key may have duplicates and follow the
     68    * insertion ordering, this method returns a {@link List}, instead of the
     69    * {@link Collection} specified in the {@link Multimap} interface.
     70    */
     71   @Override public List<V> removeAll(@Nullable Object key) {
     72     return (List<V>) super.removeAll(key);
     73   }
     74 
     75   /**
     76    * {@inheritDoc}
     77    *
     78    * <p>Because the values for a given key may have duplicates and follow the
     79    * insertion ordering, this method returns a {@link List}, instead of the
     80    * {@link Collection} specified in the {@link Multimap} interface.
     81    */
     82   @Override public List<V> replaceValues(
     83       @Nullable K key, Iterable<? extends V> values) {
     84     return (List<V>) super.replaceValues(key, values);
     85   }
     86 
     87   /**
     88    * Stores a key-value pair in the multimap.
     89    *
     90    * @param key key to store in the multimap
     91    * @param value value to store in the multimap
     92    * @return {@code true} always
     93    */
     94   @Override public boolean put(@Nullable K key, @Nullable V value) {
     95     return super.put(key, value);
     96   }
     97 
     98   /**
     99    * {@inheritDoc}
    100    *
    101    * <p>Though the method signature doesn't say so explicitly, the returned map
    102    * has {@link List} values.
    103    */
    104   @Override public Map<K, Collection<V>> asMap() {
    105     return super.asMap();
    106   }
    107 
    108   /**
    109    * Compares the specified object to this multimap for equality.
    110    *
    111    * <p>Two {@code ListMultimap} instances are equal if, for each key, they
    112    * contain the same values in the same order. If the value orderings disagree,
    113    * the multimaps will not be considered equal.
    114    */
    115   @Override public boolean equals(@Nullable Object object) {
    116     return super.equals(object);
    117   }
    118 
    119   private static final long serialVersionUID = 6588350623831699109L;
    120 }
    121