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-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:  * FixedMillisecond.java
  29:  * ---------------------
  30:  * (C) Copyright 2002-2007 by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 19-Mar-2002 : Version 1, based on original Millisecond implementation (DG);
  38:  * 24-Jun-2002 : Removed unnecessary imports (DG);
  39:  * 10-Sep-2002 : Added getSerialIndex() method (DG);
  40:  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41:  * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented 
  42:  *               Serializable (DG);
  43:  * 21-Oct-2003 : Added hashCode() method (DG);
  44:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  45:  * 06-Oct-2006 : Added peg() 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:      * This method is overridden to do nothing.
 105:      * 
 106:      * @param calendar  ignored
 107:      * 
 108:      * @since 1.0.3
 109:      */
 110:     public void peg(Calendar calendar) {
 111:         // nothing to do        
 112:     }
 113: 
 114:     /**
 115:      * Returns the millisecond preceding this one.
 116:      *
 117:      * @return The millisecond preceding this one.
 118:      */
 119:     public RegularTimePeriod previous() {
 120:         RegularTimePeriod result = null;
 121:         long t = this.time.getTime();
 122:         if (t != Long.MIN_VALUE) {
 123:             result = new FixedMillisecond(t - 1);
 124:         }
 125:         return result;
 126:     }
 127: 
 128:     /**
 129:      * Returns the millisecond following this one.
 130:      *
 131:      * @return The millisecond following this one.
 132:      */
 133:     public RegularTimePeriod next() {
 134:         RegularTimePeriod result = null;
 135:         long t = this.time.getTime();
 136:         if (t != Long.MAX_VALUE) {
 137:             result = new FixedMillisecond(t + 1);
 138:         }
 139:         return result;
 140:     }
 141: 
 142:     /**
 143:      * Tests the equality of this object against an arbitrary Object.
 144:      *
 145:      * @param object  the object to compare
 146:      *
 147:      * @return A boolean.
 148:      */
 149:     public boolean equals(Object object) {
 150:         if (object instanceof FixedMillisecond) {
 151:             FixedMillisecond m = (FixedMillisecond) object;
 152:             return this.time.equals(m.getTime());
 153:         }
 154:         else {
 155:             return false;
 156:         }
 157: 
 158:     }
 159: 
 160:     /**
 161:      * Returns a hash code for this object instance.
 162:      * 
 163:      * @return A hash code.
 164:      */
 165:     public int hashCode() {
 166:         return this.time.hashCode();
 167:     }
 168: 
 169:     /**
 170:      * Returns an integer indicating the order of this Millisecond object
 171:      * relative to the specified
 172:      * object: negative == before, zero == same, positive == after.
 173:      *
 174:      * @param o1    the object to compare.
 175:      *
 176:      * @return negative == before, zero == same, positive == after.
 177:      */
 178:     public int compareTo(Object o1) {
 179: 
 180:         int result;
 181:         long difference;
 182: 
 183:         // CASE 1 : Comparing to another Second object
 184:         // -------------------------------------------
 185:         if (o1 instanceof FixedMillisecond) {
 186:             FixedMillisecond t1 = (FixedMillisecond) o1;
 187:             difference = this.time.getTime() - t1.time.getTime();
 188:             if (difference > 0) {
 189:                 result = 1;
 190:             }
 191:             else {
 192:                 if (difference < 0) {
 193:                    result = -1;
 194:                 }
 195:                 else {
 196:                     result = 0;
 197:                 }
 198:             }
 199:         }
 200: 
 201:         // CASE 2 : Comparing to another TimePeriod object
 202:         // -----------------------------------------------
 203:         else if (o1 instanceof RegularTimePeriod) {
 204:             // more difficult case - evaluate later...
 205:             result = 0;
 206:         }
 207: 
 208:         // CASE 3 : Comparing to a non-TimePeriod object
 209:         // ---------------------------------------------
 210:         else {
 211:             // consider time periods to be ordered after general objects
 212:             result = 1;
 213:         }
 214: 
 215:         return result;
 216: 
 217:     }
 218: 
 219:     /**
 220:      * Returns the first millisecond of the time period.
 221:      *
 222:      * @return The first millisecond of the time period.
 223:      */
 224:     public long getFirstMillisecond() {
 225:         return this.time.getTime();
 226:     }
 227: 
 228: 
 229:     /**
 230:      * Returns the first millisecond of the time period.
 231:      *
 232:      * @param calendar  the calendar.
 233:      *
 234:      * @return The first millisecond of the time period.
 235:      */
 236:     public long getFirstMillisecond(Calendar calendar) {
 237:         return this.time.getTime();
 238:     }
 239: 
 240:     /**
 241:      * Returns the last millisecond of the time period.
 242:      *
 243:      * @return The last millisecond of the time period.
 244:      */
 245:     public long getLastMillisecond() {
 246:         return this.time.getTime();
 247:     }
 248: 
 249:     /**
 250:      * Returns the last millisecond of the time period.
 251:      *
 252:      * @param calendar  the calendar.
 253:      *
 254:      * @return The last millisecond of the time period.
 255:      */
 256:     public long getLastMillisecond(Calendar calendar) {
 257:         return this.time.getTime();
 258:     }
 259: 
 260:     /**
 261:      * Returns the millisecond closest to the middle of the time period.
 262:      *
 263:      * @return The millisecond closest to the middle of the time period.
 264:      */
 265:     public long getMiddleMillisecond() {
 266:         return this.time.getTime();
 267:     }
 268: 
 269:     /**
 270:      * Returns the millisecond closest to the middle of the time period.
 271:      *
 272:      * @param calendar  the calendar.
 273:      *
 274:      * @return The millisecond closest to the middle of the time period.
 275:      */
 276:     public long getMiddleMillisecond(Calendar calendar) {
 277:         return this.time.getTime();
 278:     }
 279: 
 280:     /**
 281:      * Returns a serial index number for the millisecond.
 282:      *
 283:      * @return The serial index number.
 284:      */
 285:     public long getSerialIndex() {
 286:         return this.time.getTime();
 287:     }
 288: 
 289: }