Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* 
   * Copyright (C) 2012 FoxLabs
   * 
   * 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 org.foxlabs.validation.constraint;
 
 import java.util.List;
 
This class provides SequenceElementConstraint implementation that performs validation of collection elements.

Parameters:
<V> The type of collection elements
Author(s):
Fox Mulder
See also:
ConstraintFactory.collectionElement(org.foxlabs.validation.constraint.Constraint)
 
 public final class CollectionElementConstraint<V> extends SequenceElementConstraint<Collection<V>, V> {
    
    
Constructs a new CollectionElementConstraint with the specified constraint of collection elements.

Parameters:
constraint Constraint to be used for validation of collection elements.
Throws:
java.lang.IllegalArgumentException if the specified constraint is null.
 
     CollectionElementConstraint(Constraint<V> constraint) {
         super(constraint);
     }
    
    
Returns java.util.Collection type.

Returns:
java.util.Collection type.
 
     @Override
     public Class<?> getType() {
         return Collection.class;
     }
    
    
Checks whether all elements of the specified collection conforms to the constraint of collection elements.

Parameters:
collection Collection whose elements to be checked.
context Validation context.
violations List of violations.
Returns:
Collection with possibly modified elements if constraint of collection elements can modify values; unmodified collection otherwise.
 
     @Override
     protected <T> Collection<V> doValidate(Collection<V> collectionValidationContext<T> context,
             List<ConstraintViolationExceptionviolations) {
         context.setCurrentTarget(.);
         if (collection instanceof List) {
             ListIterator<V> itr = ((List<V>) collection).listIterator();
             for (int i = 0; itr.hasNext(); i++) {
                 try {
                     context.setCurrentIndex(i);
                     V oldValue = itr.next();
                     V newValue = .validate(oldValuecontext);
                     if (oldValue != newValue)
                         itr.set(newValue);
                 } catch (ConstraintViolationException e) {
                     violations.add(e);
                 }
             }
         } else {
             List<V> newValues = new LinkedList<V>();
             Iterator<V> itr = collection.iterator();
             for (int i = 0; itr.hasNext(); i++) {
                 try {
                     context.setCurrentIndex(i);
                     V oldValue = itr.next();
                     V newValue = .validate(oldValuecontext);
                     if (oldValue == null ? newValue != null : !oldValue.equals(newValue)) {
                         newValues.add(newValue);
                         itr.remove();
                    }
                } catch (ConstraintViolationException e) {
                    violations.add(e);
                }
            }
            collection.addAll(newValues);
        }
        return collection;
    }
    
New to GrepCode? Check out our FAQ X