001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, 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-2007, 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 *
040 */
041
042package org.jfree.data.time;
043
044import java.io.Serializable;
045
046/**
047 * Represents a time period and an associated value.
048 */
049public class TimePeriodValue implements Cloneable, Serializable {
050
051    /** For serialization. */
052    private static final long serialVersionUID = 3390443360845711275L;
053    
054    /** The time period. */
055    private TimePeriod period;
056
057    /** The value associated with the time period. */
058    private Number value;
059
060    /**
061     * Constructs a new data item.
062     *
063     * @param period  the time period (<code>null</code> not permitted).
064     * @param value  the value associated with the time period.
065     * 
066     * @throws IllegalArgumentException if <code>period</code> is 
067     *     <code>null</code>.
068     */
069    public TimePeriodValue(TimePeriod period, Number value) {
070        if (period == null) {
071            throw new IllegalArgumentException("Null 'period' argument.");
072        }
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</code> not permitted).
081     * @param value  the value associated with the time period.
082     * 
083     * @throws IllegalArgumentException if <code>period</code> is 
084     *     <code>null</code>.
085     */
086    public TimePeriodValue(TimePeriod period, double value) {
087        this(period, new Double(value));
088    }
089
090    /**
091     * Returns the time period.
092     *
093     * @return The time period (never <code>null</code>).
094     */
095    public TimePeriod getPeriod() {
096        return this.period;
097    }
098
099    /**
100     * Returns the value.
101     *
102     * @return The value (possibly <code>null</code>).
103     * 
104     * @see #setValue(Number)
105     */
106    public Number getValue() {
107        return this.value;
108    }
109
110    /**
111     * Sets the value for this data item.
112     *
113     * @param value  the new value (<code>null</code> permitted).
114     * 
115     * @see #getValue()
116     */
117    public void setValue(Number value) {
118        this.value = value;
119    }
120
121    /**
122     * Tests this object for equality with the target object.
123     *
124     * @param obj  the object (<code>null</code> permitted).
125     *
126     * @return A boolean.
127     */
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    public int hashCode() {
156        int result;
157        result = (this.period != null ? this.period.hashCode() : 0);
158        result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
159        return result;
160    }
161
162    /**
163     * Clones the object.
164     * <P>
165     * Note: no need to clone the period or value since they are immutable 
166     * classes.
167     *
168     * @return A clone.
169     */
170    public Object clone() {
171        Object clone = null;
172        try {
173            clone = super.clone();
174        }
175        catch (CloneNotSupportedException e) { // won't get here...
176            e.printStackTrace();
177        }
178        return clone;
179    }
180
181}