001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2011, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ----------------------- 028 * TimeSeriesDataItem.java 029 * ----------------------- 030 * (C) Copyright 2001-2009, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 11-Oct-2001 : Version 1 (DG); 038 * 15-Nov-2001 : Updated Javadoc comments (DG); 039 * 29-Nov-2001 : Added cloning (DG); 040 * 24-Jun-2002 : Removed unnecessary import (DG); 041 * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG); 042 * 13-Mar-2003 : Renamed TimeSeriesDataPair --> TimeSeriesDataItem, moved to 043 * com.jrefinery.data.time package, implemented Serializable (DG) 044 * ------------- JFREECHART 1.0.x --------------------------------------------- 045 * 09-Jun-2009 : Tidied up equals() (DG); 046 */ 047 048package org.jfree.data.time; 049 050import java.io.Serializable; 051import org.jfree.util.ObjectUtilities; 052 053/** 054 * Represents one data item in a time series. 055 * <P> 056 * The time period can be any of the following: 057 * <ul> 058 * <li>{@link Year}</li> 059 * <li>{@link Quarter}</li> 060 * <li>{@link Month}</li> 061 * <li>{@link Week}</li> 062 * <li>{@link Day}</li> 063 * <li>{@link Hour}</li> 064 * <li>{@link Minute}</li> 065 * <li>{@link Second}</li> 066 * <li>{@link Millisecond}</li> 067 * <li>{@link FixedMillisecond}</li> 068 * </ul> 069 * 070 * The time period is an immutable property of the data item. Data items will 071 * often be sorted within a list, and allowing the time period to be changed 072 * could destroy the sort order. 073 * <P> 074 * Implements the <code>Comparable</code> interface so that standard Java 075 * sorting can be used to keep the data items in order. 076 * 077 */ 078public class TimeSeriesDataItem implements Cloneable, Comparable, Serializable { 079 080 /** For serialization. */ 081 private static final long serialVersionUID = -2235346966016401302L; 082 083 /** The time period. */ 084 private RegularTimePeriod period; 085 086 /** The value associated with the time period. */ 087 private Number value; 088 089 /** 090 * Constructs a new data item that associates a value with a time period. 091 * 092 * @param period the time period (<code>null</code> not permitted). 093 * @param value the value (<code>null</code> permitted). 094 */ 095 public TimeSeriesDataItem(RegularTimePeriod period, Number value) { 096 if (period == null) { 097 throw new IllegalArgumentException("Null 'period' argument."); 098 } 099 this.period = period; 100 this.value = value; 101 } 102 103 /** 104 * Constructs a new data item that associates a value with a time period. 105 * 106 * @param period the time period (<code>null</code> not permitted). 107 * @param value the value associated with the time period. 108 */ 109 public TimeSeriesDataItem(RegularTimePeriod period, double value) { 110 this(period, new Double(value)); 111 } 112 113 /** 114 * Returns the time period. 115 * 116 * @return The time period (never <code>null</code>). 117 */ 118 public RegularTimePeriod getPeriod() { 119 return this.period; 120 } 121 122 /** 123 * Returns the value. 124 * 125 * @return The value (<code>null</code> possible). 126 * 127 * @see #setValue(java.lang.Number) 128 */ 129 public Number getValue() { 130 return this.value; 131 } 132 133 /** 134 * Sets the value for this data item. 135 * 136 * @param value the value (<code>null</code> permitted). 137 * 138 * @see #getValue() 139 */ 140 public void setValue(Number value) { 141 this.value = value; 142 } 143 144 /** 145 * Tests this object for equality with an arbitrary object. 146 * 147 * @param obj the other object (<code>null</code> permitted). 148 * 149 * @return A boolean. 150 */ 151 public boolean equals(Object obj) { 152 if (this == obj) { 153 return true; 154 } 155 if (!(obj instanceof TimeSeriesDataItem)) { 156 return false; 157 } 158 TimeSeriesDataItem that = (TimeSeriesDataItem) obj; 159 if (!ObjectUtilities.equal(this.period, that.period)) { 160 return false; 161 } 162 if (!ObjectUtilities.equal(this.value, that.value)) { 163 return false; 164 } 165 return true; 166 } 167 168 /** 169 * Returns a hash code. 170 * 171 * @return A hash code. 172 */ 173 public int hashCode() { 174 int result; 175 result = (this.period != null ? this.period.hashCode() : 0); 176 result = 29 * result + (this.value != null ? this.value.hashCode() : 0); 177 return result; 178 } 179 180 /** 181 * Returns an integer indicating the order of this data pair object 182 * relative to another object. 183 * <P> 184 * For the order we consider only the timing: 185 * negative == before, zero == same, positive == after. 186 * 187 * @param o1 The object being compared to. 188 * 189 * @return An integer indicating the order of the data item object 190 * relative to another object. 191 */ 192 public int compareTo(Object o1) { 193 194 int result; 195 196 // CASE 1 : Comparing to another TimeSeriesDataItem object 197 // ------------------------------------------------------- 198 if (o1 instanceof TimeSeriesDataItem) { 199 TimeSeriesDataItem datapair = (TimeSeriesDataItem) o1; 200 result = getPeriod().compareTo(datapair.getPeriod()); 201 } 202 203 // CASE 2 : Comparing to a general object 204 // --------------------------------------------- 205 else { 206 // consider time periods to be ordered after general objects 207 result = 1; 208 } 209 210 return result; 211 212 } 213 214 /** 215 * Clones the data item. Note: there is no need to clone the period or 216 * value since they are immutable classes. 217 * 218 * @return A clone of the data item. 219 */ 220 public Object clone() { 221 Object clone = null; 222 try { 223 clone = super.clone(); 224 } 225 catch (CloneNotSupportedException e) { // won't get here... 226 e.printStackTrace(); 227 } 228 return clone; 229 } 230 231}