Source for 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
  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-2005, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: SimpleTimePeriod.java,v 1.5.2.1 2005/10/25 21:35:24 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 07-Oct-2002 : Added Javadocs (DG);
  40:  * 10-Jan-2003 : Renamed TimeAllocation --> SimpleTimePeriod (DG);
  41:  * 13-Mar-2003 : Added equals() method, and Serializable interface (DG);
  42:  * 21-Oct-2003 : Added hashCode() method (DG);
  43:  * 27-Jan-2005 : Implemented Comparable, to enable this class to be used
  44:  *               in the TimeTableXYDataset class (DG);
  45:  *
  46:  */
  47: 
  48: package org.jfree.data.time;
  49: 
  50: import java.io.Serializable;
  51: import java.util.Date;
  52: 
  53: /**
  54:  * An arbitrary period of time, measured to millisecond precision using 
  55:  * <code>java.util.Date</code>.
  56:  * <p>
  57:  * This class is intentionally immutable (that is, once constructed, you cannot 
  58:  * alter the start and end attributes).
  59:  */
  60: public class SimpleTimePeriod implements TimePeriod, Comparable, Serializable {
  61: 
  62:     /** For serialization. */
  63:     private static final long serialVersionUID = 8684672361131829554L;
  64:     
  65:     /** The start date/time. */
  66:     private Date start;
  67: 
  68:     /** The end date/time. */
  69:     private Date end;
  70: 
  71:     /**
  72:      * Creates a new time allocation.
  73:      *
  74:      * @param start  the start date/time in milliseconds.
  75:      * @param end  the end date/time in milliseconds.
  76:      */
  77:     public SimpleTimePeriod(long start, long end) {
  78:         this(new Date(start), new Date(end));   
  79:     }
  80:     
  81:     /**
  82:      * Creates a new time allocation.
  83:      *
  84:      * @param start  the start date/time (<code>null</code> not permitted).
  85:      * @param end  the end date/time (<code>null</code> not permitted).
  86:      */
  87:     public SimpleTimePeriod(Date start, Date end) {
  88:         if (start.getTime() > end.getTime()) {
  89:             throw new IllegalArgumentException("Requires end >= start.");
  90:         }
  91:         this.start = start;
  92:         this.end = end;
  93:     }
  94: 
  95:     /**
  96:      * Returns the start date/time.
  97:      *
  98:      * @return The start date/time (never <code>null</code>).
  99:      */
 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: }