Frames | No Frames |
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: * TimeSeriesDataItem.java 29: * ----------------------- 30: * (C) Copyright 2001-2005, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * $Id: TimeSeriesDataItem.java,v 1.5.2.1 2005/10/25 21:35:24 mungady Exp $ 36: * 37: * Changes 38: * ------- 39: * 11-Oct-2001 : Version 1 (DG); 40: * 15-Nov-2001 : Updated Javadoc comments (DG); 41: * 29-Nov-2001 : Added cloning (DG); 42: * 24-Jun-2002 : Removed unnecessary import (DG); 43: * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG); 44: * 13-Mar-2003 : Renamed TimeSeriesDataPair --> TimeSeriesDataItem, moved to 45: * com.jrefinery.data.time package, implemented Serializable (DG) 46: */ 47: 48: package org.jfree.data.time; 49: 50: import java.io.Serializable; 51: 52: /** 53: * Represents one data item in a time series. 54: * <P> 55: * The time period can be any of the following: 56: * <ul> 57: * <li>{@link Year}</li> 58: * <li>{@link Quarter}</li> 59: * <li>{@link Month}</li> 60: * <li>{@link Week}</li> 61: * <li>{@link Day}</li> 62: * <li>{@link Hour}</li> 63: * <li>{@link Minute}</li> 64: * <li>{@link Second}</li> 65: * <li>{@link Millisecond}</li> 66: * <li>{@link FixedMillisecond}</li> 67: * </ul> 68: * 69: * The time period is an immutable property of the data item. Data items will 70: * often be sorted within a list, and allowing the time period to be changed 71: * could destroy the sort order. 72: * <P> 73: * Implements the <code>Comparable</code> interface so that standard Java 74: * sorting can be used to keep the data items in order. 75: * 76: */ 77: public class TimeSeriesDataItem implements Cloneable, Comparable, Serializable { 78: 79: /** For serialization. */ 80: private static final long serialVersionUID = -2235346966016401302L; 81: 82: /** The time period. */ 83: private RegularTimePeriod period; 84: 85: /** The value associated with the time period. */ 86: private Number value; 87: 88: /** 89: * Constructs a new data item that associates a value with a time period. 90: * 91: * @param period the time period (<code>null</code> not permitted). 92: * @param value the value (<code>null</code> permitted). 93: */ 94: public TimeSeriesDataItem(RegularTimePeriod period, Number value) { 95: if (period == null) { 96: throw new IllegalArgumentException("Null 'period' argument."); 97: } 98: this.period = period; 99: this.value = value; 100: } 101: 102: /** 103: * Constructs a new data item that associates a value with a time period. 104: * 105: * @param period the time period (<code>null</code> not permitted). 106: * @param value the value associated with the time period. 107: */ 108: public TimeSeriesDataItem(RegularTimePeriod period, double value) { 109: this(period, new Double(value)); 110: } 111: 112: /** 113: * Returns the time period. 114: * 115: * @return The time period (never <code>null</code>). 116: */ 117: public RegularTimePeriod getPeriod() { 118: return this.period; 119: } 120: 121: /** 122: * Returns the value. 123: * 124: * @return The value (<code>null</code> possible). 125: */ 126: public Number getValue() { 127: return this.value; 128: } 129: 130: /** 131: * Sets the value for this data item. 132: * 133: * @param value the value (<code>null</code> permitted). 134: */ 135: public void setValue(Number value) { 136: this.value = value; 137: } 138: 139: /** 140: * Tests this object for equality with an arbitrary object. 141: * 142: * @param o the other object. 143: * 144: * @return A boolean. 145: */ 146: public boolean equals(Object o) { 147: if (this == o) { 148: return true; 149: } 150: if (!(o instanceof TimeSeriesDataItem)) { 151: return false; 152: } 153: TimeSeriesDataItem timeSeriesDataItem = (TimeSeriesDataItem) o; 154: if (this.period != null) { 155: if (!this.period.equals(timeSeriesDataItem.period)) { 156: return false; 157: } 158: } 159: else if (timeSeriesDataItem.period != null) { 160: return false; 161: } 162: 163: if (this.value != null) { 164: if (!this.value.equals(timeSeriesDataItem.value)) { 165: return false; 166: } 167: } 168: else if (timeSeriesDataItem.value != null) { 169: return false; 170: } 171: 172: return true; 173: } 174: 175: /** 176: * Returns a hash code. 177: * 178: * @return A hash code. 179: */ 180: public int hashCode() { 181: int result; 182: result = (this.period != null ? this.period.hashCode() : 0); 183: result = 29 * result + (this.value != null ? this.value.hashCode() : 0); 184: return result; 185: } 186: 187: /** 188: * Returns an integer indicating the order of this data pair object 189: * relative to another object. 190: * <P> 191: * For the order we consider only the timing: 192: * negative == before, zero == same, positive == after. 193: * 194: * @param o1 The object being compared to. 195: * 196: * @return An integer indicating the order of the data item object 197: * relative to another object. 198: */ 199: public int compareTo(Object o1) { 200: 201: int result; 202: 203: // CASE 1 : Comparing to another TimeSeriesDataItem object 204: // ------------------------------------------------------- 205: if (o1 instanceof TimeSeriesDataItem) { 206: TimeSeriesDataItem datapair = (TimeSeriesDataItem) o1; 207: result = getPeriod().compareTo(datapair.getPeriod()); 208: } 209: 210: // CASE 2 : Comparing to a general 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: * Clones the data item. Note: there is no need to clone the period or 223: * value since they are immutable classes. 224: * 225: * @return A clone of the data item. 226: */ 227: public Object clone() { 228: Object clone = null; 229: try { 230: clone = super.clone(); 231: } 232: catch (CloneNotSupportedException e) { // won't get here... 233: e.printStackTrace(); 234: } 235: return clone; 236: } 237: 238: }