Coverage Report - org.apache.commons.contract.constraints.ListConstraints
 
Classes in this File Line Coverage Branch Coverage Complexity
ListConstraints
0%
0/48
0%
0/22
3.714
 
 1  
 package org.apache.commons.contract.constraints;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.Iterator;
 5  
 import java.util.List;
 6  
 import java.util.Locale;
 7  
 
 8  
 import org.apache.commons.contract.Context;
 9  
 import org.apache.commons.i18n.bundles.ErrorBundle;
 10  
 import org.apache.commons.i18n.bundles.TextBundle;
 11  
 
 12  
 public class ListConstraints implements Constraints {
 13  
         protected Constraints entryValueDescriptor;
 14  
 
 15  0
     public ListConstraints() {
 16  0
         entryValueDescriptor = Unconstrained.UNCONSTRAINED;
 17  0
     }
 18  
 
 19  0
     public ListConstraints(Constraints entryValueDescriptor) {
 20  0
         this.entryValueDescriptor = entryValueDescriptor;
 21  0
     }
 22  
 
 23  
     public void setEntryValueDescriptor(Constraints entryValueDescriptor) {
 24  0
         this.entryValueDescriptor = entryValueDescriptor;
 25  0
     }
 26  
 
 27  
     public Constraints getEntryValueDescriptor() {
 28  0
         return entryValueDescriptor;
 29  
     }
 30  
 
 31  
     public Object cast(Object value, Context context) throws CastException {
 32  0
             List list = new ArrayList();
 33  0
             if ( value instanceof String[] ) {
 34  0
                     for ( int i = 0; i < ((String[])value).length; i++ ) {
 35  0
                             list.add(entryValueDescriptor.cast(((String [])value)[i], context));
 36  
                     }
 37  0
             } else if ( value instanceof List ) {
 38  0
                     for ( Iterator i = ((List)value).iterator(); i.hasNext(); ) {
 39  0
                             Object entry = i.next();
 40  0
                 if ( entry instanceof Evaluatable ) {
 41  
                     try {
 42  0
                         entry = ((Evaluatable)entry).evaluate(context);
 43  0
                     } catch (Exception e) {
 44  0
                         throw new CastException(new ErrorBundle("evaluatingAnyFailed"), e);
 45  0
                     }
 46  
                 }
 47  0
                             list.add(entryValueDescriptor.cast(entry, context));
 48  0
                     }
 49  0
         } else if ( value instanceof Object[] ) {
 50  0
                     for ( int i = 0; i < ((Object [])value).length; i++ ) {
 51  0
                             Object entry = ((Object [])value)[i];
 52  0
                 if ( entry instanceof Evaluatable ) {
 53  
                     try {
 54  0
                         entry = ((Evaluatable)entry).evaluate(context);
 55  0
                     } catch (Exception e) {
 56  0
                         throw new CastException(new ErrorBundle("evaluatingAnyFailed"), e);
 57  0
                     }
 58  
                 }
 59  0
                 list.add(entryValueDescriptor.cast(entry, context));
 60  
                     }
 61  
             } else {
 62  0
                     throw new CastException(new ErrorBundle("uncastableList", new Object[] { value }));
 63  
             }
 64  0
                 return list;
 65  
     }
 66  
     
 67  
     public void validate(Object value, Context context) throws ValidationException {
 68  
             try {
 69  0
                     List list = (List)value;
 70  0
                     for ( Iterator i = list.iterator(); i.hasNext();) {
 71  0
                 Object entry = i.next();
 72  0
                             if ( entry != null ) {
 73  0
                                     entryValueDescriptor.validate(entry, context);
 74  
                             }
 75  0
                     }
 76  0
             } catch ( ValidationException exception ) {
 77  0
                     throw new ValidationException(new ErrorBundle("invalidListEntry", new Object[] { value }), exception);
 78  0
             }
 79  0
     }
 80  
     
 81  
     public TextBundle verboseConstraints() {
 82  0
         if ( entryValueDescriptor == Unconstrained.UNCONSTRAINED ) {
 83  0
             return new TextBundle("unconstrainedList");
 84  
         } else {
 85  0
             TextBundle verbosedEntryContrstaints = entryValueDescriptor.verboseConstraints();
 86  0
             return new TextBundle("constrainedList", new String[] { verbosedEntryContrstaints.getText(Locale.getDefault()) });
 87  
         }
 88  
     }
 89  
 }