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, Serializable {
061
062 /** For serialization. */
063 private static final long serialVersionUID = 8684672361131829554L;
064
065 /** The start date/time. */
066 private Date start;
067
068 /** The end date/time. */
069 private Date end;
070
071 /**
072 * Creates a new time allocation.
073 *
074 * @param start the start date/time in milliseconds.
075 * @param end the end date/time in milliseconds.
076 */
077 public SimpleTimePeriod(long start, long end) {
078 this(new Date(start), new Date(end));
079 }
080
081 /**
082 * Creates a new time allocation.
083 *
084 * @param start the start date/time (<code>null</code> not permitted).
085 * @param end the end date/time (<code>null</code> not permitted).
086 */
087 public SimpleTimePeriod(Date start, Date end) {
088 if (start.getTime() > end.getTime()) {
089 throw new IllegalArgumentException("Requires end >= start.");
090 }
091 this.start = start;
092 this.end = end;
093 }
094
095 /**
096 * Returns the start date/time.
097 *
098 * @return The start date/time (never <code>null</code>).
099 */
100 public Date getStart() {
101 return this.start;
102 }
103
104 /**
105 * Returns the end date/time.
106 *
107 * @return The end date/time (never <code>null</code>).
108 */
109 public Date getEnd() {
110 return this.end;
111 }
112
113 /**
114 * Tests this time period instance for equality with an arbitrary object.
115 * The object is considered equal if it is an instance of {@link TimePeriod}
116 * and it has the same start and end dates.
117 *
118 * @param obj the other object (<code>null</code> permitted).
119 *
120 * @return A boolean.
121 */
122 public boolean equals(Object obj) {
123 if (obj == this) {
124 return true;
125 }
126 if (!(obj instanceof TimePeriod)) {
127 return false;
128 }
129 TimePeriod that = (TimePeriod) obj;
130 if (!this.start.equals(that.getStart())) {
131 return false;
132 }
133 if (!this.end.equals(that.getEnd())) {
134 return false;
135 }
136 return true;
137 }
138
139 /**
140 * Returns an integer that indicates the relative ordering of two
141 * time periods.
142 *
143 * @param obj the object (<code>null</code> not permitted).
144 *
145 * @return An integer.
146 *
147 * @throws ClassCastException if <code>obj</code> is not an instance of
148 * {@link TimePeriod}.
149 */
150 public int compareTo(Object obj) {
151 TimePeriod that = (TimePeriod) obj;
152 long t0 = getStart().getTime();
153 long t1 = getEnd().getTime();
154 long m0 = t0 + (t1 - t0) / 2L;
155 long t2 = that.getStart().getTime();
156 long t3 = that.getEnd().getTime();
157 long m1 = t2 + (t3 - t2) / 2L;
158 if (m0 < m1) {
159 return -1;
160 }
161 else if (m0 > m1) {
162 return 1;
163 }
164 else {
165 if (t0 < t2) {
166 return -1;
167 }
168 else if (t0 > t2) {
169 return 1;
170 }
171 else {
172 if (t1 < t3) {
173 return -1;
174 }
175 else if (t1 > t3) {
176 return 1;
177 }
178 else {
179 return 0;
180 }
181 }
182 }
183 }
184
185 /**
186 * Returns a hash code for this object instance. The approach described by
187 * Joshua Bloch in "Effective Java" has been used here - see:
188 * <p>
189 * <code>http://developer.java.sun.com/
190 * developer/Books/effectivejava/Chapter3.pdf</code>
191 *
192 * @return A hash code.
193 */
194 public int hashCode() {
195 int result = 17;
196 result = 37 * result + this.start.hashCode();
197 result = 37 * result + this.end.hashCode();
198 return result;
199 }
200
201 }