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

acscommons.com.google.common.collect.AbstractMultimap Maven / Gradle / Ivy

There is a newer version: 6.9.4
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 acscommons.com.google.common.collect;

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

import acscommons.com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.WeakOuter;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * A skeleton {@code Multimap} implementation, not necessarily in terms of a {@code Map}.
 *
 * @author Louis Wasserman
 */
@GwtCompatible
@ElementTypesAreNonnullByDefault
abstract class AbstractMultimap
    implements Multimap {
  @Override
  public boolean isEmpty() {
    return size() == 0;
  }

  @Override
  public boolean containsValue(@CheckForNull Object value) {
    for (Collection collection : asMap().values()) {
      if (collection.contains(value)) {
        return true;
      }
    }

    return false;
  }

  @Override
  public boolean containsEntry(@CheckForNull Object key, @CheckForNull Object value) {
    Collection collection = asMap().get(key);
    return collection != null && collection.contains(value);
  }

  @CanIgnoreReturnValue
  @Override
  public boolean remove(@CheckForNull Object key, @CheckForNull Object value) {
    Collection collection = asMap().get(key);
    return collection != null && collection.remove(value);
  }

  @CanIgnoreReturnValue
  @Override
  public boolean put(@ParametricNullness K key, @ParametricNullness V value) {
    return get(key).add(value);
  }

  @CanIgnoreReturnValue
  @Override
  public boolean putAll(@ParametricNullness K key, Iterable values) {
    checkNotNull(values);
    // make sure we only call values.iterator() once
    // and we only call get(key) if values is nonempty
    if (values instanceof Collection) {
      Collection valueCollection = (Collection) values;
      return !valueCollection.isEmpty() && get(key).addAll(valueCollection);
    } else {
      Iterator valueItr = values.iterator();
      return valueItr.hasNext() && Iterators.addAll(get(key), valueItr);
    }
  }

  @CanIgnoreReturnValue
  @Override
  public boolean putAll(Multimap multimap) {
    boolean changed = false;
    for (Entry entry : multimap.entries()) {
      changed |= put(entry.getKey(), entry.getValue());
    }
    return changed;
  }

  @CanIgnoreReturnValue
  @Override
  public Collection replaceValues(@ParametricNullness K key, Iterable values) {
    checkNotNull(values);
    Collection result = removeAll(key);
    putAll(key, values);
    return result;
  }

  @LazyInit @CheckForNull private transient Collection> entries;

  @Override
  public Collection> entries() {
    Collection> result = entries;
    return (result == null) ? entries = createEntries() : result;
  }

  abstract Collection> createEntries();

  @WeakOuter
  class Entries extends Multimaps.Entries {
    @Override
    Multimap multimap() {
      return AbstractMultimap.this;
    }

    @Override
    public Iterator> iterator() {
      return entryIterator();
    }

    @Override
    public Spliterator> spliterator() {
      return entrySpliterator();
    }
  }

  @WeakOuter
  class EntrySet extends Entries implements Set> {
    @Override
    public int hashCode() {
      return Sets.hashCodeImpl(this);
    }

    @Override
    public boolean equals(@CheckForNull Object obj) {
      return Sets.equalsImpl(this, obj);
    }
  }

  abstract Iterator> entryIterator();

  Spliterator> entrySpliterator() {
    return Spliterators.spliterator(
        entryIterator(), size(), (this instanceof SetMultimap) ? Spliterator.DISTINCT : 0);
  }

  @LazyInit @CheckForNull private transient Set keySet;

  @Override
  public Set keySet() {
    Set result = keySet;
    return (result == null) ? keySet = createKeySet() : result;
  }

  abstract Set createKeySet();

  @LazyInit @CheckForNull private transient Multiset keys;

  @Override
  public Multiset keys() {
    Multiset result = keys;
    return (result == null) ? keys = createKeys() : result;
  }

  abstract Multiset createKeys();

  @LazyInit @CheckForNull private transient Collection values;

  @Override
  public Collection values() {
    Collection result = values;
    return (result == null) ? values = createValues() : result;
  }

  abstract Collection createValues();

  @WeakOuter
  class Values extends AbstractCollection {
    @Override
    public Iterator iterator() {
      return valueIterator();
    }

    @Override
    public Spliterator spliterator() {
      return valueSpliterator();
    }

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

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

    @Override
    public void clear() {
      AbstractMultimap.this.clear();
    }
  }

  Iterator valueIterator() {
    return Maps.valueIterator(entries().iterator());
  }

  Spliterator valueSpliterator() {
    return Spliterators.spliterator(valueIterator(), size(), 0);
  }

  @LazyInit @CheckForNull private transient Map> asMap;

  @Override
  public Map> asMap() {
    Map> result = asMap;
    return (result == null) ? asMap = createAsMap() : result;
  }

  abstract Map> createAsMap();

  // Comparison and hashing

  @Override
  public boolean equals(@CheckForNull Object object) {
    return Multimaps.equalsImpl(this, object);
  }

  /**
   * Returns the hash code for this multimap.
   *
   * 

The hash code of a multimap is defined as the hash code of the map view, as returned by * {@link Multimap#asMap}. * * @see Map#hashCode */ @Override public int hashCode() { return asMap().hashCode(); } /** * Returns a string representation of the multimap, generated by calling {@code toString} on the * map returned by {@link Multimap#asMap}. * * @return a string representation of the multimap */ @Override public String toString() { return asMap().toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy