Source for org.jfree.data.time.FixedMillisecond

   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:  * FixedMillisecond.java
  29:  * ---------------------
  30:  * (C) Copyright 2002-2005 by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: FixedMillisecond.java,v 1.4.2.1 2005/10/25 21:35:24 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 19-Mar-2002 : Version 1, based on original Millisecond implementation (DG);
  40:  * 24-Jun-2002 : Removed unnecessary imports (DG);
  41:  * 10-Sep-2002 : Added getSerialIndex() method (DG);
  42:  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  43:  * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented 
  44:  *               Serializable (DG);
  45:  * 21-Oct-2003 : Added hashCode() method (DG);
  46:  *
  47:  */
  48: 
  49: package org.jfree.data.time;
  50: 
  51: import java.io.Serializable;
  52: import java.util.Calendar;
  53: import java.util.Date;
  54: 
  55: /**
  56:  * Wrapper for a <code>java.util.Date</code> object that allows it to be used 
  57:  * as a {@link RegularTimePeriod}.  This class is immutable, which is a 
  58:  * requirement for all {@link RegularTimePeriod} subclasses.
  59:  */
  60: public class FixedMillisecond extends RegularTimePeriod 
  61:                               implements Serializable {
  62: 
  63:     /** For serialization. */
  64:     private static final long serialVersionUID = 7867521484545646931L;
  65:     
  66:     /** The millisecond. */
  67:     private Date time;
  68: 
  69:     /**
  70:      * Constructs a millisecond based on the current system time.
  71:      */
  72:     public FixedMillisecond() {
  73:         this(new Date());
  74:     }
  75: 
  76:     /**
  77:      * Constructs a millisecond.
  78:      *
  79:      * @param millisecond  the millisecond (same encoding as java.util.Date).
  80:      */
  81:     public FixedMillisecond(long millisecond) {
  82:         this(new Date(millisecond));
  83:     }
  84: 
  85:     /**
  86:      * Constructs a millisecond.
  87:      *
  88:      * @param time  the time.
  89:      */
  90:     public FixedMillisecond(Date time) {
  91:         this.time = time;
  92:     }
  93: 
  94:     /**
  95:      * Returns the date/time.
  96:      *
  97:      * @return The date/time.
  98:      */
  99:     public Date getTime() {
 100:         return this.time;
 101:     }
 102: 
 103:     /**
 104:      * Returns the millisecond preceding this one.
 105:      *
 106:      * @return The millisecond preceding this one.
 107:      */
 108:     public RegularTimePeriod previous() {
 109:         RegularTimePeriod result = null;
 110:         long t = this.time.getTime();
 111:         if (t != Long.MIN_VALUE) {
 112:             result = new FixedMillisecond(t - 1);
 113:         }
 114:         return result;
 115:     }
 116: 
 117:     /**
 118:      * Returns the millisecond following this one.
 119:      *
 120:      * @return The millisecond following this one.
 121:      */
 122:     public RegularTimePeriod next() {
 123:         RegularTimePeriod result = null;
 124:         long t = this.time.getTime();
 125:         if (t != Long.MAX_VALUE) {
 126:             result = new FixedMillisecond(t + 1);
 127:         }
 128:         return result;
 129:     }
 130: 
 131:     /**
 132:      * Tests the equality of this object against an arbitrary Object.
 133:      *
 134:      * @param object  the object to compare
 135:      *
 136:      * @return A boolean.
 137:      */
 138:     public boolean equals(Object object) {
 139:         if (object instanceof FixedMillisecond) {
 140:             FixedMillisecond m = (FixedMillisecond) object;
 141:             return this.time.equals(m.getTime());
 142:         }
 143:         else {
 144:             return false;
 145:         }
 146: 
 147:     }
 148: 
 149:     /**
 150:      * Returns a hash code for this object instance.
 151:      * 
 152:      * @return A hash code.
 153:      */
 154:     public int hashCode() {
 155:         return this.time.hashCode();
 156:     }
 157: 
 158:     /**
 159:      * Returns an integer indicating the order of this Millisecond object
 160:      * relative to the specified
 161:      * object: negative == before, zero == same, positive == after.
 162:      *
 163:      * @param o1    the object to compare.
 164:      *
 165:      * @return negative == before, zero == same, positive == after.
 166:      */
 167:     public int compareTo(Object o1) {
 168: 
 169:         int result;
 170:         long difference;
 171: 
 172:         // CASE 1 : Comparing to another Second object
 173:         // -------------------------------------------
 174:         if (o1 instanceof FixedMillisecond) {
 175:             FixedMillisecond t1 = (FixedMillisecond) o1;
 176:             difference = this.time.getTime() - t1.time.getTime();
 177:             if (difference > 0) {
 178:                 result = 1;
 179:             }
 180:             else {
 181:                 if (difference < 0) {
 182:                    result = -1;
 183:                 }
 184:                 else {
 185:                     result = 0;
 186:                 }
 187:             }
 188:         }
 189: 
 190:         // CASE 2 : Comparing to another TimePeriod object
 191:         // -----------------------------------------------
 192:         else if (o1 instanceof RegularTimePeriod) {
 193:             // more difficult case - evaluate later...
 194:             result = 0;
 195:         }
 196: 
 197:         // CASE 3 : Comparing to a non-TimePeriod object
 198:         // ---------------------------------------------
 199:         else {
 200:             // consider time periods to be ordered after general objects
 201:             result = 1;
 202:         }
 203: 
 204:         return result;
 205: 
 206:     }
 207: 
 208:     /**
 209:      * Returns the first millisecond of the time period.
 210:      *
 211:      * @return The first millisecond of the time period.
 212:      */
 213:     public long getFirstMillisecond() {
 214:         return this.time.getTime();
 215:     }
 216: 
 217: 
 218:     /**
 219:      * Returns the first millisecond of the time period.
 220:      *
 221:      * @param calendar  the calendar.
 222:      *
 223:      * @return The first millisecond of the time period.
 224:      */
 225:     public long getFirstMillisecond(Calendar calendar) {
 226:         return this.time.getTime();
 227:     }
 228: 
 229:     /**
 230:      * Returns the last millisecond of the time period.
 231:      *
 232:      * @return The last millisecond of the time period.
 233:      */
 234:     public long getLastMillisecond() {
 235:         return this.time.getTime();
 236:     }
 237: 
 238:     /**
 239:      * Returns the last millisecond of the time period.
 240:      *
 241:      * @param calendar  the calendar.
 242:      *
 243:      * @return The last millisecond of the time period.
 244:      */
 245:     public long getLastMillisecond(Calendar calendar) {
 246:         return this.time.getTime();
 247:     }
 248: 
 249:     /**
 250:      * Returns the millisecond closest to the middle of the time period.
 251:      *
 252:      * @return The millisecond closest to the middle of the time period.
 253:      */
 254:     public long getMiddleMillisecond() {
 255:         return this.time.getTime();
 256:     }
 257: 
 258:     /**
 259:      * Returns the millisecond closest to the middle of the time period.
 260:      *
 261:      * @param calendar  the calendar.
 262:      *
 263:      * @return The millisecond closest to the middle of the time period.
 264:      */
 265:     public long getMiddleMillisecond(Calendar calendar) {
 266:         return this.time.getTime();
 267:     }
 268: 
 269:     /**
 270:      * Returns a serial index number for the millisecond.
 271:      *
 272:      * @return The serial index number.
 273:      */
 274:     public long getSerialIndex() {
 275:         return this.time.getTime();
 276:     }
 277: 
 278: }