Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2008, 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-2008, 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: * 02-Jun-2008 : Fixed problem with fields being mutable (DG); 44: * 45: */ 46: 47: package org.jfree.data.time; 48: 49: import java.io.Serializable; 50: import java.util.Date; 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, Serializable { 60: 61: /** For serialization. */ 62: private static final long serialVersionUID = 8684672361131829554L; 63: 64: /** The start date/time. */ 65: private long start; 66: 67: /** The end date/time. */ 68: private long 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: if (start > end) { 78: throw new IllegalArgumentException("Requires start <= end."); 79: } 80: this.start = start; 81: this.end = end; 82: } 83: 84: /** 85: * Creates a new time allocation. 86: * 87: * @param start the start date/time (<code>null</code> not permitted). 88: * @param end the end date/time (<code>null</code> not permitted). 89: */ 90: public SimpleTimePeriod(Date start, Date end) { 91: this(start.getTime(), end.getTime()); 92: } 93: 94: /** 95: * Returns the start date/time. 96: * 97: * @return The start date/time (never <code>null</code>). 98: */ 99: public Date getStart() { 100: return new Date(this.start); 101: } 102: 103: /** 104: * Returns the start date/time in milliseconds. 105: * 106: * @return The start. 107: * 108: * @since 1.0.10. 109: */ 110: public long getStartMillis() { 111: return this.start; 112: } 113: 114: /** 115: * Returns the end date/time. 116: * 117: * @return The end date/time (never <code>null</code>). 118: */ 119: public Date getEnd() { 120: return new Date(this.end); 121: } 122: 123: /** 124: * Returns the end date/time in milliseconds. 125: * 126: * @return The end. 127: * 128: * @since 1.0.10. 129: */ 130: public long getEndMillis() { 131: return this.end; 132: } 133: 134: /** 135: * Tests this time period instance for equality with an arbitrary object. 136: * The object is considered equal if it is an instance of {@link TimePeriod} 137: * and it has the same start and end dates. 138: * 139: * @param obj the other object (<code>null</code> permitted). 140: * 141: * @return A boolean. 142: */ 143: public boolean equals(Object obj) { 144: if (obj == this) { 145: return true; 146: } 147: if (!(obj instanceof TimePeriod)) { 148: return false; 149: } 150: TimePeriod that = (TimePeriod) obj; 151: if (!this.getStart().equals(that.getStart())) { 152: return false; 153: } 154: if (!this.getEnd().equals(that.getEnd())) { 155: return false; 156: } 157: return true; 158: } 159: 160: /** 161: * Returns an integer that indicates the relative ordering of two 162: * time periods. 163: * 164: * @param obj the object (<code>null</code> not permitted). 165: * 166: * @return An integer. 167: * 168: * @throws ClassCastException if <code>obj</code> is not an instance of 169: * {@link TimePeriod}. 170: */ 171: public int compareTo(Object obj) { 172: TimePeriod that = (TimePeriod) obj; 173: long t0 = getStart().getTime(); 174: long t1 = getEnd().getTime(); 175: long m0 = t0 + (t1 - t0) / 2L; 176: long t2 = that.getStart().getTime(); 177: long t3 = that.getEnd().getTime(); 178: long m1 = t2 + (t3 - t2) / 2L; 179: if (m0 < m1) { 180: return -1; 181: } 182: else if (m0 > m1) { 183: return 1; 184: } 185: else { 186: if (t0 < t2) { 187: return -1; 188: } 189: else if (t0 > t2) { 190: return 1; 191: } 192: else { 193: if (t1 < t3) { 194: return -1; 195: } 196: else if (t1 > t3) { 197: return 1; 198: } 199: else { 200: return 0; 201: } 202: } 203: } 204: } 205: 206: /** 207: * Returns a hash code for this object instance. The approach described by 208: * Joshua Bloch in "Effective Java" has been used here - see: 209: * <p> 210: * <code>http://developer.java.sun.com/ 211: * developer/Books/effectivejava/Chapter3.pdf</code> 212: * 213: * @return A hash code. 214: */ 215: public int hashCode() { 216: int result = 17; 217: result = 37 * result + (int) this.start; 218: result = 37 * result + (int) this.end; 219: return result; 220: } 221: 222: }