1 package org.apache.torque.om;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Date;
23
24 /***
25 * This class can be used as an ObjectKey to uniquely identify an
26 * object within an application where the id is a Date.
27 *
28 * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
29 * @version $Id: DateKey.java 476550 2006-11-18 16:08:37Z tfischer $
30 */
31 public class DateKey extends SimpleKey
32 {
33 /***
34 * Serial version
35 */
36 private static final long serialVersionUID = 3102583536685348517L;
37
38 /***
39 * Creates an DateKey whose internal representation will be
40 * set later, through a set method
41 */
42 public DateKey()
43 {
44 }
45
46 /***
47 * Creates a DateKey whose internal representation is a Date
48 * given by the long number given by the String
49 *
50 * @param key the key value
51 * @throws NumberFormatException if key is not valid
52 */
53 public DateKey(String key)
54 {
55 this.key = new Date(Long.parseLong(key));
56 }
57
58 /***
59 * Creates a DateKey
60 *
61 * @param key the key value
62 */
63 public DateKey(Date key)
64 {
65 this.key = key;
66 }
67
68 /***
69 * Creates a DateKey that is equivalent to key.
70 *
71 * @param key the key value
72 */
73 public DateKey(DateKey key)
74 {
75 if (key != null)
76 {
77 this.key = key.getValue();
78 }
79 else
80 {
81 this.key = null;
82 }
83 }
84
85 /***
86 * Sets the internal representation to a String
87 *
88 * @param key the key value
89 */
90 public void setValue(String key)
91 {
92 this.key = new Date(Long.parseLong(key));
93 }
94
95 /***
96 * Sets the internal representation to the same object used by key.
97 *
98 * @param key the key value
99 */
100 public void setValue(DateKey key)
101 {
102 if (key != null)
103 {
104 this.key = key.getValue();
105 }
106 else
107 {
108 this.key = null;
109 }
110 }
111
112 /***
113 * Access the underlying Date object.
114 *
115 * @return a <code>Date</code> value
116 */
117 public Date getDate()
118 {
119 return (Date) key;
120 }
121
122 /***
123 * keyObj is equal to this DateKey if keyObj is a DateKey or String
124 * that contains the same information this key contains. Two ObjectKeys
125 * that both contain null values are not considered equal.
126 *
127 * @param keyObj the comparison value
128 * @return whether the two objects are equal
129 */
130 public boolean equals(Object keyObj)
131 {
132 boolean isEqual = false;
133
134 if (key != null)
135 {
136 if (keyObj instanceof String)
137 {
138 isEqual = toString().equals(keyObj);
139 }
140
141
142 else if (keyObj instanceof DateKey)
143 {
144 Object obj = ((DateKey) keyObj).getValue();
145 isEqual = key.equals(obj);
146 }
147 }
148 return isEqual;
149 }
150
151 /***
152 * get a String representation
153 *
154 * @return a String representation of the Date or an empty String if the
155 * Date is null
156 */
157 public String toString()
158 {
159 Date dt = getDate();
160 return (dt == null ? "" : Long.toString(dt.getTime()));
161 }
162 }