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

jersey.repackaged.com.google.common.collect.FilteredKeyMultimap Maven / Gradle / Ivy

There is a newer version: 8.16.0
Show newest version
/*
 * Copyright (C) 2012 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 jersey.repackaged.com.google.common.collect;

import static jersey.repackaged.com.google.common.base.Preconditions.checkNotNull;
import static jersey.repackaged.com.google.common.base.Preconditions.checkPositionIndex;

import jersey.repackaged.com.google.common.annotations.GwtCompatible;
import jersey.repackaged.com.google.common.base.Predicate;
import jersey.repackaged.com.google.common.base.Predicates;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Nullable;

/**
 * Implementation of {@link Multimaps#filterKeys(Multimap, Predicate)}.
 *
 * @author Louis Wasserman
 */
@GwtCompatible
class FilteredKeyMultimap extends FilteredMultimap {
  final Predicate keyPredicate;

  FilteredKeyMultimap(Multimap unfiltered, Predicate keyPredicate) {
    super(unfiltered);
    this.keyPredicate = checkNotNull(keyPredicate);
  }

  @Override
  Predicate> entryPredicate() {
    return Predicates.compose(keyPredicate, Maps.keyFunction());
  }

  @Override
  public int size() {
    int size = 0;
    for (Collection collection : asMap().values()) {
      size += collection.size();
    }
    return size;
  }

  @Override
  public boolean containsKey(@Nullable Object key) {
    if (unfiltered.containsKey(key)) {
      @SuppressWarnings("unchecked") // k is equal to a K, if not one itself
      K k = (K) key;
      return keyPredicate.apply(k);
    }
    return false;
  }

  @Override
  public Collection removeAll(Object key) {
    return containsKey(key) ? unfiltered.removeAll(key) : unmodifiableEmptyCollection();
  }

  Collection unmodifiableEmptyCollection() {
    if (unfiltered instanceof SetMultimap) {
      return ImmutableSet.of();
    } else {
      return ImmutableList.of();
    }
  }

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

  @Override
  Set createKeySet() {
    return Sets.filter(unfiltered.keySet(), keyPredicate);
  }

  @Override
  public Collection get(K key) {
    if (keyPredicate.apply(key)) {
      return unfiltered.get(key);
    } else if (unfiltered instanceof SetMultimap) {
      return new AddRejectingSet(key);
    } else {
      return new AddRejectingList(key);
    }
  }
  
  static class AddRejectingSet extends ForwardingSet {
    final K key;

    AddRejectingSet(K key) {
      this.key = key;
    }

    @Override
    public boolean add(V element) {
      throw new IllegalArgumentException("Key does not satisfy predicate: " + key);
    }

    @Override
    public boolean addAll(Collection collection) {
      checkNotNull(collection);
      throw new IllegalArgumentException("Key does not satisfy predicate: " + key);
    }

    @Override
    protected Set delegate() {
      return Collections.emptySet();
    }
  }

  static class AddRejectingList extends ForwardingList {
    final K key;

    AddRejectingList(K key) {
      this.key = key;
    }

    @Override
    public boolean add(V v) {
      add(0, v);
      return true;
    }

    @Override
    public boolean addAll(Collection collection) {
      addAll(0, collection);
      return true;
    }

    @Override
    public void add(int index, V element) {
      checkPositionIndex(index, 0);
      throw new IllegalArgumentException("Key does not satisfy predicate: " + key);
    }

    @Override
    public boolean addAll(int index, Collection elements) {
      checkNotNull(elements);
      checkPositionIndex(index, 0);
      throw new IllegalArgumentException("Key does not satisfy predicate: " + key);
    }

    @Override
    protected List delegate() {
      return Collections.emptyList();
    }
  }

  @Override
  Iterator> entryIterator() {
    return Iterators.filter(
        unfiltered.entries().iterator(), Predicates.compose(keyPredicate, Maps.keyFunction()));
  }

  @Override
  Collection> createEntries() {
    return new Multimaps.Entries() {
      @Override
      Multimap multimap() {
        return FilteredKeyMultimap.this;
      }

      @Override
      public Iterator> iterator() {
        return entryIterator();
      }
      
      @Override
      @SuppressWarnings("unchecked")
      public boolean remove(@Nullable Object o) {
        if (o instanceof Entry) {
          Entry entry = (Entry) o;
          if (unfiltered.containsEntry(entry.getKey(), entry.getValue())
              && keyPredicate.apply((K) entry.getKey())) {
            return unfiltered.remove(entry.getKey(), entry.getValue());
          }
        }
        return false;
      }
      
      @Override
      public boolean removeAll(Collection c) {
        Predicate> combinedPredicate = Predicates.and(
            Predicates.compose(keyPredicate, Maps.keyFunction()), Predicates.in(c));
        return Iterators.removeIf(unfiltered.entries().iterator(), combinedPredicate);
      }
      
      @Override
      public boolean retainAll(Collection c) {
        Predicate> combinedPredicate = Predicates.and(
            Predicates.compose(keyPredicate, Maps.keyFunction()), 
            Predicates.not(Predicates.in(c)));
        return Iterators.removeIf(unfiltered.entries().iterator(), combinedPredicate);
      }
    };
  }

  @Override
  Map> createAsMap() {
    return Maps.filterKeys(unfiltered.asMap(), keyPredicate);
  }
  
  @Override
  Multiset createKeys() {
    return Multisets.filter(unfiltered.keys(), keyPredicate);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy