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