001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, 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: * SimpleTimePeriod.java
029: * ---------------------
030: * (C) Copyright 2002-2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: SimpleTimePeriod.java,v 1.5.2.1 2005/10/25 21:35:24 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 07-Oct-2002 : Added Javadocs (DG);
040: * 10-Jan-2003 : Renamed TimeAllocation --> SimpleTimePeriod (DG);
041: * 13-Mar-2003 : Added equals() method, and Serializable interface (DG);
042: * 21-Oct-2003 : Added hashCode() method (DG);
043: * 27-Jan-2005 : Implemented Comparable, to enable this class to be used
044: * in the TimeTableXYDataset class (DG);
045: *
046: */
047:
048: package org.jfree.data.time;
049:
050: import java.io.Serializable;
051: import java.util.Date;
052:
053: /**
054: * An arbitrary period of time, measured to millisecond precision using
055: * <code>java.util.Date</code>.
056: * <p>
057: * This class is intentionally immutable (that is, once constructed, you cannot
058: * alter the start and end attributes).
059: */
060: public class SimpleTimePeriod implements TimePeriod, Comparable,
061: Serializable {
062:
063: /** For serialization. */
064: private static final long serialVersionUID = 8684672361131829554L;
065:
066: /** The start date/time. */
067: private Date start;
068:
069: /** The end date/time. */
070: private Date end;
071:
072: /**
073: * Creates a new time allocation.
074: *
075: * @param start the start date/time in milliseconds.
076: * @param end the end date/time in milliseconds.
077: */
078: public SimpleTimePeriod(long start, long end) {
079: this (new Date(start), new Date(end));
080: }
081:
082: /**
083: * Creates a new time allocation.
084: *
085: * @param start the start date/time (<code>null</code> not permitted).
086: * @param end the end date/time (<code>null</code> not permitted).
087: */
088: public SimpleTimePeriod(Date start, Date end) {
089: if (start.getTime() > end.getTime()) {
090: throw new IllegalArgumentException("Requires end >= start.");
091: }
092: this .start = start;
093: this .end = end;
094: }
095:
096: /**
097: * Returns the start date/time.
098: *
099: * @return The start date/time (never <code>null</code>).
100: */
101: public Date getStart() {
102: return this .start;
103: }
104:
105: /**
106: * Returns the end date/time.
107: *
108: * @return The end date/time (never <code>null</code>).
109: */
110: public Date getEnd() {
111: return this .end;
112: }
113:
114: /**
115: * Tests this time period instance for equality with an arbitrary object.
116: * The object is considered equal if it is an instance of {@link TimePeriod}
117: * and it has the same start and end dates.
118: *
119: * @param obj the other object (<code>null</code> permitted).
120: *
121: * @return A boolean.
122: */
123: public boolean equals(Object obj) {
124: if (obj == this ) {
125: return true;
126: }
127: if (!(obj instanceof TimePeriod)) {
128: return false;
129: }
130: TimePeriod that = (TimePeriod) obj;
131: if (!this .start.equals(that.getStart())) {
132: return false;
133: }
134: if (!this .end.equals(that.getEnd())) {
135: return false;
136: }
137: return true;
138: }
139:
140: /**
141: * Returns an integer that indicates the relative ordering of two
142: * time periods.
143: *
144: * @param obj the object (<code>null</code> not permitted).
145: *
146: * @return An integer.
147: *
148: * @throws ClassCastException if <code>obj</code> is not an instance of
149: * {@link TimePeriod}.
150: */
151: public int compareTo(Object obj) {
152: TimePeriod that = (TimePeriod) obj;
153: long t0 = getStart().getTime();
154: long t1 = getEnd().getTime();
155: long m0 = t0 + (t1 - t0) / 2L;
156: long t2 = that.getStart().getTime();
157: long t3 = that.getEnd().getTime();
158: long m1 = t2 + (t3 - t2) / 2L;
159: if (m0 < m1) {
160: return -1;
161: } else if (m0 > m1) {
162: return 1;
163: } else {
164: if (t0 < t2) {
165: return -1;
166: } else if (t0 > t2) {
167: return 1;
168: } else {
169: if (t1 < t3) {
170: return -1;
171: } else if (t1 > t3) {
172: return 1;
173: } else {
174: return 0;
175: }
176: }
177: }
178: }
179:
180: /**
181: * Returns a hash code for this object instance. The approach described by
182: * Joshua Bloch in "Effective Java" has been used here - see:
183: * <p>
184: * <code>http://developer.java.sun.com/
185: * developer/Books/effectivejava/Chapter3.pdf</code>
186: *
187: * @return A hash code.
188: */
189: public int hashCode() {
190: int result = 17;
191: result = 37 * result + this .start.hashCode();
192: result = 37 * result + this.end.hashCode();
193: return result;
194: }
195:
196: }
|