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;
This class provides SequenceElementConstraint implementation that performs validation of array elements.

Parameters:
<V> The type of array elements
Author(s):
Fox Mulder
See also:
ConstraintFactory.arrayElement(org.foxlabs.validation.constraint.Constraint)
public final class ArrayElementConstraint<V> extends SequenceElementConstraint<Object, V> {
    
    
Array type.
    private final Class<Objecttype;
    
    
Constructs a new ArrayElementConstraint with the specified constraint of array elements.

Parameters:
constraint Constraint to be used for validation of array elements.
Throws:
java.lang.IllegalArgumentException if the specified constraint is null.
    ArrayElementConstraint(Constraint<V> constraint) {
        super(constraint);
        this. = Types.arrayTypeOf(constraint.getType());
    }
    
    
Returns array type.

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

Parameters:
array Array whose elements to be checked.
context Validation context.
violations List of violations.
Returns:
Array with possibly modified elements if constraint of array elements can modify values; unmodified array otherwise.
    @Override
    @SuppressWarnings("unchecked")
    protected <T> Object doValidate(Object arrayValidationContext<T> context,
            List<ConstraintViolationExceptionviolations) {
        int length = Array.getLength(array);
        context.setCurrentTarget(.);
        for (int i = 0; i < lengthi++) {
            try {
                context.setCurrentIndex(i);
                V oldValue = (V) Array.get(arrayi);
                V newValue = .validate(oldValuecontext);
                if (oldValue != newValue)
                    Array.set(arrayinewValue);
            } catch (ConstraintViolationException e) {
                violations.add(e);
            }
        }
        return array;
    }
    
New to GrepCode? Check out our FAQ X