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