Coverage Report - net.sf.ezmorph.object.DateMorpher
 
Classes in this File Line Coverage Branch Coverage Complexity
DateMorpher
92%
69/75
85%
39/46
3.308
 
 1  
 /*
 2  
  * Copyright 2006-2007 the original author or authors.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package net.sf.ezmorph.object;
 18  
 
 19  
 import java.text.ParseException;
 20  
 import java.text.SimpleDateFormat;
 21  
 import java.util.Date;
 22  
 import java.util.Locale;
 23  
 
 24  
 import net.sf.ezmorph.MorphException;
 25  
 
 26  
 import org.apache.commons.lang.builder.EqualsBuilder;
 27  
 import org.apache.commons.lang.builder.HashCodeBuilder;
 28  
 
 29  
 /**
 30  
  * Morphs a String to a Date.<br>
 31  
  * <p>
 32  
  * This morpher will iterate through the supplied formats until one succeeds or
 33  
  * the default value is returned (if default value is configured).
 34  
  * </p>
 35  
  *
 36  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 37  
  */
 38  
 public final class DateMorpher extends AbstractObjectMorpher
 39  
 {
 40  
    private Date defaultValue;
 41  
    private String[] formats;
 42  
    private boolean lenient;
 43  
    private Locale locale;
 44  
 
 45  
    /**
 46  
     * @param formats a list of formats this morpher supports.
 47  
     */
 48  
    public DateMorpher( String[] formats )
 49  
    {
 50  136
       this( formats, Locale.getDefault(), false );
 51  128
    }
 52  
 
 53  
    /**
 54  
     * @param formats a list of formats this morpher supports.
 55  
     * @param lenient if the parsing should be lenient or not.
 56  
     */
 57  
    public DateMorpher( String[] formats, boolean lenient )
 58  
    {
 59  8
       this( formats, Locale.getDefault(), lenient );
 60  0
    }
 61  
 
 62  
    /**
 63  
     * @param formats a list of formats this morpher supports.
 64  
     * @param defaultValue return value if the value to be morphed is null.
 65  
     */
 66  
    public DateMorpher( String[] formats, Date defaultValue )
 67  
    {
 68  120
       this( formats, defaultValue, Locale.getDefault(), false );
 69  120
    }
 70  
 
 71  
    /**
 72  
     * @param formats a list of formats this morpher supports.
 73  
     * @param defaultValue return value if the value to be morphed is null.
 74  
     * @param locale the Locale used to parse each format.
 75  
     * @param lenient if the parsing should be lenient or not.
 76  
     */
 77  
    public DateMorpher( String[] formats, Date defaultValue, Locale locale, boolean lenient )
 78  
    {
 79  120
       super( true );
 80  120
       if( formats == null || formats.length == 0 ){
 81  0
          throw new MorphException( "invalid array of formats" );
 82  
       }
 83  
       // should use defensive copying ?
 84  120
       this.formats = formats;
 85  
 
 86  120
       if( locale == null ){
 87  0
          this.locale = Locale.getDefault();
 88  
       }else{
 89  120
          this.locale = locale;
 90  
       }
 91  
 
 92  120
       this.lenient = lenient;
 93  120
       setDefaultValue( defaultValue );
 94  120
    }
 95  
 
 96  
    /**
 97  
     * @param formats a list of formats this morpher supports.
 98  
     * @param locale the Locale used to parse each format.
 99  
     */
 100  
    public DateMorpher( String[] formats, Locale locale )
 101  
    {
 102  0
       this( formats, locale, false );
 103  0
    }
 104  
 
 105  
    /**
 106  
     * @param formats a list of formats this morpher supports.
 107  
     * @param locale the Locale used to parse each format.
 108  
     * @param lenient if the parsing should be lenient or not.
 109  
     */
 110  108
    public DateMorpher( String[] formats, Locale locale, boolean lenient )
 111  36
    {
 112  144
       if( formats == null || formats.length == 0 ){
 113  16
          throw new MorphException( "invalid array of formats" );
 114  
       }
 115  
       // should use defensive copying ?
 116  128
       this.formats = formats;
 117  
 
 118  128
       if( locale == null ){
 119  0
          this.locale = Locale.getDefault();
 120  
       }else{
 121  128
          this.locale = locale;
 122  
       }
 123  
 
 124  128
       this.lenient = lenient;
 125  128
    }
 126  
 
 127  
    public boolean equals( Object obj )
 128  
    {
 129  28
       if( this == obj ){
 130  8
          return true;
 131  
       }
 132  20
       if( obj == null ){
 133  4
          return false;
 134  
       }
 135  
 
 136  16
       if( !(obj instanceof DateMorpher) ){
 137  4
          return false;
 138  
       }
 139  
 
 140  12
       DateMorpher other = (DateMorpher) obj;
 141  12
       EqualsBuilder builder = new EqualsBuilder();
 142  12
       builder.append( formats, other.formats );
 143  12
       builder.append( locale, other.locale );
 144  12
       builder.append( lenient, other.lenient );
 145  12
       if( isUseDefault() && other.isUseDefault() ){
 146  4
          builder.append( getDefaultValue(), other.getDefaultValue() );
 147  4
          return builder.isEquals();
 148  8
       }else if( !isUseDefault() && !other.isUseDefault() ){
 149  4
          return builder.isEquals();
 150  
       }else{
 151  4
          return false;
 152  
       }
 153  
    }
 154  
 
 155  
    /**
 156  
     * Returns the default value for this Morpher.
 157  
     */
 158  
    public Date getDefaultValue()
 159  
    {
 160  20
       return (Date) defaultValue.clone();
 161  
    }
 162  
 
 163  
    public int hashCode()
 164  
    {
 165  24
       HashCodeBuilder builder = new HashCodeBuilder();
 166  24
       builder.append( formats );
 167  24
       builder.append( locale );
 168  24
       builder.append( lenient );
 169  24
       if( isUseDefault() ){
 170  12
          builder.append( getDefaultValue() );
 171  
       }
 172  24
       return builder.toHashCode();
 173  
    }
 174  
 
 175  
    public Object morph( Object value )
 176  
    {
 177  28
       if( value == null ){
 178  4
          return null;
 179  
       }
 180  
 
 181  26
       if( Date.class.isAssignableFrom( value.getClass() ) ){
 182  4
          return (Date) value;
 183  
       }
 184  
 
 185  20
       if( !supports( value.getClass() ) ){
 186  4
          throw new MorphException( value.getClass() + " is not supported" );
 187  
       }
 188  
 
 189  16
       String strValue = (String) value;
 190  16
       SimpleDateFormat dateParser = null;
 191  
 
 192  32
       for( int i = 0; i < formats.length; i++ ){
 193  24
          if( dateParser == null ){
 194  16
             dateParser = new SimpleDateFormat( formats[i], locale );
 195  
          }else{
 196  8
             dateParser.applyPattern( formats[i] );
 197  
          }
 198  24
          dateParser.setLenient( lenient );
 199  
          try{
 200  24
             return dateParser.parse( strValue.toLowerCase() );
 201  
          }
 202  16
          catch( ParseException pe ){
 203  
             // ignore exception, try the next format
 204  
          }
 205  
       }
 206  
 
 207  
       // unable to parse the date
 208  8
       if( isUseDefault() ){
 209  4
          return defaultValue;
 210  
       }else{
 211  4
          throw new MorphException( "Unable to parse the date " + value );
 212  
       }
 213  
    }
 214  
 
 215  
    public Class morphsTo()
 216  
    {
 217  8
       return Date.class;
 218  
    }
 219  
 
 220  
    /**
 221  
     * Sets the defaultValue to use if the value to be morphed is null.
 222  
     *
 223  
     * @param defaultValue return value if the value to be morphed is null
 224  
     */
 225  
    public void setDefaultValue( Date defaultValue )
 226  
    {
 227  124
       this.defaultValue = (Date) defaultValue.clone();
 228  124
    }
 229  
 
 230  
    public boolean supports( Class clazz )
 231  
    {
 232  24
       return String.class.isAssignableFrom( clazz );
 233  
    }
 234  
 }