package org.glassfish.jersey.internal.guava;
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 static org.glassfish.jersey.internal.guava.Preconditions.checkNotNull;
abstract class AbstractMultimap<K, V> implements Multimap<K, V> {
private transient Collection<Entry<K, V>> entries;
private transient Set<K> keySet;
private transient Collection<V> values;
private transient Map<K, Collection<V>> asMap;
@Override
public boolean containsValue(Object value) {
for (Collection<V> collection : asMap().values()) {
if (collection.contains(value)) {
return true;
}
}
return false;
}
@Override
public boolean containsEntry(Object key, Object value) {
Collection<V> collection = asMap().get(key);
return collection != null && collection.contains(value);
}
@Override
public boolean remove(Object key, Object value) {
Collection<V> collection = asMap().get(key);
return collection != null && collection.remove(value);
}
@Override
public boolean put(K key, V value) {
return get(key).add(value);
}
@Override
public boolean putAll(K key, Iterable<? extends V> values) {
checkNotNull(values);
if (values instanceof Collection) {
Collection<? extends V> valueCollection = (Collection<? extends V>) values;
return !valueCollection.isEmpty() && get(key).addAll(valueCollection);
} else {
Iterator<? extends V> valueItr = values.iterator();
return valueItr.hasNext() && Iterators.addAll(get(key), valueItr);
}
}
@Override
public Collection<Entry<K, V>> entries() {
Collection<Entry<K, V>> result = entries;
return (result == null) ? entries = createEntries() : result;
}
private Collection<Entry<K, V>> createEntries() {
if (this instanceof SetMultimap) {
return new EntrySet();
} else {
return new Entries();
}
}
abstract Iterator<Entry<K, V>> entryIterator();
@Override
public Set<K> keySet() {
Set<K> result = keySet;
return (result == null) ? keySet = createKeySet() : result;
}
Set<K> createKeySet() {
return new Maps.KeySet<K, Collection<V>>(asMap());
}
@Override
public Collection<V> values() {
Collection<V> result = values;
return (result == null) ? values = createValues() : result;
}
private Collection<V> createValues() {
return new Values();
}
Iterator<V> valueIterator() {
return Maps.valueIterator(entries().iterator());
}
@Override
public Map<K, Collection<V>> asMap() {
Map<K, Collection<V>> result = asMap;
return (result == null) ? asMap = createAsMap() : result;
}
abstract Map<K, Collection<V>> createAsMap();
@Override
public boolean equals(Object object) {
return Multimaps.equalsImpl(this, object);
}
@Override
public int hashCode() {
return asMap().hashCode();
}
@Override
public String toString() {
return asMap().toString();
}
private class Entries extends Multimaps.Entries<K, V> {
@Override
Multimap<K, V> multimap() {
return AbstractMultimap.this;
}
@Override
public Iterator<Entry<K, V>> iterator() {
return entryIterator();
}
}
private class EntrySet extends Entries implements Set<Entry<K, V>> {
@Override
public int hashCode() {
return Sets.hashCodeImpl(this);
}
@Override
public boolean equals(Object obj) {
return Sets.equalsImpl(this, obj);
}
}
private class Values extends AbstractCollection<V> {
@Override
public Iterator<V> iterator() {
return valueIterator();
}
@Override
public int size() {
return AbstractMultimap.this.size();
}
@Override
public boolean contains(Object o) {
return AbstractMultimap.this.containsValue(o);
}
@Override
public void clear() {
AbstractMultimap.this.clear();
}
}
}