Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 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 com.google.common.collect;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
 import java.util.List;
 import java.util.Set;
Factories and utilities pertaining to the Constraint interface.

Author(s):
Mike Bostock
Jared Levy
 
 final class Constraints {
   private Constraints() {}

  
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
 
   public static <E> Collection<E> constrainedCollection(
       Collection<E> collectionConstraint<? super E> constraint) {
     return new ConstrainedCollection<E>(collectionconstraint);
   }

  
 
   static class ConstrainedCollection<E> extends ForwardingCollection<E> {
     private final Collection<E> delegate;
     private final Constraint<? super E> constraint;
 
     public ConstrainedCollection(
         Collection<E> delegateConstraint<? super E> constraint) {
       this. = checkNotNull(delegate);
       this. = checkNotNull(constraint);
     }
     @Override protected Collection<E> delegate() {
       return ;
     }
     @Override public boolean add(E element) {
       .checkElement(element);
       return .add(element);
     }
     @Override public boolean addAll(Collection<? extends E> elements) {
       return .addAll(checkElements(elements));
     }
   }

  
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
 
   public static <E> Set<E> constrainedSet(
       Set<E> setConstraint<? super E> constraint) {
     return new ConstrainedSet<E>(setconstraint);
   }

  
 
   static class ConstrainedSet<E> extends ForwardingSet<E> {
     private final Set<E> delegate;
     private final Constraint<? super E> constraint;
    public ConstrainedSet(Set<E> delegateConstraint<? super E> constraint) {
      this. = checkNotNull(delegate);
      this. = checkNotNull(constraint);
    }
    @Override protected Set<E> delegate() {
      return ;
    }
    @Override public boolean add(E element) {
      .checkElement(element);
      return .add(element);
    }
    @Override public boolean addAll(Collection<? extends E> elements) {
      return .addAll(checkElements(elements));
    }
  }

  
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
  public static <E> SortedSet<E> constrainedSortedSet(
      SortedSet<E> sortedSetConstraint<? super E> constraint) {
    return new ConstrainedSortedSet<E>(sortedSetconstraint);
  }

  
  private static class ConstrainedSortedSet<E> extends ForwardingSortedSet<E> {
    final SortedSet<E> delegate;
    final Constraint<? super E> constraint;
        SortedSet<E> delegateConstraint<? super E> constraint) {
      this. = checkNotNull(delegate);
      this. = checkNotNull(constraint);
    }
    @Override protected SortedSet<E> delegate() {
      return ;
    }
    @Override public SortedSet<E> headSet(E toElement) {
      return constrainedSortedSet(.headSet(toElement), );
    }
    @Override public SortedSet<E> subSet(E fromElement, E toElement) {
      return constrainedSortedSet(
          .subSet(fromElementtoElement), );
    }
    @Override public SortedSet<E> tailSet(E fromElement) {
      return constrainedSortedSet(.tailSet(fromElement), );
    }
    @Override public boolean add(E element) {
      .checkElement(element);
      return .add(element);
    }
    @Override public boolean addAll(Collection<? extends E> elements) {
      return .addAll(checkElements(elements));
    }
  }

  
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 java.util.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
  public static <E> List<E> constrainedList(
      List<E> listConstraint<? super E> constraint) {
    return (list instanceof RandomAccess)
        ? new ConstrainedRandomAccessList<E>(listconstraint)
        : new ConstrainedList<E>(listconstraint);
  }

  
  private static class ConstrainedList<E> extends ForwardingList<E> {
    final List<E> delegate;
    final Constraint<? super E> constraint;
    ConstrainedList(List<E> delegateConstraint<? super E> constraint) {
      this. = checkNotNull(delegate);
      this. = checkNotNull(constraint);
    }
    @Override protected List<E> delegate() {
      return ;
    }
    @Override public boolean add(E element) {
      .checkElement(element);
      return .add(element);
    }
    @Override public void add(int index, E element) {
      .checkElement(element);
      .add(indexelement);
    }
    @Override public boolean addAll(Collection<? extends E> elements) {
      return .addAll(checkElements(elements));
    }
    @Override public boolean addAll(int indexCollection<? extends E> elements)
    {
      return .addAll(indexcheckElements(elements));
    }
    @Override public ListIterator<E> listIterator() {
    }
    @Override public ListIterator<E> listIterator(int index) {
    }
    @Override public E set(int index, E element) {
      .checkElement(element);
      return .set(indexelement);
    }
    @Override public List<E> subList(int fromIndexint toIndex) {
      return constrainedList(
          .subList(fromIndextoIndex), );
    }
  }

  
  static class ConstrainedRandomAccessList<E> extends ConstrainedList<E>
      implements RandomAccess {
        List<E> delegateConstraint<? super E> constraint) {
      super(delegateconstraint);
    }
  }

  
Returns a constrained view of the specified list iterator, using the specified constraint. Any operations that would add new elements to the underlying list will be verified by the constraint.

Parameters:
listIterator the iterator for which to return a constrained view
constraint the constraint for elements in the list
Returns:
a constrained view of the specified iterator
  private static <E> ListIterator<E> constrainedListIterator(
      ListIterator<E> listIteratorConstraint<? super E> constraint) {
    return new ConstrainedListIterator<E>(listIteratorconstraint);
  }

  
  static class ConstrainedListIterator<E> extends ForwardingListIterator<E> {
    private final ListIterator<E> delegate;
    private final Constraint<? super E> constraint;
        ListIterator<E> delegateConstraint<? super E> constraint) {
      this. = delegate;
      this. = constraint;
    }
    @Override protected ListIterator<E> delegate() {
      return ;
    }
    @Override public void add(E element) {
      .checkElement(element);
      .add(element);
    }
    @Override public void set(E element) {
      .checkElement(element);
      .set(element);
    }
  }
      Collection<E> collectionConstraint<E> constraint) {
    if (collection instanceof SortedSet) {
      return constrainedSortedSet((SortedSet<E>) collectionconstraint);
    } else if (collection instanceof Set) {
      return constrainedSet((Set<E>) collectionconstraint);
    } else if (collection instanceof List) {
      return constrainedList((List<E>) collectionconstraint);
    } else {
      return constrainedCollection(collectionconstraint);
    }
  }
  /*
   * TODO(kevinb): For better performance, avoid making a copy of the elements
   * by having addAll() call add() repeatedly instead.
   */
  private static <E> Collection<E> checkElements(
      Collection<E> elementsConstraint<? super E> constraint) {
    Collection<E> copy = Lists.newArrayList(elements);
    for (E element : copy) {
      constraint.checkElement(element);
    }
    return copy;
  }
New to GrepCode? Check out our FAQ X