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;
 import java.util.Map;
 
 
This class provides base implementation of the Constraint that encapsulates other constraints to validate a value.

Parameters:
<V> The type of value to be validated
Author(s):
Fox Mulder
 
 public abstract class ConstraintAggregation<V> extends AbstractValidation<V> implements Constraint<V> {
    
    
The type of value to be validated.
 
     protected final Class<?> type;
    
    
Array of constraints to be used for validation of a value.
 
     protected final Constraint<? super V>[] constraints;
    
    
Constructs a new ConstraintAggregation with the specified value type and array of other constraints.

Parameters:
type The type of value to be validated.
constraints Array of constraints to be used for validation of a value.
Throws:
java.lang.IllegalArgumentException if the specified type is null or the specified array of constraints is null or empty or contains null elements.
 
     protected ConstraintAggregation(Class<?> type, Constraint<? super V>... constraints) {
         this. = Assert.notNull(type"type");
         this. = Assert.noNullElements(constraints"constraints");
     }
    
    
Returns the type of value to be validated.

Returns:
The type of value to be validated.
 
     @Override
     public final Class<?> getType() {
         return ;
     }
    
    
Returns array of constraints to be used for validation of a value.

Returns:
Array of constraints to be used for validation of a value.
 
     public final Constraint<? super V>[] getConstraints() {
         return .clone();
     }
    
    
Appends constraints argument that contains encapsulated constraints.

Parameters:
context Validation context.
arguments Arguments to be substituted into the error message template.
Returns:
true.
 
     @Override
     public boolean appendMessageArguments(ValidationContext<?> contextMap<StringObjectarguments) {
         super.appendMessageArguments(contextarguments);
         arguments.put("constraints");
         return true;
     }
    
    
Checks whether the specified value conforms to all of the encapsulated constraints.

Parameters:
value Value to be validated.
context Validation context.
Returns:
Possibly modified value if at least one of the encapsulated constraints can modify value; unmodified value otherwise.
Throws:
ConstraintViolationException if the specified value not conforms at least one of the encapsulated constraint.
    @Override
    @SuppressWarnings("unchecked")
    public <T> V validate(V valueValidationContext<T> context) {
        for (Constraint<? super V> constraint : ) {
            try {
                value = (V) constraint.validate(valuecontext);
            } catch (ConstraintViolationException e) {
                violations.add(e);
                if (context.isFailFast())
                    break;
            }
        }
        if (violations.isEmpty())
            return value;
        throw new ConstraintViolationException(thiscontextvalue,
                new ValidationException(violations));
    }
    
New to GrepCode? Check out our FAQ X