All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.common.collect.FilteredMultimapValues Maven / Gradle / Ivy

Go to download

Guava is a suite of core and expanded libraries that include utility classes, Google's collections, I/O classes, and much more.

There is a newer version: 33.1.0-jre
Show newest version
/*
 * Copyright (C) 2013 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.common.collect;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.j2objc.annotations.Weak;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map.Entry;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * Implementation for {@link FilteredMultimap#values()}.
 *
 * @author Louis Wasserman
 */
@GwtCompatible
@ElementTypesAreNonnullByDefault
final class FilteredMultimapValues
    extends AbstractCollection {
  @Weak private final FilteredMultimap multimap;

  FilteredMultimapValues(FilteredMultimap multimap) {
    this.multimap = checkNotNull(multimap);
  }

  @Override
  public Iterator iterator() {
    return Maps.valueIterator(multimap.entries().iterator());
  }

  @Override
  public boolean contains(@CheckForNull Object o) {
    return multimap.containsValue(o);
  }

  @Override
  public int size() {
    return multimap.size();
  }

  @Override
  public boolean remove(@CheckForNull Object o) {
    Predicate> entryPredicate = multimap.entryPredicate();
    for (Iterator> unfilteredItr = multimap.unfiltered().entries().iterator();
        unfilteredItr.hasNext(); ) {
      Entry entry = unfilteredItr.next();
      if (entryPredicate.apply(entry) && Objects.equal(entry.getValue(), o)) {
        unfilteredItr.remove();
        return true;
      }
    }
    return false;
  }

  @Override
  public boolean removeAll(Collection c) {
    return Iterables.removeIf(
        multimap.unfiltered().entries(),
        // explicit > is required to build with JDK6
        Predicates.>and(
            multimap.entryPredicate(), Maps.valuePredicateOnEntries(Predicates.in(c))));
  }

  @Override
  public boolean retainAll(Collection c) {
    return Iterables.removeIf(
        multimap.unfiltered().entries(),
        // explicit > is required to build with JDK6
        Predicates.>and(
            multimap.entryPredicate(),
            Maps.valuePredicateOnEntries(Predicates.not(Predicates.in(c)))));
  }

  @Override
  public void clear() {
    multimap.clear();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy