Coverage Report - org.apache.commons.contract.constraints.MapConstraints
 
Classes in this File Line Coverage Branch Coverage Complexity
MapConstraints
0%
0/76
0%
0/44
4
 
 1  
 package org.apache.commons.contract.constraints;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.HashMap;
 5  
 import java.util.Iterator;
 6  
 import java.util.List;
 7  
 import java.util.Map;
 8  
 
 9  
 import org.apache.commons.contract.Context;
 10  
 import org.apache.commons.contract.descriptor.ParameterDescriptor;
 11  
 import org.apache.commons.contract.i18n.ParameterBundle;
 12  
 import org.apache.commons.i18n.bundles.ErrorBundle;
 13  
 import org.apache.commons.i18n.bundles.TextBundle;
 14  
 
 15  
 public class MapConstraints implements Constraints {
 16  0
     public final static MapConstraints UNCONSTRAINED = new MapConstraints(new ParameterDescriptor(MapConstraints.ALL, new ParameterBundle("mapEntry/any"), Unconstrained.UNCONSTRAINED)); 
 17  
     public final static String ALL = "*";
 18  
         
 19  0
         protected List entryConstraints = new ArrayList();
 20  
 
 21  0
         public MapConstraints() {
 22  0
         }
 23  
         
 24  0
     public MapConstraints(List entryDescriptors) {
 25  0
         this.entryConstraints = entryDescriptors;
 26  0
     }
 27  
 
 28  0
     public MapConstraints(ParameterDescriptor parameterDescriptor) {
 29  0
             entryConstraints.add(parameterDescriptor);
 30  0
     }
 31  
 
 32  0
     public MapConstraints(ParameterDescriptor[] parameterDescriptors) {
 33  0
         for ( int i = 0; i < parameterDescriptors.length; i++ ) {
 34  0
             entryConstraints.add(parameterDescriptors[i]);
 35  
         }
 36  0
     }
 37  
 
 38  
     public void addEntryDescriptor(ParameterDescriptor parameterDescriptor) {
 39  0
         entryConstraints.add(parameterDescriptor);
 40  0
     }
 41  
 
 42  
     public List getEntryDescriptors() {
 43  0
             return entryConstraints;
 44  
     }
 45  
     
 46  
     public Object cast(Object value, Context context) throws CastException {
 47  0
         if ( entryConstraints.isEmpty() ) {
 48  0
                 throw new CastException(new ErrorBundle("noMapEntryDescriptorsFound"));
 49  
         }
 50  0
         if ( value instanceof Map ) {
 51  0
             return castedMap((Map)value, context);
 52  0
         } else if ( value instanceof Map ) {
 53  0
             return castedMap((Map)value, context);
 54  
         } else {
 55  0
             throw new CastException(new ErrorBundle("uncastableMapValue", new Object[] { value }));
 56  
         }
 57  
     }
 58  
 
 59  
     protected Map castedMap(Map map, Context context) throws CastException {
 60  0
             Map castedMap = new HashMap(map);
 61  0
             for ( Iterator i = entryConstraints.iterator(); i.hasNext(); ) {
 62  0
             ParameterDescriptor parameterDescriptor = (ParameterDescriptor)i.next();
 63  0
             Constraints entryDescriptor = parameterDescriptor.getConstraints();
 64  0
             String key = parameterDescriptor.getName();
 65  0
             if ( key.equals(ALL) ) {
 66  0
                     for ( Iterator j = castedMap.entrySet().iterator(); j.hasNext(); ) {
 67  0
                             Map.Entry entry = (Map.Entry)j.next();
 68  0
                             Object value = entry.getValue();
 69  0
                     if ( value instanceof Evaluatable ) {
 70  
                         try {
 71  0
                             value = ((Evaluatable)value).evaluate(context);
 72  0
                         } catch (Exception e) {
 73  0
                             throw new CastException(new ErrorBundle("evaluatingAnyFailed"), e);
 74  0
                         }
 75  
                     }
 76  0
                            castedMap.put(entry.getKey(), entryDescriptor.cast(value, context));
 77  0
                     }
 78  
             } else {
 79  0
                     if ( !castedMap.containsKey(key) ) {
 80  0
                             if ( !parameterDescriptor.isRequired() ) {
 81  0
                                     castedMap.put(key, parameterDescriptor.getDefaultValue());
 82  
                             }
 83  
                     } else {
 84  0
                             Object object = castedMap.get(key);
 85  0
                             if ( object == null && parameterDescriptor.getDefaultValue() == null || object.equals(parameterDescriptor.getDefaultValue())) {
 86  0
                                     castedMap.put(key, object);
 87  
                             } else {
 88  0
                         if ( object instanceof Evaluatable ) {
 89  
                             try {
 90  0
                                 object = ((Evaluatable)object).evaluate(context);
 91  0
                             } catch (Exception e) {
 92  0
                                 throw new CastException(new ErrorBundle("evaluatingAnyFailed"), e);
 93  0
                             }
 94  
                         }
 95  0
                                     castedMap.put(key, entryDescriptor.cast(object, context));
 96  
                             }
 97  
                     }
 98  
             }
 99  0
         }
 100  0
         return castedMap;
 101  
     }
 102  
     
 103  
     public void validate(Object value, Context context) throws ValidationException {
 104  0
             Map map = (Map)value;
 105  0
             for ( Iterator i = entryConstraints.iterator(); i.hasNext(); ) {
 106  0
             ParameterDescriptor parameterDescriptor = (ParameterDescriptor)i.next();
 107  0
             Constraints entryDescriptor = parameterDescriptor.getConstraints();
 108  0
             String key = parameterDescriptor.getName();
 109  0
             if ( key.equals(ALL) ) {
 110  0
                     for ( Iterator j = map.values().iterator(); j.hasNext(); ) {
 111  0
                             Object entryValue = j.next();
 112  0
                             if ( entryValue != null ) {
 113  0
                                     entryDescriptor.validate(entryValue, context);
 114  
                             }
 115  0
                     }
 116  
             } else {
 117  0
                     if ( !map.containsKey(key) ) {
 118  0
                             if ( parameterDescriptor.isRequired() ) {
 119  0
                                     throw new ValidationException(new ErrorBundle("mapEntryMissing", new String[] { key }));
 120  
                             }
 121  
                     } else {
 122  0
                             Object entryValue = map.get(key);
 123  0
                             if ( entryValue != null ) {
 124  0
                                     entryDescriptor.validate(entryValue, context);
 125  
                             }
 126  
                     }
 127  
             }
 128  0
         }
 129  0
    }
 130  
     
 131  
     public TextBundle verboseConstraints() {
 132  0
         if ( entryConstraints.isEmpty() ) {
 133  0
             return new TextBundle("invalidMapConstraints");
 134  
         }
 135  0
         return new TextBundle("unconstrainedMap");
 136  
     }
 137  
 
 138  
 }