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