View Javadoc

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.sql.Types;
23  import java.util.Date;
24  
25  /**
26   * This class can be used as an ObjectKey to uniquely identify an
27   * object within an application where the id is a Date.
28   *
29   * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
30   * @version $Id: DateKey.java 1351125 2012-06-17 16:51:03Z tv $
31   */
32  public class DateKey extends SimpleKey
33  {
34      /**
35       * Serial version
36       */
37      private static final long serialVersionUID = 3102583536685348517L;
38  
39      /**
40       * Creates an DateKey whose internal representation will be
41       * set later, through a set method
42       */
43      public DateKey()
44      {
45          // empty
46      }
47  
48      /**
49       * Creates a DateKey whose internal representation is a Date
50       * given by the long number given by the String
51       *
52       * @param key the key value
53       * @throws NumberFormatException if key is not valid
54       */
55      public DateKey(String key)
56      {
57          if (key != null)
58          {
59              this.key = new Date(Long.parseLong(key));
60          }
61          else
62          {
63              this.key = null;
64          }
65      }
66  
67      /**
68       * Creates a DateKey
69       *
70       * @param key the key value
71       */
72      public DateKey(Date key)
73      {
74          this.key = key;
75      }
76  
77      /**
78       * Creates a DateKey that is equivalent to key.
79       *
80       * @param key the key value
81       */
82      public DateKey(DateKey key)
83      {
84          if (key != null)
85          {
86              this.key = key.getValue();
87          }
88          else
89          {
90              this.key = null;
91          }
92      }
93  
94      /**
95       * Sets the internal representation to a String
96       *
97       * @param key the key value
98       */
99      public void setValue(String key)
100     {
101         if (key != null)
102         {
103             this.key = new Date(Long.parseLong(key));
104         }
105         else
106         {
107             this.key = null;
108         }
109     }
110 
111     /**
112      * Sets the internal representation to the same object used by key.
113      *
114      * @param key the key value
115      */
116     public void setValue(DateKey key)
117     {
118         if (key != null)
119         {
120             this.key = key.getValue();
121         }
122         else
123         {
124             this.key = null;
125         }
126     }
127 
128     /**
129      * Returns the JDBC type of the key
130      * as defined in <code>java.sql.Types</code>.
131      *
132      * @return <code>Types.TIMESTAMP</code>.
133      */
134     public int getJdbcType()
135     {
136         return Types.TIMESTAMP;
137     }
138 
139     /**
140      * Access the underlying Date object.
141      *
142      * @return a <code>Date</code> value
143      */
144     public Date getDate()
145     {
146         return (Date) key;
147     }
148 
149     /**
150      * Get a String representation for this key.
151      *
152      * @return a String representation of the Date or an empty String if the
153      *          Date is null
154      */
155     @Override
156     public String toString()
157     {
158         Date dt = getDate();
159         return (dt == null ? "" : Long.toString(dt.getTime()));
160     }
161 }