Coverage Report - net.sf.ezmorph.object.MapToDateMorpher
 
Classes in this File Line Coverage Branch Coverage Complexity
MapToDateMorpher
96%
51/53
80%
32/40
3.3
 
 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.util.Calendar;
 20  
 import java.util.Date;
 21  
 import java.util.Map;
 22  
 
 23  
 import net.sf.ezmorph.MorphException;
 24  
 
 25  
 import org.apache.commons.lang.builder.EqualsBuilder;
 26  
 import org.apache.commons.lang.builder.HashCodeBuilder;
 27  
 
 28  
 /**
 29  
  * Morphs a Map into a Date.<br>
 30  
  * The Map should have at least one of the following keys
 31  
  * [yer,month,day,hour,minutes,seconds,milliseconds] and the values should be
 32  
  * instances of Number. Any key that is not defined will have zero (0) assigned
 33  
  * as its value.
 34  
  *
 35  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 36  
  */
 37  
 public class MapToDateMorpher extends AbstractObjectMorpher
 38  
 {
 39  
    private Date defaultValue;
 40  
 
 41  
    public MapToDateMorpher()
 42  
    {
 43  100
       super();
 44  100
    }
 45  
 
 46  
    public MapToDateMorpher( Date defaultValue )
 47  
    {
 48  96
       super( true );
 49  96
       this.defaultValue = defaultValue;
 50  96
    }
 51  
 
 52  
    public boolean equals( Object obj )
 53  
    {
 54  32
       if( this == obj ){
 55  8
          return true;
 56  
       }
 57  24
       if( obj == null ){
 58  4
          return false;
 59  
       }
 60  
 
 61  20
       if( !(obj instanceof MapToDateMorpher) ){
 62  8
          return false;
 63  
       }
 64  
 
 65  12
       MapToDateMorpher other = (MapToDateMorpher) obj;
 66  12
       EqualsBuilder builder = new EqualsBuilder();
 67  12
       if( isUseDefault() && other.isUseDefault() ){
 68  4
          builder.append( getDefaultValue(), other.getDefaultValue() );
 69  4
          return builder.isEquals();
 70  8
       }else if( !isUseDefault() && !other.isUseDefault() ){
 71  4
          return builder.isEquals();
 72  
       }else{
 73  4
          return false;
 74  
       }
 75  
    }
 76  
 
 77  
    /**
 78  
     * Returns the default value for this Morpher.
 79  
     */
 80  
    public Date getDefaultValue()
 81  
    {
 82  20
       return (Date) defaultValue.clone();
 83  
    }
 84  
 
 85  
    public int hashCode()
 86  
    {
 87  24
       HashCodeBuilder builder = new HashCodeBuilder();
 88  24
       if( isUseDefault() ){
 89  12
          builder.append( getDefaultValue() );
 90  
       }
 91  24
       return builder.toHashCode();
 92  
    }
 93  
 
 94  
    public Object morph( Object value )
 95  
    {
 96  24
       if( value == null ){
 97  4
          return null;
 98  
       }
 99  
 
 100  22
       if( Date.class.isAssignableFrom( value.getClass() ) ){
 101  4
          return (Date) value;
 102  
       }
 103  
 
 104  16
       if( !supports( value.getClass() ) ){
 105  4
          throw new MorphException( value.getClass() + " is not supported" );
 106  
       }
 107  
 
 108  12
       Map map = (Map) value;
 109  12
       if( map.isEmpty() ){
 110  4
          if( isUseDefault() ){
 111  4
             return defaultValue;
 112  
          }else{
 113  0
             throw new MorphException( "Unable to parse the date " + value );
 114  
          }
 115  
       }
 116  
 
 117  8
       Calendar c = Calendar.getInstance();
 118  8
       c.set( Calendar.YEAR, getValue( map, "year" ) );
 119  8
       c.set( Calendar.MONTH, getValue( map, "month" ) );
 120  8
       c.set( Calendar.DATE, getValue( map, "day" ) );
 121  8
       c.set( Calendar.HOUR_OF_DAY, getValue( map, "hour" ) );
 122  8
       c.set( Calendar.MINUTE, getValue( map, "minutes" ) );
 123  8
       c.set( Calendar.SECOND, getValue( map, "seconds" ) );
 124  8
       c.set( Calendar.MILLISECOND, getValue( map, "milliseconds" ) );
 125  8
       return c.getTime();
 126  
    }
 127  
 
 128  
    public Class morphsTo()
 129  
    {
 130  4
       return Date.class;
 131  
    }
 132  
 
 133  
    /**
 134  
     * Sets the defaultValue to use if the value to be morphed is null.
 135  
     *
 136  
     * @param defaultValue return value if the value to be morphed is null
 137  
     */
 138  
    public void setDefaultValue( Date defaultValue )
 139  
    {
 140  4
       this.defaultValue = (Date) defaultValue.clone();
 141  4
    }
 142  
 
 143  
    public boolean supports( Class clazz )
 144  
    {
 145  20
       return clazz != null && Map.class.isAssignableFrom( clazz );
 146  
    }
 147  
 
 148  
    private int getValue( Map map, String key )
 149  
    {
 150  56
       Object value = map.get( key );
 151  56
       if( value == null || !(value instanceof Number) ){
 152  0
          return 0;
 153  
       }
 154  
 
 155  56
       Number n = (Number) value;
 156  56
       return n.intValue();
 157  
    }
 158  
 }