001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * TimePeriodValue.java
029 * --------------------
030 * (C) Copyright 2003-2016, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 22-Apr-2003 : Version 1 (DG);
038 * 03-Oct-2006 : Added null argument check to constructor (DG);
039 * 07-Apr-2008 : Added a toString() override for debugging (DG);
040 * 02-Jul-2013 : Use ParamChecks (DG);
041 *
042 */
043
044package org.jfree.data.time;
045
046import java.io.Serializable;
047import org.jfree.chart.util.Args;
048
049/**
050 * Represents a time period and an associated value.
051 */
052public class TimePeriodValue implements Cloneable, Serializable {
053
054    /** For serialization. */
055    private static final long serialVersionUID = 3390443360845711275L;
056
057    /** The time period. */
058    private TimePeriod period;
059
060    /** The value associated with the time period. */
061    private Number value;
062
063    /**
064     * Constructs a new data item.
065     *
066     * @param period  the time period ({@code null} not permitted).
067     * @param value  the value associated with the time period.
068     *
069     * @throws IllegalArgumentException if {@code period} is {@code null}.
070     */
071    public TimePeriodValue(TimePeriod period, Number value) {
072        Args.nullNotPermitted(period, "period");
073        this.period = period;
074        this.value = value;
075    }
076
077    /**
078     * Constructs a new data item.
079     *
080     * @param period  the time period ({@code null} not permitted).
081     * @param value  the value associated with the time period.
082     *
083     * @throws IllegalArgumentException if {@code period} is {@code null}.
084     */
085    public TimePeriodValue(TimePeriod period, double value) {
086        this(period, Double.valueOf(value));
087    }
088
089    /**
090     * Returns the time period.
091     *
092     * @return The time period (never {@code null}).
093     */
094    public TimePeriod getPeriod() {
095        return this.period;
096    }
097
098    /**
099     * Returns the value.
100     *
101     * @return The value (possibly {@code null}).
102     *
103     * @see #setValue(Number)
104     */
105    public Number getValue() {
106        return this.value;
107    }
108
109    /**
110     * Sets the value for this data item.
111     *
112     * @param value  the new value ({@code null} permitted).
113     *
114     * @see #getValue()
115     */
116    public void setValue(Number value) {
117        this.value = value;
118    }
119
120    /**
121     * Tests this object for equality with the target object.
122     *
123     * @param obj  the object ({@code null} permitted).
124     *
125     * @return A boolean.
126     */
127    @Override
128    public boolean equals(Object obj) {
129        if (this == obj) {
130            return true;
131        }
132        if (!(obj instanceof TimePeriodValue)) {
133            return false;
134        }
135
136        TimePeriodValue timePeriodValue = (TimePeriodValue) obj;
137
138        if (this.period != null ? !this.period.equals(timePeriodValue.period)
139                : timePeriodValue.period != null) {
140            return false;
141        }
142        if (this.value != null ? !this.value.equals(timePeriodValue.value)
143                : timePeriodValue.value != null) {
144            return false;
145        }
146
147        return true;
148    }
149
150    /**
151     * Returns a hash code value for the object.
152     *
153     * @return The hashcode
154     */
155    @Override
156    public int hashCode() {
157        int result;
158        result = (this.period != null ? this.period.hashCode() : 0);
159        result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
160        return result;
161    }
162
163    /**
164     * Clones the object.
165     * <P>
166     * Note: no need to clone the period or value since they are immutable
167     * classes.
168     *
169     * @return A clone.
170     */
171    @Override
172    public Object clone() {
173        Object clone = null;
174        try {
175            clone = super.clone();
176        }
177        catch (CloneNotSupportedException e) { // won't get here...
178            throw new RuntimeException(e);
179        }
180        return clone;
181    }
182
183    /**
184     * Returns a string representing this instance, primarily for use in
185     * debugging.
186     *
187     * @return A string.
188     */
189    @Override
190    public String toString() {
191        return "TimePeriodValue[" + getPeriod() + "," + getValue() + "]";
192    }
193
194}