1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33
34
35
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
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
135
136
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 }