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