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 keys.

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

Parameters:
constraint Constraint to be used for validation of map keys.
Throws:
java.lang.NullPointerException if the specified constraint is null.
    MapKeyConstraint(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 keys of the specified map conforms to the constraint of map keys.

Parameters:
map Map whose keys to be checked.
context Validation context.
violations List of violations.
Returns:
Map with possibly modified keys if constraint of map keys can modify values; unmodified map otherwise.
    @Override
    protected <T> Map<V, ObjectdoValidate(Map<V, ObjectmapValidationContext<T> context,
            List<ConstraintViolationExceptionviolations) {
        context.setCurrentTarget(.);
        Map<V, ObjectnewKeys = new IdentityHashMap<V, Object>();
        Iterator<Map.Entry<V, Object>> itr = map.entrySet().iterator();
        while (itr.hasNext()) {
            try {
                Map.Entry<V, Objectentry = itr.next();
                context.setCurrentIndex(entry.getKey());
                V oldKey = entry.getKey();
                V newKey = .validate(oldKeycontext);
                if (oldKey == null ? newKey != null : !oldKey.equals(newKey)) {
                    newKeys.put(newKeyentry.getValue());
                    itr.remove();
                }
            } catch (ConstraintViolationException e) {
                violations.add(e);
            }
        }
        map.putAll(newKeys);
        return map;
    }
    
New to GrepCode? Check out our FAQ X