KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > time > SimpleTimePeriod


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ---------------------
27  * SimpleTimePeriod.java
28  * ---------------------
29  * (C) Copyright 2002-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: SimpleTimePeriod.java,v 1.5 2005/05/19 10:35:27 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 07-Oct-2002 : Added Javadocs (DG);
39  * 10-Jan-2003 : Renamed TimeAllocation --> SimpleTimePeriod (DG);
40  * 13-Mar-2003 : Added equals() method, and Serializable interface (DG);
41  * 21-Oct-2003 : Added hashCode() method (DG);
42  * 27-Jan-2005 : Implemented Comparable, to enable this class to be used
43  * in the TimeTableXYDataset class (DG);
44  *
45  */

46
47 package org.jfree.data.time;
48
49 import java.io.Serializable JavaDoc;
50 import java.util.Date JavaDoc;
51
52 /**
53  * An arbitrary period of time, measured to millisecond precision using
54  * <code>java.util.Date</code>.
55  * <p>
56  * This class is intentionally immutable (that is, once constructed, you cannot
57  * alter the start and end attributes).
58  */

59 public class SimpleTimePeriod implements TimePeriod, Comparable JavaDoc, Serializable JavaDoc {
60
61     /** For serialization. */
62     private static final long serialVersionUID = 8684672361131829554L;
63     
64     /** The start date/time. */
65     private Date JavaDoc start;
66
67     /** The end date/time. */
68     private Date JavaDoc end;
69
70     /**
71      * Creates a new time allocation.
72      *
73      * @param start the start date/time in milliseconds.
74      * @param end the end date/time in milliseconds.
75      */

76     public SimpleTimePeriod(long start, long end) {
77         this(new Date JavaDoc(start), new Date JavaDoc(end));
78     }
79     
80     /**
81      * Creates a new time allocation.
82      *
83      * @param start the start date/time (<code>null</code> not permitted).
84      * @param end the end date/time (<code>null</code> not permitted).
85      */

86     public SimpleTimePeriod(Date JavaDoc start, Date JavaDoc end) {
87         if (start.getTime() > end.getTime()) {
88             throw new IllegalArgumentException JavaDoc("Requires end >= start.");
89         }
90         this.start = start;
91         this.end = end;
92     }
93
94     /**
95      * Returns the start date/time.
96      *
97      * @return The start date/time (never <code>null</code>).
98      */

99     public Date JavaDoc getStart() {
100         return this.start;
101     }
102
103     /**
104      * Returns the end date/time.
105      *
106      * @return The end date/time (never <code>null</code>).
107      */

108     public Date JavaDoc getEnd() {
109         return this.end;
110     }
111
112     /**
113      * Tests this time period instance for equality with an arbitrary object.
114      * The object is considered equal if it is an instance of {@link TimePeriod}
115      * and it has the same start and end dates.
116      *
117      * @param obj the other object (<code>null</code> permitted).
118      *
119      * @return A boolean.
120      */

121     public boolean equals(Object JavaDoc obj) {
122         if (obj == this) {
123             return true;
124         }
125         if (!(obj instanceof TimePeriod)) {
126             return false;
127         }
128         TimePeriod that = (TimePeriod) obj;
129         if (!this.start.equals(that.getStart())) {
130             return false;
131         }
132         if (!this.end.equals(that.getEnd())) {
133             return false;
134         }
135         return true;
136     }
137     
138     /**
139      * Returns an integer that indicates the relative ordering of two
140      * time periods.
141      *
142      * @param obj the object (<code>null</code> not permitted).
143      *
144      * @return An integer.
145      *
146      * @throws ClassCastException if <code>obj</code> is not an instance of
147      * {@link TimePeriod}.
148      */

149     public int compareTo(Object JavaDoc obj) {
150         TimePeriod that = (TimePeriod) obj;
151         long t0 = getStart().getTime();
152         long t1 = getEnd().getTime();
153         long m0 = t0 + (t1 - t0) / 2L;
154         long t2 = that.getStart().getTime();
155         long t3 = that.getEnd().getTime();
156         long m1 = t2 + (t3 - t2) / 2L;
157         if (m0 < m1) {
158             return -1;
159         }
160         else if (m0 > m1) {
161             return 1;
162         }
163         else {
164             if (t0 < t2) {
165                 return -1;
166             }
167             else if (t0 > t2) {
168                 return 1;
169             }
170             else {
171                 if (t1 < t3) {
172                     return -1;
173                 }
174                 else if (t1 > t3) {
175                     return 1;
176                 }
177                 else {
178                     return 0;
179                 }
180             }
181         }
182     }
183     
184     /**
185      * Returns a hash code for this object instance. The approach described by
186      * Joshua Bloch in "Effective Java" has been used here - see:
187      * <p>
188      * <code>http://developer.java.sun.com/
189      * developer/Books/effectivejava/Chapter3.pdf</code>
190      *
191      * @return A hash code.
192      */

193     public int hashCode() {
194         int result = 17;
195         result = 37 * result + this.start.hashCode();
196         result = 37 * result + this.end.hashCode();
197         return result;
198     }
199
200 }
201
Popular Tags