View Javadoc
1   /*
2    * Copyright (C) 2013 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.collect;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.base.Objects;
21  import com.google.common.base.Predicate;
22  import com.google.common.base.Predicates;
23  import com.google.j2objc.annotations.Weak;
24  import java.util.AbstractCollection;
25  import java.util.Collection;
26  import java.util.Iterator;
27  import java.util.Map;
28  import java.util.Map.Entry;
29  import javax.annotation.Nullable;
30  
31  /**
32   * Implementation for {@link FilteredMultimap#values()}.
33   *
34   * @author Louis Wasserman
35   */
36  @GwtCompatible
37  final class FilteredMultimapValues<K, V> extends AbstractCollection<V> {
38    @Weak private final FilteredMultimap<K, V> multimap;
39  
40    FilteredMultimapValues(FilteredMultimap<K, V> multimap) {
41      this.multimap = checkNotNull(multimap);
42    }
43  
44    @Override
45    public Iterator<V> iterator() {
46      return Maps.valueIterator(multimap.entries().iterator());
47    }
48  
49    @Override
50    public boolean contains(@Nullable Object o) {
51      return multimap.containsValue(o);
52    }
53  
54    @Override
55    public int size() {
56      return multimap.size();
57    }
58  
59    @Override
60    public boolean remove(@Nullable Object o) {
61      Predicate<? super Entry<K, V>> entryPredicate = multimap.entryPredicate();
62      for (Iterator<Entry<K, V>> unfilteredItr = multimap.unfiltered().entries().iterator();
63          unfilteredItr.hasNext();
64          ) {
65        Map.Entry<K, V> entry = unfilteredItr.next();
66        if (entryPredicate.apply(entry) && Objects.equal(entry.getValue(), o)) {
67          unfilteredItr.remove();
68          return true;
69        }
70      }
71      return false;
72    }
73  
74    @Override
75    public boolean removeAll(Collection<?> c) {
76      return Iterables.removeIf(
77          multimap.unfiltered().entries(),
78          // explicit <Entry<K, V>> is required to build with JDK6
79          Predicates.<Entry<K, V>>and(
80              multimap.entryPredicate(), Maps.<V>valuePredicateOnEntries(Predicates.in(c))));
81    }
82  
83    @Override
84    public boolean retainAll(Collection<?> c) {
85      return Iterables.removeIf(
86          multimap.unfiltered().entries(),
87          // explicit <Entry<K, V>> is required to build with JDK6
88          Predicates.<Entry<K, V>>and(
89              multimap.entryPredicate(),
90              Maps.<V>valuePredicateOnEntries(Predicates.not(Predicates.in(c)))));
91    }
92  
93    @Override
94    public void clear() {
95      multimap.clear();
96    }
97  }