001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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-2013, 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 * 03-Jul-2013 : Use ParamChecks (DG); 047 * 048 */ 049 050package org.jfree.data.time; 051 052import java.io.Serializable; 053import org.jfree.chart.util.ParamChecks; 054import org.jfree.util.ObjectUtilities; 055 056/** 057 * Represents one data item in a time series. 058 * <P> 059 * The time period can be any of the following: 060 * <ul> 061 * <li>{@link Year}</li> 062 * <li>{@link Quarter}</li> 063 * <li>{@link Month}</li> 064 * <li>{@link Week}</li> 065 * <li>{@link Day}</li> 066 * <li>{@link Hour}</li> 067 * <li>{@link Minute}</li> 068 * <li>{@link Second}</li> 069 * <li>{@link Millisecond}</li> 070 * <li>{@link FixedMillisecond}</li> 071 * </ul> 072 * 073 * The time period is an immutable property of the data item. Data items will 074 * often be sorted within a list, and allowing the time period to be changed 075 * could destroy the sort order. 076 * <P> 077 * Implements the <code>Comparable</code> interface so that standard Java 078 * sorting can be used to keep the data items in order. 079 * 080 */ 081public class TimeSeriesDataItem implements Cloneable, Comparable, Serializable { 082 083 /** For serialization. */ 084 private static final long serialVersionUID = -2235346966016401302L; 085 086 /** The time period. */ 087 private RegularTimePeriod period; 088 089 /** The value associated with the time period. */ 090 private Number value; 091 092 /** 093 * Constructs a new data item that associates a value with a time period. 094 * 095 * @param period the time period (<code>null</code> not permitted). 096 * @param value the value (<code>null</code> permitted). 097 */ 098 public TimeSeriesDataItem(RegularTimePeriod period, Number value) { 099 ParamChecks.nullNotPermitted(period, "period"); 100 this.period = period; 101 this.value = value; 102 } 103 104 /** 105 * Constructs a new data item that associates a value with a time period. 106 * 107 * @param period the time period (<code>null</code> not permitted). 108 * @param value the value associated with the time period. 109 */ 110 public TimeSeriesDataItem(RegularTimePeriod period, double value) { 111 this(period, new Double(value)); 112 } 113 114 /** 115 * Returns the time period. 116 * 117 * @return The time period (never <code>null</code>). 118 */ 119 public RegularTimePeriod getPeriod() { 120 return this.period; 121 } 122 123 /** 124 * Returns the value. 125 * 126 * @return The value (<code>null</code> possible). 127 * 128 * @see #setValue(java.lang.Number) 129 */ 130 public Number getValue() { 131 return this.value; 132 } 133 134 /** 135 * Sets the value for this data item. 136 * 137 * @param value the value (<code>null</code> permitted). 138 * 139 * @see #getValue() 140 */ 141 public void setValue(Number value) { 142 this.value = value; 143 } 144 145 /** 146 * Tests this object for equality with an arbitrary object. 147 * 148 * @param obj the other object (<code>null</code> permitted). 149 * 150 * @return A boolean. 151 */ 152 @Override 153 public boolean equals(Object obj) { 154 if (this == obj) { 155 return true; 156 } 157 if (!(obj instanceof TimeSeriesDataItem)) { 158 return false; 159 } 160 TimeSeriesDataItem that = (TimeSeriesDataItem) obj; 161 if (!ObjectUtilities.equal(this.period, that.period)) { 162 return false; 163 } 164 if (!ObjectUtilities.equal(this.value, that.value)) { 165 return false; 166 } 167 return true; 168 } 169 170 /** 171 * Returns a hash code. 172 * 173 * @return A hash code. 174 */ 175 @Override 176 public int hashCode() { 177 int result; 178 result = (this.period != null ? this.period.hashCode() : 0); 179 result = 29 * result + (this.value != null ? this.value.hashCode() : 0); 180 return result; 181 } 182 183 /** 184 * Returns an integer indicating the order of this data pair object 185 * relative to another object. 186 * <P> 187 * For the order we consider only the timing: 188 * negative == before, zero == same, positive == after. 189 * 190 * @param o1 The object being compared to. 191 * 192 * @return An integer indicating the order of the data item object 193 * relative to another object. 194 */ 195 @Override 196 public int compareTo(Object o1) { 197 198 int result; 199 200 // CASE 1 : Comparing to another TimeSeriesDataItem object 201 // ------------------------------------------------------- 202 if (o1 instanceof TimeSeriesDataItem) { 203 TimeSeriesDataItem datapair = (TimeSeriesDataItem) o1; 204 result = getPeriod().compareTo(datapair.getPeriod()); 205 } 206 207 // CASE 2 : Comparing to a general object 208 // --------------------------------------------- 209 else { 210 // consider time periods to be ordered after general objects 211 result = 1; 212 } 213 214 return result; 215 216 } 217 218 /** 219 * Clones the data item. Note: there is no need to clone the period or 220 * value since they are immutable classes. 221 * 222 * @return A clone of the data item. 223 */ 224 @Override 225 public Object clone() { 226 Object clone = null; 227 try { 228 clone = super.clone(); 229 } 230 catch (CloneNotSupportedException e) { // won't get here... 231 e.printStackTrace(); 232 } 233 return clone; 234 } 235 236}