001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * --------------------
028 * TimePeriodValue.java
029 * --------------------
030 * (C) Copyright 2003-2008, 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 *
041 */
042
043package org.jfree.data.time;
044
045import java.io.Serializable;
046
047/**
048 * Represents a time period and an associated value.
049 */
050public class TimePeriodValue implements Cloneable, Serializable {
051
052    /** For serialization. */
053    private static final long serialVersionUID = 3390443360845711275L;
054
055    /** The time period. */
056    private TimePeriod period;
057
058    /** The value associated with the time period. */
059    private Number value;
060
061    /**
062     * Constructs a new data item.
063     *
064     * @param period  the time period (<code>null</code> not permitted).
065     * @param value  the value associated with the time period.
066     *
067     * @throws IllegalArgumentException if <code>period</code> is
068     *     <code>null</code>.
069     */
070    public TimePeriodValue(TimePeriod period, Number value) {
071        if (period == null) {
072            throw new IllegalArgumentException("Null 'period' argument.");
073        }
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    public boolean equals(Object obj) {
130        if (this == obj) {
131            return true;
132        }
133        if (!(obj instanceof TimePeriodValue)) {
134            return false;
135        }
136
137        TimePeriodValue timePeriodValue = (TimePeriodValue) obj;
138
139        if (this.period != null ? !this.period.equals(timePeriodValue.period)
140                : timePeriodValue.period != null) {
141            return false;
142        }
143        if (this.value != null ? !this.value.equals(timePeriodValue.value)
144                : timePeriodValue.value != null) {
145            return false;
146        }
147
148        return true;
149    }
150
151    /**
152     * Returns a hash code value for the object.
153     *
154     * @return The hashcode
155     */
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    public Object clone() {
172        Object clone = null;
173        try {
174            clone = super.clone();
175        }
176        catch (CloneNotSupportedException e) { // won't get here...
177            e.printStackTrace();
178        }
179        return clone;
180    }
181
182    /**
183     * Returns a string representing this instance, primarily for use in
184     * debugging.
185     *
186     * @return A string.
187     */
188    public String toString() {
189        return "TimePeriodValue[" + getPeriod() + "," + getValue() + "]";
190    }
191
192}