Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, 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 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * --------------------- 28: * SimpleTimePeriod.java 29: * --------------------- 30: * (C) Copyright 2002-2007, by Object Refinery Limited and Contributors. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * Changes 36: * ------- 37: * 07-Oct-2002 : Added Javadocs (DG); 38: * 10-Jan-2003 : Renamed TimeAllocation --> SimpleTimePeriod (DG); 39: * 13-Mar-2003 : Added equals() method, and Serializable interface (DG); 40: * 21-Oct-2003 : Added hashCode() method (DG); 41: * 27-Jan-2005 : Implemented Comparable, to enable this class to be used 42: * in the TimeTableXYDataset class (DG); 43: * 44: */ 45: 46: package org.jfree.data.time; 47: 48: import java.io.Serializable; 49: import java.util.Date; 50: 51: /** 52: * An arbitrary period of time, measured to millisecond precision using 53: * <code>java.util.Date</code>. 54: * <p> 55: * This class is intentionally immutable (that is, once constructed, you cannot 56: * alter the start and end attributes). 57: */ 58: public class SimpleTimePeriod implements TimePeriod, Comparable, Serializable { 59: 60: /** For serialization. */ 61: private static final long serialVersionUID = 8684672361131829554L; 62: 63: /** The start date/time. */ 64: private Date start; 65: 66: /** The end date/time. */ 67: private Date end; 68: 69: /** 70: * Creates a new time allocation. 71: * 72: * @param start the start date/time in milliseconds. 73: * @param end the end date/time in milliseconds. 74: */ 75: public SimpleTimePeriod(long start, long end) { 76: this(new Date(start), new Date(end)); 77: } 78: 79: /** 80: * Creates a new time allocation. 81: * 82: * @param start the start date/time (<code>null</code> not permitted). 83: * @param end the end date/time (<code>null</code> not permitted). 84: */ 85: public SimpleTimePeriod(Date start, Date end) { 86: if (start.getTime() > end.getTime()) { 87: throw new IllegalArgumentException("Requires end >= start."); 88: } 89: this.start = start; 90: this.end = end; 91: } 92: 93: /** 94: * Returns the start date/time. 95: * 96: * @return The start date/time (never <code>null</code>). 97: */ 98: public Date getStart() { 99: return this.start; 100: } 101: 102: /** 103: * Returns the end date/time. 104: * 105: * @return The end date/time (never <code>null</code>). 106: */ 107: public Date getEnd() { 108: return this.end; 109: } 110: 111: /** 112: * Tests this time period instance for equality with an arbitrary object. 113: * The object is considered equal if it is an instance of {@link TimePeriod} 114: * and it has the same start and end dates. 115: * 116: * @param obj the other object (<code>null</code> permitted). 117: * 118: * @return A boolean. 119: */ 120: public boolean equals(Object obj) { 121: if (obj == this) { 122: return true; 123: } 124: if (!(obj instanceof TimePeriod)) { 125: return false; 126: } 127: TimePeriod that = (TimePeriod) obj; 128: if (!this.start.equals(that.getStart())) { 129: return false; 130: } 131: if (!this.end.equals(that.getEnd())) { 132: return false; 133: } 134: return true; 135: } 136: 137: /** 138: * Returns an integer that indicates the relative ordering of two 139: * time periods. 140: * 141: * @param obj the object (<code>null</code> not permitted). 142: * 143: * @return An integer. 144: * 145: * @throws ClassCastException if <code>obj</code> is not an instance of 146: * {@link TimePeriod}. 147: */ 148: public int compareTo(Object obj) { 149: TimePeriod that = (TimePeriod) obj; 150: long t0 = getStart().getTime(); 151: long t1 = getEnd().getTime(); 152: long m0 = t0 + (t1 - t0) / 2L; 153: long t2 = that.getStart().getTime(); 154: long t3 = that.getEnd().getTime(); 155: long m1 = t2 + (t3 - t2) / 2L; 156: if (m0 < m1) { 157: return -1; 158: } 159: else if (m0 > m1) { 160: return 1; 161: } 162: else { 163: if (t0 < t2) { 164: return -1; 165: } 166: else if (t0 > t2) { 167: return 1; 168: } 169: else { 170: if (t1 < t3) { 171: return -1; 172: } 173: else if (t1 > t3) { 174: return 1; 175: } 176: else { 177: return 0; 178: } 179: } 180: } 181: } 182: 183: /** 184: * Returns a hash code for this object instance. The approach described by 185: * Joshua Bloch in "Effective Java" has been used here - see: 186: * <p> 187: * <code>http://developer.java.sun.com/ 188: * developer/Books/effectivejava/Chapter3.pdf</code> 189: * 190: * @return A hash code. 191: */ 192: public int hashCode() { 193: int result = 17; 194: result = 37 * result + this.start.hashCode(); 195: result = 37 * result + this.end.hashCode(); 196: return result; 197: } 198: 199: }