KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > om > DateKey


1 package org.apache.torque.om;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements. See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership. The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied. See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */

21
22 import java.util.Date JavaDoc;
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 JavaDoc key)
54     {
55         this.key = new Date JavaDoc(Long.parseLong(key));
56     }
57
58     /**
59      * Creates a DateKey
60      *
61      * @param key the key value
62      */

63     public DateKey(Date JavaDoc 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 JavaDoc key)
91     {
92         this.key = new Date JavaDoc(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 JavaDoc getDate()
118     {
119         return (Date JavaDoc) 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 JavaDoc keyObj)
131     {
132         boolean isEqual = false;
133
134         if (key != null)
135         {
136             if (keyObj instanceof String JavaDoc)
137             {
138                 isEqual = toString().equals(keyObj);
139             }
140             // check against a DateKey. Two keys are equal, if their
141
// internal keys equivalent.
142
else if (keyObj instanceof DateKey)
143             {
144                 Object JavaDoc 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 JavaDoc toString()
158     {
159         Date JavaDoc dt = getDate();
160         return (dt == null ? "" : Long.toString(dt.getTime()));
161     }
162 }
163
Popular Tags