com.google.common.collect
Interface MapConstraint<K,V>


public interface MapConstraint<K,V>

Interface for defining a constraint on the types of keys and values that are allowed to be added to a Map or Multimap. For example, to enforce that a map contains no null keys or values, you might say:

  public void checkKeyValue(Object key, Object value) {
    if (key == null) {
      throw new NullPointerException();
    }
    if (value == null) {
      throw new NullPointerException();
    }
  }
Then use MapConstraints.constrainedMap(java.util.Map, com.google.common.collect.MapConstraint) to enforce the constraint. This example is contrived; to check for null use MapConstraints.NOT_NULL.

See Constraint for an important comment regarding determinism, thread-safety and mutability when implementing constraints.

Author:
Mike Bostock
See Also:
MapConstraints, Constraint

Method Summary
 void checkKeyValue(K key, V value)
          Implement this method to throw a suitable RuntimeException if the specified key or value is illegal.
 

Method Detail

checkKeyValue

void checkKeyValue(@Nullable
                   K key,
                   @Nullable
                   V value)
Implement this method to throw a suitable RuntimeException if the specified key or value is illegal. Typically this is either a NullPointerException, an IllegalArgumentException, or a ClassCastException, though a more application-specific exception class may be used as appropriate.