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 map values.

Parameters:
<V> The type of map values
Author(s):
Fox Mulder
See also:
ConstraintFactory.mapValue(org.foxlabs.validation.constraint.Constraint)
public final class MapValueConstraint<V> extends SequenceElementConstraint<Map<Object, V>, V> {
    
    
Constructs a new MapValueConstraint with the specified constraint of map values.

Parameters:
constraint Constraint to be used for validation of map values.
Throws:
java.lang.NullPointerException if the specified constraint is null.
    MapValueConstraint(Constraint<V> constraint) {
        super(constraint);
    }
    
    
Returns java.util.Map type.

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

Parameters:
map Map whose values to be checked.
context Validation context.
violations List of violations.
Returns:
Map with possibly modified values if constraint of map values can modify values; unmodified map otherwise.
    @Override
    protected <T> Map<Object, V> doValidate(Map<Object, V> mapValidationContext<T> context,
            List<ConstraintViolationExceptionviolations) {
        context.setCurrentTarget(.);
        for (Map.Entry<Object, V> entry : map.entrySet()) {
            try {
                context.setCurrentIndex(entry.getKey());
                V oldValue = entry.getValue();
                V newValue = .validate(oldValuecontext);
                if (oldValue == null ? newValue != null : !oldValue.equals(newValue))
                    entry.setValue(newValue);
            } catch (ConstraintViolationException e) {
                violations.add(e);
            }
        }
        return map;
    }
    
New to GrepCode? Check out our FAQ X