Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.google.common.collect.MapConstraints Maven / Gradle / Ivy
Go to download
Google Collections Library is a suite of new collections and collection-related goodness for Java 5.0
/*
* Copyright (C) 2007 Google Inc.
*
* 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 java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* Factory and utilities pertaining to the {@code MapConstraint} interface.
*
* {@link MapConstraint}, {@code Map} and {@code Multimap} instances returned
* by this class are serializable.
*
* @see Constraints
* @author Mike Bostock
*/
public final class MapConstraints {
private MapConstraints() {}
/**
* A constraint that verifies that neither the key nor the value is null. If
* either is null, a {@link NullPointerException} is thrown.
*/
public static final MapConstraint NOT_NULL
= new NotNullMapConstraint();
/** @see MapConstraints#NOT_NULL */
private static class NotNullMapConstraint
implements MapConstraint, Serializable {
public void checkKeyValue(Object key, Object value) {
checkNotNull(key);
checkNotNull(value);
}
private Object readResolve() {
return NOT_NULL; // preserve singleton property
}
private static final long serialVersionUID = -8865373377796638411L;
}
/**
* Returns a constraint that verifies that the key is an instance of {@code
* keyType} and that the value is an instance of {@code valueType}. A {@link
* ClassCastException} is thrown otherwise. In addition, the constraint throws
* a {@link ClassCastException} is thrown when the key or value is
* {@code null}.
*
* @param keyType the required type for keys
* @param valueType the required type for values
* @return a constraint which verifies the type of keys and values
*/
static MapConstraint classConstraint(
Class> keyType, Class> valueType) {
return new ClassMapConstraint(keyType, valueType);
}
/** @see MapConstraints#classConstraint */
private static class ClassMapConstraint
implements MapConstraint, Serializable {
final Class> keyType;
final Class> valueType;
ClassMapConstraint(Class> keyType, Class> valueType) {
this.keyType = checkNotNull(keyType);
this.valueType = checkNotNull(valueType);
}
public void checkKeyValue(Object key, Object value) {
keyType.cast(checkNotNull(key));
valueType.cast(checkNotNull(value));
}
static final long serialVersionUID = 5170999662998754707L;
}
/**
* Returns a constrained view of the specified map, using the specified
* constraint. Any operations that modify the map will have the associated
* keys and values verified with the constraint.
*
* @param map the map for which to return a constrained view
* @param constraint the constraint for entries in the map
* @return a constrained view of the specified map
*/
public static Map constrainedMap(
Map map, MapConstraint super K, ? super V> constraint) {
return new ConstrainedMap(map, constraint);
}
/**
* Returns a constrained view of the specified multimap, using the specified
* constraint. Any operations that modify the multimap will have the
* associated keys and values verified with the constraint.
*
* @param multimap the multimap for which to return a constrained view
* @param constraint the constraint for entries in the multimap
* @return a constrained view of the multimap
*/
public static Multimap constrainedMultimap(
Multimap multimap, MapConstraint super K, ? super V> constraint) {
return new ConstrainedMultimap(multimap, constraint);
}
/**
* Returns a constrained view of the specified list multimap, using the
* specified constraint. Any operations that modify the multimap will have the
* associated keys and values verified with the constraint.
*
* @param multimap the multimap for which to return a constrained view
* @param constraint the constraint for entries in the multimap
* @return a constrained view of the specified multimap
*/
public static ListMultimap constrainedListMultimap(
ListMultimap multimap,
MapConstraint super K, ? super V> constraint) {
return new Multimaps.ListDecorator(
new ConstrainedMultimap(multimap, constraint));
}
/**
* Returns a constrained view of the specified set multimap, using the
* specified constraint. Any operations that modify the multimap will have the
* associated keys and values verified with the constraint.
*
* @param multimap the multimap for which to return a constrained view
* @param constraint the constraint for entries in the multimap
* @return a constrained view of the specified multimap
*/
public static SetMultimap constrainedSetMultimap(
SetMultimap multimap,
MapConstraint super K, ? super V> constraint) {
return new Multimaps.SetDecorator(
new ConstrainedMultimap(multimap, constraint));
}
/**
* Returns a constrained view of the specified sorted-set multimap, using the
* specified constraint. Any operations that modify the multimap will have the
* associated keys and values verified with the constraint.
*
* @param multimap the multimap for which to return a constrained view
* @param constraint the constraint for entries in the multimap
* @return a constrained view of the specified multimap
*/
public static SortedSetMultimap constrainedSortedSetMultimap(
SortedSetMultimap multimap,
MapConstraint super K, ? super V> constraint) {
return new Multimaps.SortedSetDecorator(
new ConstrainedMultimap(multimap, constraint));
}
/**
* Returns a constrained view of the specified entry, using the specified
* constraint. The {@link Entry#setValue} operation will be verified with the
* constraint.
*
* @param entry the entry for which to return a constrained view
* @param constraint the constraint for the entry
* @return a constrained view of the specified entry
*/
private static Entry constrainedEntry(
Entry entry, final MapConstraint super K, ? super V> constraint) {
checkNotNull(entry);
checkNotNull(constraint);
return new ForwardingMapEntry(entry) { // not Serializable
@Override public V setValue(V value) {
constraint.checkKeyValue(getKey(), value);
return super.setValue(value);
}
};
}
/**
* Returns a constrained view of the specified {@code asMap} entry, using the
* specified constraint. The {@link Entry#setValue} operation will be verified
* with the constraint, and the collection returned by {@link Entry#getValue}
* will be similarly constrained.
*
* @param entry the {@code asMap} entry for which to return a constrained view
* @param constraint the constraint for the entry
* @return a constrained view of the specified entry
*/
private static Entry> constrainedAsMapEntry(
Entry> entry,
final MapConstraint super K, ? super V> constraint) {
checkNotNull(entry);
checkNotNull(constraint);
return new ForwardingMapEntry>(entry) { // not Serializable
@Override public Collection getValue() {
return Constraints.constrainedTypePreservingCollection(
super.getValue(), new Constraint() {
public void checkElement(V value) {
constraint.checkKeyValue(getKey(), value);
}
});
}
};
}
/**
* Returns a constrained view of the specified set of {@code asMap} entries,
* using the specified constraint. The {@link Entry#setValue} operation will
* be verified with the constraint, and the collection returned by {@link
* Entry#getValue} will be similarly constrained. The {@code add} and {@code
* addAll} operations simply forward to the underlying set, which throws an
* {@link UnsupportedOperationException} per the multimap specification.
*
* @param entries the entries for which to return a constrained view
* @param constraint the constraint for the entries
* @return a constrained view of the entries
*/
private static Set>> constrainedAsMapEntries(
Set>> entries,
MapConstraint super K, ? super V> constraint) {
return new ConstrainedAsMapEntries(entries, constraint);
}
/**
* Returns a constrained view of the specified collection (or set) of entries,
* using the specified constraint. The {@link Entry#setValue} operation will
* be verified with the constraint, along with add operations on the returned
* collection. The {@code add} and {@code addAll} operations simply forward to
* the underlying collection, which throws an {@link
* UnsupportedOperationException} per the map and multimap specification.
*
* @param entries the entries for which to return a constrained view
* @param constraint the constraint for the entries
* @return a constrained view of the specified entries
*/
@SuppressWarnings("unchecked")
private static Collection> constrainedEntries(
Collection> entries,
MapConstraint super K, ? super V> constraint) {
if (entries instanceof Set>) {
return constrainedEntrySet((Set>) entries, constraint);
}
return new ConstrainedEntries(entries, constraint);
}
/**
* Returns a constrained view of the specified set of entries, using the
* specified constraint. The {@link Entry#setValue} operation will be verified
* with the constraint, along with add operations on the returned set. The
* {@code add} and {@code addAll} operations simply forward to the underlying
* set, which throws an {@link UnsupportedOperationException} per the map and
* multimap specification.
*
* @param entries the entries for which to return a constrained view
* @param constraint the constraint for the entries
* @return a constrained view of the specified entries
*/
private static Set> constrainedEntrySet(
Set> entries,
MapConstraint super K, ? super V> constraint) {
return new ConstrainedEntrySet(entries, constraint);
}
/** @see MapConstraints#constrainedMap */
static class ConstrainedMap extends ForwardingMap {
final MapConstraint super K, ? super V> constraint;
private transient volatile Set> entrySet;
ConstrainedMap(
Map delegate, MapConstraint super K, ? super V> constraint) {
super(delegate);
this.constraint = checkNotNull(constraint);
}
@Override public Set> entrySet() {
if (entrySet == null) {
entrySet = constrainedEntrySet(super.entrySet(), constraint);
}
return entrySet;
}
@Override public V put(K key, V value) {
constraint.checkKeyValue(key, value);
return super.put(key, value);
}
@Override public void putAll(Map extends K, ? extends V> map) {
super.putAll(checkMap(map, constraint));
}
private static final long serialVersionUID = 2187468218878274045L;
}
/**
* Returns a constrained view of the specified bimap, using the specified
* constraint. Any operations that modify the bimap will have the associated
* keys and values verified with the constraint.
*
* @param map the bimap for which to return a constrained view
* @param constraint the constraint for entries in the bimap
* @return a constrained view of the specified map
*/
public static BiMap constrainedBiMap(
BiMap map, MapConstraint super K, ? super V> constraint) {
return new ConstrainedBiMap(map, null, constraint);
}
/** @see MapConstraints#constrainedBiMap */
private static class ConstrainedBiMap extends ConstrainedMap
implements BiMap {
transient volatile BiMap inverse;
ConstrainedBiMap(BiMap delegate, BiMap inverse,
MapConstraint super K, ? super V> constraint) {
super(delegate, constraint);
this.inverse = inverse;
}
@SuppressWarnings("unchecked")
@Override protected BiMap delegate() {
return (BiMap) super.delegate();
}
public V forcePut(K key, V value) {
constraint.checkKeyValue(key, value);
return delegate().forcePut(key, value);
}
public BiMap inverse() {
if (inverse == null) {
inverse = new ConstrainedBiMap(delegate().inverse(), this,
new InverseConstraint(constraint));
}
return inverse;
}
@Override public Set values() {
return delegate().values();
}
}
/** @see MapConstraints#constrainedBiMap */
private static class InverseConstraint
implements MapConstraint, Serializable {
final MapConstraint super V, ? super K> constraint;
public InverseConstraint(MapConstraint super V, ? super K> constraint) {
this.constraint = checkNotNull(constraint);
}
public void checkKeyValue(K key, V value) {
constraint.checkKeyValue(value, key);
}
private static final long serialVersionUID = 4461050635804577699L;
}
/** @see MapConstraints#constrainedMultimap */
private static class ConstrainedMultimap
extends ForwardingMultimap {
final MapConstraint super K, ? super V> constraint;
transient volatile Collection> entries;
transient volatile Map> asMap;
public ConstrainedMultimap(Multimap delegate,
MapConstraint super K, ? super V> constraint) {
super(delegate);
this.constraint = constraint;
}
@Override public Map> asMap() {
if (asMap == null) {
asMap = new ForwardingMap>(delegate().asMap()) {
volatile Set>> entrySet;
volatile Collection> values;
@Override public Set>> entrySet() {
if (entrySet == null) {
entrySet = constrainedAsMapEntries(
super.entrySet(), constraint);
}
return entrySet;
}
@SuppressWarnings("unchecked")
@Override public Collection get(Object key) {
Collection collection = ConstrainedMultimap.this.get((K) key);
return collection.isEmpty() ? null : collection;
}
@Override public Collection> values() {
if (values == null) {
values = new ConstrainedAsMapValues(
delegate().values(), entrySet());
}
return values;
}
@Override public boolean containsValue(Object o) {
return values().contains(o);
}
};
}
return asMap;
}
@Override public Collection> entries() {
if (entries == null) {
entries = constrainedEntries(super.entries(), constraint);
}
return entries;
}
@Override public Collection get(final K key) {
return Constraints.constrainedTypePreservingCollection(
super.get(key), new Constraint() {
public void checkElement(V value) {
constraint.checkKeyValue(key, value);
}
});
}
@Override public boolean put(K key, V value) {
constraint.checkKeyValue(key, value);
return super.put(key, value);
}
@Override public void putAll(K key, Iterable extends V> values) {
super.putAll(key, checkValues(key, values, constraint));
}
@Override public void putAll(Multimap extends K, ? extends V> multimap) {
super.putAll(checkMultimap(multimap, constraint));
}
@Override public Collection replaceValues(
K key, Iterable extends V> values) {
return super.replaceValues(key, checkValues(key, values, constraint));
}
private static final long serialVersionUID = 1022236989881570422L;
}
/** @see ConstrainedMultimap#asMap */
private static class ConstrainedAsMapValues
extends ForwardingCollection> {
final Set>> entrySet;
/**
* @param entrySet map entries, linking each key with its corresponding
* values, that already enforce the constraint
*/
ConstrainedAsMapValues(Collection> delegate,
Set>> entrySet) {
super(delegate);
this.entrySet = entrySet;
}
@Override public Iterator> iterator() {
final Iterator>> iterator = entrySet.iterator();
return new Iterator>() {
public boolean hasNext() {
return iterator.hasNext();
}
public Collection next() {
return iterator.next().getValue();
}
public void remove() {
iterator.remove();
}
};
}
@Override public Object[] toArray() {
return toArrayImpl(this);
}
@Override public T[] toArray(T[] array) {
return toArrayImpl(this, array);
}
@Override public boolean contains(Object o) {
return containsImpl(this, o);
}
@Override public boolean containsAll(Collection> c) {
return containsAllImpl(this, c);
}
@Override public boolean remove(Object o) {
return removeImpl(this, o);
}
@Override public boolean removeAll(Collection> c) {
return removeAllImpl(this, c);
}
@Override public boolean retainAll(Collection> c) {
return retainAllImpl(this, c);
}
}
/** @see MapConstraints#constrainedEntries */
private static class ConstrainedEntries // not Serializable
extends ForwardingCollection> {
final MapConstraint super K, ? super V> constraint;
ConstrainedEntries(Collection> entries,
MapConstraint super K, ? super V> constraint) {
super(entries);
this.constraint = constraint;
}
@Override public Iterator> iterator() {
return new ForwardingIterator>(super.iterator()) {
@Override public Entry next() {
return constrainedEntry(super.next(), constraint);
}
};
}
// See java.util.Collections.CheckedEntrySet for details on attacks.
@Override public Object[] toArray() {
return toArrayImpl(this);
}
@Override public T[] toArray(T[] array) {
return toArrayImpl(this, array);
}
@Override public boolean contains(Object o) {
return Maps.containsEntryImpl(delegate(), o);
}
@Override public boolean containsAll(Collection> c) {
return containsAllImpl(this, c);
}
@Override public boolean remove(Object o) {
return Maps.removeEntryImpl(delegate(), o);
}
@Override public boolean removeAll(Collection> c) {
return removeAllImpl(this, c);
}
@Override public boolean retainAll(Collection> c) {
return retainAllImpl(this, c);
}
}
/** @see MapConstraints#constrainedEntrySet */
static class ConstrainedEntrySet // not Serializable
extends ConstrainedEntries implements Set> {
ConstrainedEntrySet(Set> entries,
MapConstraint super K, ? super V> constraint) {
super(entries, constraint);
}
// See java.util.Collections.CheckedEntrySet for details on attacks.
@Override public boolean equals(Object o) {
return ForwardingSet.equalsImpl(this, o);
}
}
/** @see MapConstraints#constrainedAsMapEntries */
static class ConstrainedAsMapEntries // not Serializable
extends ForwardingSet>> {
private final MapConstraint super K, ? super V> constraint;
ConstrainedAsMapEntries(Set>> entries,
MapConstraint super K, ? super V> constraint) {
super(entries);
this.constraint = constraint;
}
@Override public Iterator>> iterator() {
return new ForwardingIterator>>(super.iterator()) {
@Override public Entry> next() {
return constrainedAsMapEntry(super.next(), constraint);
}
};
}
// See java.util.Collections.CheckedEntrySet for details on attacks.
@Override public Object[] toArray() {
return toArrayImpl(this);
}
@Override public T[] toArray(T[] array) {
return toArrayImpl(this, array);
}
@Override public boolean contains(Object o) {
return Maps.containsEntryImpl(delegate(), o);
}
@Override public boolean containsAll(Collection> c) {
return containsAllImpl(this, c);
}
@Override public boolean equals(Object o) {
return equalsImpl(this, o);
}
@Override public boolean remove(Object o) {
return Maps.removeEntryImpl(delegate(), o);
}
@Override public boolean removeAll(Collection> c) {
return removeAllImpl(this, c);
}
@Override public boolean retainAll(Collection> c) {
return retainAllImpl(this, c);
}
}
private static Collection checkValues(K key,
Iterable extends V> values,
MapConstraint super K, ? super V> constraint) {
Collection copy = Lists.newArrayList(values);
for (V value : copy) {
constraint.checkKeyValue(key, value);
}
return copy;
}
private static Map checkMap(Map extends K, ? extends V> map,
MapConstraint super K, ? super V> constraint) {
Map copy = new LinkedHashMap(map);
for (Entry entry : copy.entrySet()) {
constraint.checkKeyValue(entry.getKey(), entry.getValue());
}
return copy;
}
private static Multimap checkMultimap(
Multimap extends K, ? extends V> map,
MapConstraint super K, ? super V> constraint) {
Multimap copy = new LinkedListMultimap(map);
for (Entry entry : copy.entries()) {
constraint.checkKeyValue(entry.getKey(), entry.getValue());
}
return copy;
}
}