KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > util > TimeStampedValue


1 /*
2  * TimeStampedValue, wrapper class for values marked with a timestamp.
3  * Copyright (C) Achim Westermann, created on 09.09.2004, 12:38:21
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  *
22  */

23 package info.monitorenter.util;
24
25 import java.util.Map JavaDoc;
26
27 /**
28  * Simple wrapper around a time in ms and a value Object.
29  * <p>
30  *
31  * The key is the time in ms and may be used in a Map.
32  * <code>{@link #compareTo(java.lang.Object)}</code> compares the key.
33  *
34  * @author <a HREF='mailto:Achim.Westermann@gmx.de'>Achim Westermann</a>
35  *
36  * @version $Revision: 1.1 $
37  */

38 public final class TimeStampedValue implements Map.Entry JavaDoc, Comparable JavaDoc {
39   /**
40    * The timestamp (difference, measured in milliseconds, between the current
41    * time and midnight, January 1, 1970 UTC).
42    */

43   private long m_key;
44
45   /** The timestamped value. */
46   private Object JavaDoc m_value;
47
48   /**
49    * Creates an instance with the given timestamp key and the value to
50    * timestamp.
51    * <p>
52    *
53    * @param key
54    * the timestamp (difference, measured in milliseconds, between the
55    * current time and midnight, January 1, 1970 UTC).
56    *
57    * @param value
58    * the value to timestamp.
59    */

60   public TimeStampedValue(final long key, final Object JavaDoc value) {
61     this.m_key = key;
62     this.m_value = value;
63   }
64
65   /**
66    * Creates an instance for the given value that is timestamped with the
67    * current time.
68    * <p>
69    *
70    * @param value
71    * the value to timestamp.
72    *
73    * @see System#currentTimeMillis()
74    */

75   public TimeStampedValue(final Object JavaDoc value) {
76     this(System.currentTimeMillis(), value);
77   }
78
79   /**
80    * Returns the {@link Long} that marks the timestamp (difference, measured in
81    * milliseconds, between the current time and midnight, January 1, 1970 UTC).
82    *
83    * @return the {@link Long} that marks the timestamp (difference, measured in
84    * milliseconds, between the current time and midnight, January 1,
85    * 1970 UTC).
86    *
87    * @see java.util.Map$Entry#getKey()
88    */

89   public Object JavaDoc getKey() {
90     return new Long JavaDoc(this.m_key);
91   }
92
93   /**
94    * Returns the timestamped instance.
95    * <p>
96    *
97    * @return the timestamped instance.
98    *
99    * @see java.util.Map$Entry#getValue()
100    */

101   public Object JavaDoc getValue() {
102     return this.m_value;
103   }
104
105   /**
106    * Returns the timestamp (difference, measured in milliseconds, between the
107    * current time and midnight, January 1, 1970 UTC).
108    * <p>
109    *
110    * @return the timestamp (difference, measured in milliseconds, between the
111    * current time and midnight, January 1, 1970 UTC).
112    *
113    */

114   public long getTime() {
115     return this.m_key;
116   }
117
118   /**
119    * Returns true, if o!=null && this.key.equals(0) &&
120    * o.insanceOf(TimeStampedValue).
121    *
122    * @param o
123    * the {@link TimeStampedValue} to compare this instance to.
124    *
125    * @return true, if o!=null && this.key.equals(0) &&
126    * o.insanceOf(TimeStampedValue).
127    */

128   public boolean equals(final Object JavaDoc o) {
129     if (o == null) {
130       return false;
131     }
132     TimeStampedValue compare = null;
133     try {
134       compare = (TimeStampedValue) o;
135     } catch (ClassCastException JavaDoc e) {
136       return false;
137     }
138     if (this.getTime() == compare.getTime()) {
139       return true;
140     }
141     return false;
142   }
143
144   /**
145    *
146    * @see java.lang.Object#hashCode()
147    */

148   public int hashCode() {
149     int result = (int) this.m_key;
150     if (this.m_key > Integer.MAX_VALUE) {
151       result = -result;
152     }
153     return result;
154
155   }
156
157   /**
158    * Assigns a different value to the timestamp.
159    * <p>
160    *
161    * @param value
162    * the new value to be marked with this timestamp.
163    *
164    * @return the previous value that was contained.
165    *
166    * @see java.util.Map$Entry#setValue(java.lang.Object)
167    */

168   public Object JavaDoc setValue(final Object JavaDoc value) {
169     Object JavaDoc ret = this.m_value;
170     this.m_value = value;
171     return ret;
172   }
173
174   /**
175    * Compares the given {@link TimeStampedValue} to this by the internal
176    * {@link #getTime()}.
177    * <p>
178    *
179    * @see java.lang.Comparable
180    */

181   public int compareTo(final java.lang.Object JavaDoc obj) {
182     TimeStampedValue other = (TimeStampedValue) obj;
183     if (this.m_key < other.m_key) {
184       return -1;
185     }
186     if (this.m_key == other.m_key) {
187       return 0;
188     }
189     return 1;
190   }
191
192   /**
193    * Returns wethter the internal timestamp marks a time in the past or not.
194    * <p>
195    *
196    * For normal a timestamp represents a value regarded at a time. But it is
197    * also thinkable to mark a value for expiration in the future. This method
198    * returns true if the internal time- representing key is smaller than the
199    * actual time.
200    * <p>
201    *
202    * @return true if the internal timestamp marks a moment in the past, false
203    * else.
204    */

205   public boolean isPast() {
206     return this.m_key < System.currentTimeMillis();
207   }
208 }
209
Popular Tags