com.google.common.collect
Class Constraints

java.lang.Object
  extended by com.google.common.collect.Constraints

@Beta
@GwtCompatible
public final class Constraints
extends Object

Factories and utilities pertaining to the Constraint interface.

Since:
3
Author:
Mike Bostock, Jared Levy
See Also:
MapConstraints

Method Summary
static
<E> Collection<E>
constrainedCollection(Collection<E> collection, Constraint<? super E> constraint)
          Returns a constrained view of the specified collection, using the specified constraint.
static
<E> List<E>
constrainedList(List<E> list, Constraint<? super E> constraint)
          Returns a constrained view of the specified list, using the specified constraint.
static
<E> Multiset<E>
constrainedMultiset(Multiset<E> multiset, Constraint<? super E> constraint)
          Returns a constrained view of the specified multiset, using the specified constraint.
static
<E> Set<E>
constrainedSet(Set<E> set, Constraint<? super E> constraint)
          Returns a constrained view of the specified set, using the specified constraint.
static
<E> SortedSet<E>
constrainedSortedSet(SortedSet<E> sortedSet, Constraint<? super E> constraint)
          Returns a constrained view of the specified sorted set, using the specified constraint.
static
<E> Constraint<E>
notNull()
          Returns a constraint that verifies that the element is not null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

notNull

public static <E> Constraint<E> notNull()
Returns a constraint that verifies that the element is not null. If the element is null, a NullPointerException is thrown.


constrainedCollection

public static <E> Collection<E> constrainedCollection(Collection<E> collection,
                                                      Constraint<? super E> constraint)
Returns a constrained view of the specified collection, using the specified constraint. Any operations that add new elements to the collection will call the provided constraint. However, this method does not verify that existing elements satisfy the constraint.

The returned collection is not serializable.

Parameters:
collection - the collection to constrain
constraint - the constraint that validates added elements
Returns:
a constrained view of the collection

constrainedSet

public static <E> Set<E> constrainedSet(Set<E> set,
                                        Constraint<? super E> constraint)
Returns a constrained view of the specified set, using the specified constraint. Any operations that add new elements to the set will call the provided constraint. However, this method does not verify that existing elements satisfy the constraint.

The returned set is not serializable.

Parameters:
set - the set to constrain
constraint - the constraint that validates added elements
Returns:
a constrained view of the set

constrainedSortedSet

public static <E> SortedSet<E> constrainedSortedSet(SortedSet<E> sortedSet,
                                                    Constraint<? super E> constraint)
Returns a constrained view of the specified sorted set, using the specified constraint. Any operations that add new elements to the sorted set will call the provided constraint. However, this method does not verify that existing elements satisfy the constraint.

The returned set is not serializable.

Parameters:
sortedSet - the sorted set to constrain
constraint - the constraint that validates added elements
Returns:
a constrained view of the sorted set

constrainedList

public static <E> List<E> constrainedList(List<E> list,
                                          Constraint<? super E> constraint)
Returns a constrained view of the specified list, using the specified constraint. Any operations that add new elements to the list will call the provided constraint. However, this method does not verify that existing elements satisfy the constraint.

If list implements RandomAccess, so will the returned list. The returned list is not serializable.

Parameters:
list - the list to constrain
constraint - the constraint that validates added elements
Returns:
a constrained view of the list

constrainedMultiset

public static <E> Multiset<E> constrainedMultiset(Multiset<E> multiset,
                                                  Constraint<? super E> constraint)
Returns a constrained view of the specified multiset, using the specified constraint. Any operations that add new elements to the multiset will call the provided constraint. However, this method does not verify that existing elements satisfy the constraint.

The returned multiset is not serializable.

Parameters:
multiset - the multiset to constrain
constraint - the constraint that validates added elements
Returns:
a constrained view of the multiset