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