View Javadoc

1   package org.apache.torque.om;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Date;
20  
21  /***
22   * This class can be used as an ObjectKey to uniquely identify an
23   * object within an application where the id is a Date.
24   *
25   * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
26   * @version $Id: DateKey.java 239630 2005-08-24 12:25:32Z henning $
27   */
28  public class DateKey extends SimpleKey
29  {
30  
31      /***
32       * Creates an DateKey whose internal representation will be
33       * set later, through a set method
34       */
35      public DateKey()
36      {
37      }
38  
39      /***
40       * Creates a DateKey whose internal representation is a Date
41       * given by the long number given by the String
42       *
43       * @param key the key value
44       * @throws NumberFormatException if key is not valid
45       */
46      public DateKey(String key) throws NumberFormatException
47      {
48          this.key = new Date(Long.parseLong(key));
49      }
50  
51      /***
52       * Creates a DateKey
53       *
54       * @param key the key value
55       */
56      public DateKey(Date key)
57      {
58          this.key = key;
59      }
60  
61      /***
62       * Creates a DateKey that is equivalent to key.
63       *
64       * @param key the key value
65       */
66      public DateKey(DateKey key)
67      {
68          if (key != null)
69          {
70              this.key = key.getValue();
71          }
72          else
73          {
74              this.key = null;
75          }
76      }
77  
78      /***
79       * Sets the internal representation to a String
80       *
81       * @param key the key value
82       */
83      public void setValue(String key)
84      {
85          this.key = new Date(Long.parseLong(key));
86      }
87  
88      /***
89       * Sets the internal representation to the same object used by key.
90       *
91       * @param key the key value
92       */
93      public void setValue(DateKey key)
94      {
95          if (key != null)
96          {
97              this.key = key.getValue();
98          }
99          else
100         {
101             this.key = null;
102         }
103     }
104 
105     /***
106      * Access the underlying Date object.
107      *
108      * @return a <code>Date</code> value
109      */
110     public Date getDate()
111     {
112         return (Date) key;
113     }
114 
115     /***
116      * keyObj is equal to this DateKey if keyObj is a DateKey or String
117      * that contains the same information this key contains.  Two ObjectKeys
118      * that both contain null values are not considered equal.
119      *
120      * @param keyObj the comparison value
121      * @return whether the two objects are equal
122      */
123     public boolean equals(Object keyObj)
124     {
125         boolean isEqual = false;
126 
127         if (key != null)
128         {
129             if (keyObj instanceof String)
130             {
131                 isEqual =  toString().equals(keyObj);
132             }
133             // check against a DateKey. Two keys are equal, if their
134             // internal keys equivalent.
135             else if (keyObj instanceof DateKey)
136             {
137                 Object obj = ((DateKey) keyObj).getValue();
138                 isEqual =  key.equals(obj);
139             }
140         }
141         return isEqual;
142     }
143 
144     /***
145      * get a String representation
146      *
147      * @return a String representation of the Date or an empty String if the
148      *          Date is null
149      */
150     public String toString()
151     {
152         Date dt = getDate();
153         return (dt == null ? "" : Long.toString(dt.getTime()));
154     }
155 }