View Javadoc

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        super();
44     }
45  
46     public MapToDateMorpher( Date defaultValue )
47     {
48        super( true );
49        this.defaultValue = defaultValue;
50     }
51  
52     public boolean equals( Object obj )
53     {
54        if( this == obj ){
55           return true;
56        }
57        if( obj == null ){
58           return false;
59        }
60  
61        if( !(obj instanceof MapToDateMorpher) ){
62           return false;
63        }
64  
65        MapToDateMorpher other = (MapToDateMorpher) obj;
66        EqualsBuilder builder = new EqualsBuilder();
67        if( isUseDefault() && other.isUseDefault() ){
68           builder.append( getDefaultValue(), other.getDefaultValue() );
69           return builder.isEquals();
70        }else if( !isUseDefault() && !other.isUseDefault() ){
71           return builder.isEquals();
72        }else{
73           return false;
74        }
75     }
76  
77     /**
78      * Returns the default value for this Morpher.
79      */
80     public Date getDefaultValue()
81     {
82        return (Date) defaultValue.clone();
83     }
84  
85     public int hashCode()
86     {
87        HashCodeBuilder builder = new HashCodeBuilder();
88        if( isUseDefault() ){
89           builder.append( getDefaultValue() );
90        }
91        return builder.toHashCode();
92     }
93  
94     public Object morph( Object value )
95     {
96        if( value == null ){
97           return null;
98        }
99  
100       if( Date.class.isAssignableFrom( value.getClass() ) ){
101          return (Date) value;
102       }
103 
104       if( !supports( value.getClass() ) ){
105          throw new MorphException( value.getClass() + " is not supported" );
106       }
107 
108       Map map = (Map) value;
109       if( map.isEmpty() ){
110          if( isUseDefault() ){
111             return defaultValue;
112          }else{
113             throw new MorphException( "Unable to parse the date " + value );
114          }
115       }
116 
117       Calendar c = Calendar.getInstance();
118       c.set( Calendar.YEAR, getValue( map, "year" ) );
119       c.set( Calendar.MONTH, getValue( map, "month" ) );
120       c.set( Calendar.DATE, getValue( map, "day" ) );
121       c.set( Calendar.HOUR_OF_DAY, getValue( map, "hour" ) );
122       c.set( Calendar.MINUTE, getValue( map, "minutes" ) );
123       c.set( Calendar.SECOND, getValue( map, "seconds" ) );
124       c.set( Calendar.MILLISECOND, getValue( map, "milliseconds" ) );
125       return c.getTime();
126    }
127 
128    public Class morphsTo()
129    {
130       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       this.defaultValue = (Date) defaultValue.clone();
141    }
142 
143    public boolean supports( Class clazz )
144    {
145       return clazz != null && Map.class.isAssignableFrom( clazz );
146    }
147 
148    private int getValue( Map map, String key )
149    {
150       Object value = map.get( key );
151       if( value == null || !(value instanceof Number) ){
152          return 0;
153       }
154 
155       Number n = (Number) value;
156       return n.intValue();
157    }
158 }