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