Coverage Report - org.apache.commons.contract.constraints.DateConstraints
 
Classes in this File Line Coverage Branch Coverage Complexity
DateConstraints
0%
0/45
0%
0/12
2.2
 
 1  
 package org.apache.commons.contract.constraints;
 2  
 
 3  
 import java.text.DateFormat;
 4  
 import java.text.ParseException;
 5  
 import java.text.SimpleDateFormat;
 6  
 import java.util.ArrayList;
 7  
 import java.util.Date;
 8  
 import java.util.List;
 9  
 
 10  
 import org.apache.commons.contract.Context;
 11  
 import org.apache.commons.i18n.bundles.ErrorBundle;
 12  
 import org.apache.commons.i18n.bundles.TextBundle;
 13  
 
 14  
 public class DateConstraints implements Constraints {
 15  0
     public static final DateConstraints UNCONSTRAINED = new DateConstraints();
 16  
 
 17  
     protected boolean constrained;
 18  0
     protected List allowedValues = new ArrayList();
 19  
     protected Date earliest, latest;
 20  
     protected String formatPattern;
 21  
 
 22  0
     public DateConstraints() {
 23  0
         this.constrained = false;
 24  0
     }
 25  
 
 26  0
     public DateConstraints(String formatPattern) {
 27  0
         this.constrained = false;
 28  0
         this.formatPattern = formatPattern;
 29  0
     }
 30  
 
 31  0
     public DateConstraints(Date earliest, Date latest) {
 32  0
         constrained = true;
 33  0
         this.earliest = earliest;
 34  0
         this.latest = latest;
 35  0
     }
 36  
 
 37  
     public boolean isConstrained() {
 38  0
         return constrained;
 39  
     }
 40  
 
 41  
     public void setEarliest(Date earliest) {
 42  0
         this.earliest = earliest;
 43  0
     }
 44  
 
 45  
     public void setLatest(Date latest) {
 46  0
         this.latest = latest;
 47  0
     }
 48  
 
 49  
     public void setFormatPattern(String dateFormat) {
 50  0
         this.formatPattern = dateFormat;
 51  0
     }
 52  
     
 53  
     public Object cast(Object value, Context context) throws CastException {
 54  0
             Date date = null;
 55  0
             if ( value instanceof Date) {
 56  0
             return value;
 57  
         } else {
 58  
                 try {
 59  0
                         String valueAsString = (String)StringConstraints.UNCONSTRAINED.cast(value, null);
 60  
                         try {
 61  0
                                 date = new Date(Long.valueOf(valueAsString).longValue());
 62  0
                         } catch ( NumberFormatException exception ) {
 63  
                                 try {
 64  0
                                     if ( formatPattern == null ) {
 65  0
                                         date = DateFormat.getInstance().parse(valueAsString);
 66  
                         } else {
 67  0
                             date = new SimpleDateFormat(formatPattern).parse(valueAsString);
 68  
                         }
 69  0
                                 } catch (ParseException e) {
 70  0
                                     throw new CastException(new ErrorBundle("uncastableDateValue", new Object[] { value }), e);
 71  0
                                 }
 72  0
                         }
 73  0
                 } catch ( CastException exception ) {
 74  0
                         throw new CastException(new ErrorBundle("uncastableDateValue", new Object[] { value }), exception);
 75  0
                 }
 76  
         }
 77  0
         return date;
 78  
     }
 79  
 
 80  
     public void validate(Object value, Context context) throws ValidationException {
 81  0
         Date date = (Date)value;
 82  0
         if ( constrained && ( date.after(latest) || date.before(earliest) ) ) {
 83  0
                 throw new ValidationException(new ErrorBundle("invalidDate", new Object[] { date, earliest, latest }));
 84  
         }
 85  0
     }
 86  
     
 87  
     public TextBundle verboseConstraints() {
 88  0
         if ( constrained ) {
 89  0
             return new TextBundle("constrainedDateRange", new Object[] { earliest, latest });
 90  
         } 
 91  0
         return new TextBundle("unconstrainedDate");
 92  
     }
 93  
 }