001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ---------------------
028: * FixedMillisecond.java
029: * ---------------------
030: * (C) Copyright 2002-2006 by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: FixedMillisecond.java,v 1.4.2.2 2006/10/06 14:00:13 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 19-Mar-2002 : Version 1, based on original Millisecond implementation (DG);
040: * 24-Jun-2002 : Removed unnecessary imports (DG);
041: * 10-Sep-2002 : Added getSerialIndex() method (DG);
042: * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
043: * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented
044: * Serializable (DG);
045: * 21-Oct-2003 : Added hashCode() method (DG);
046: * ------------- JFREECHART 1.0.x ---------------------------------------------
047: * 06-Oct-2006 : Added peg() method (DG);
048: *
049: */
050:
051: package org.jfree.data.time;
052:
053: import java.io.Serializable;
054: import java.util.Calendar;
055: import java.util.Date;
056:
057: /**
058: * Wrapper for a <code>java.util.Date</code> object that allows it to be used
059: * as a {@link RegularTimePeriod}. This class is immutable, which is a
060: * requirement for all {@link RegularTimePeriod} subclasses.
061: */
062: public class FixedMillisecond extends RegularTimePeriod implements
063: Serializable {
064:
065: /** For serialization. */
066: private static final long serialVersionUID = 7867521484545646931L;
067:
068: /** The millisecond. */
069: private Date time;
070:
071: /**
072: * Constructs a millisecond based on the current system time.
073: */
074: public FixedMillisecond() {
075: this (new Date());
076: }
077:
078: /**
079: * Constructs a millisecond.
080: *
081: * @param millisecond the millisecond (same encoding as java.util.Date).
082: */
083: public FixedMillisecond(long millisecond) {
084: this (new Date(millisecond));
085: }
086:
087: /**
088: * Constructs a millisecond.
089: *
090: * @param time the time.
091: */
092: public FixedMillisecond(Date time) {
093: this .time = time;
094: }
095:
096: /**
097: * Returns the date/time.
098: *
099: * @return The date/time.
100: */
101: public Date getTime() {
102: return this .time;
103: }
104:
105: /**
106: * This method is overridden to do nothing.
107: *
108: * @param calendar ignored
109: *
110: * @since 1.0.3
111: */
112: public void peg(Calendar calendar) {
113: // nothing to do
114: }
115:
116: /**
117: * Returns the millisecond preceding this one.
118: *
119: * @return The millisecond preceding this one.
120: */
121: public RegularTimePeriod previous() {
122: RegularTimePeriod result = null;
123: long t = this .time.getTime();
124: if (t != Long.MIN_VALUE) {
125: result = new FixedMillisecond(t - 1);
126: }
127: return result;
128: }
129:
130: /**
131: * Returns the millisecond following this one.
132: *
133: * @return The millisecond following this one.
134: */
135: public RegularTimePeriod next() {
136: RegularTimePeriod result = null;
137: long t = this .time.getTime();
138: if (t != Long.MAX_VALUE) {
139: result = new FixedMillisecond(t + 1);
140: }
141: return result;
142: }
143:
144: /**
145: * Tests the equality of this object against an arbitrary Object.
146: *
147: * @param object the object to compare
148: *
149: * @return A boolean.
150: */
151: public boolean equals(Object object) {
152: if (object instanceof FixedMillisecond) {
153: FixedMillisecond m = (FixedMillisecond) object;
154: return this .time.equals(m.getTime());
155: } else {
156: return false;
157: }
158:
159: }
160:
161: /**
162: * Returns a hash code for this object instance.
163: *
164: * @return A hash code.
165: */
166: public int hashCode() {
167: return this .time.hashCode();
168: }
169:
170: /**
171: * Returns an integer indicating the order of this Millisecond object
172: * relative to the specified
173: * object: negative == before, zero == same, positive == after.
174: *
175: * @param o1 the object to compare.
176: *
177: * @return negative == before, zero == same, positive == after.
178: */
179: public int compareTo(Object o1) {
180:
181: int result;
182: long difference;
183:
184: // CASE 1 : Comparing to another Second object
185: // -------------------------------------------
186: if (o1 instanceof FixedMillisecond) {
187: FixedMillisecond t1 = (FixedMillisecond) o1;
188: difference = this .time.getTime() - t1.time.getTime();
189: if (difference > 0) {
190: result = 1;
191: } else {
192: if (difference < 0) {
193: result = -1;
194: } else {
195: result = 0;
196: }
197: }
198: }
199:
200: // CASE 2 : Comparing to another TimePeriod object
201: // -----------------------------------------------
202: else if (o1 instanceof RegularTimePeriod) {
203: // more difficult case - evaluate later...
204: result = 0;
205: }
206:
207: // CASE 3 : Comparing to a non-TimePeriod object
208: // ---------------------------------------------
209: else {
210: // consider time periods to be ordered after general objects
211: result = 1;
212: }
213:
214: return result;
215:
216: }
217:
218: /**
219: * Returns the first millisecond of the time period.
220: *
221: * @return The first millisecond of the time period.
222: */
223: public long getFirstMillisecond() {
224: return this .time.getTime();
225: }
226:
227: /**
228: * Returns the first millisecond of the time period.
229: *
230: * @param calendar the calendar.
231: *
232: * @return The first millisecond of the time period.
233: */
234: public long getFirstMillisecond(Calendar calendar) {
235: return this .time.getTime();
236: }
237:
238: /**
239: * Returns the last millisecond of the time period.
240: *
241: * @return The last millisecond of the time period.
242: */
243: public long getLastMillisecond() {
244: return this .time.getTime();
245: }
246:
247: /**
248: * Returns the last millisecond of the time period.
249: *
250: * @param calendar the calendar.
251: *
252: * @return The last millisecond of the time period.
253: */
254: public long getLastMillisecond(Calendar calendar) {
255: return this .time.getTime();
256: }
257:
258: /**
259: * Returns the millisecond closest to the middle of the time period.
260: *
261: * @return The millisecond closest to the middle of the time period.
262: */
263: public long getMiddleMillisecond() {
264: return this .time.getTime();
265: }
266:
267: /**
268: * Returns the millisecond closest to the middle of the time period.
269: *
270: * @param calendar the calendar.
271: *
272: * @return The millisecond closest to the middle of the time period.
273: */
274: public long getMiddleMillisecond(Calendar calendar) {
275: return this .time.getTime();
276: }
277:
278: /**
279: * Returns a serial index number for the millisecond.
280: *
281: * @return The serial index number.
282: */
283: public long getSerialIndex() {
284: return this.time.getTime();
285: }
286:
287: }
|