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