    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, 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-2005, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: TimePeriodValue.java,v 1.4.2.1 2005/10/25 21:35:24 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 22-Apr-2003 : Version 1 (DG);
    040     *
    041     */
    042
    043    package org.jfree.data.time;
    044
    045    import java.io.Serializable;
    046
    047    /**
    048     * Represents a time period and an associated value.
    049     */
    050    public 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.
    065         * @param value  the value associated with the time period.
    066         */
    067        public TimePeriodValue(TimePeriod period, Number value) {
    068            this.period = period;
    069            this.value = value;
    070        }
    071
    072        /**
    073         * Constructs a new data pair.
    074         *
    075         * @param period  the time period.
    076         * @param value  the value associated with the time period.
    077         */
    078        public TimePeriodValue(TimePeriod period, double value) {
    079            this(period, new Double(value));
    080        }
    081
    082        /**
    083         * Returns the time period.
    084         *
    085         * @return The time period.
    086         */
    087        public TimePeriod getPeriod() {
    088            return this.period;
    089        }
    090
    091        /**
    092         * Returns the value.
    093         *
    094         * @return The value (possibly <code>null</code>).
    095         */
    096        public Number getValue() {
    097            return this.value;
    098        }
    099
    100        /**
    101         * Sets the value for this data item.
    102         *
    103         * @param value  the new value (<code>null</code> permitted).
    104         */
    105        public void setValue(Number value) {
    106            this.value = value;
    107        }
    108
    109        /**
    110         * Tests this object for equality with the target object.
    111         *
    112         * @param obj  the object (<code>null</code> permitted).
    113         *
    114         * @return A boolean.
    115         */
    116        public boolean equals(Object obj) {
    117            if (this == obj) {
    118                return true;
    119            }
    120            if (!(obj instanceof TimePeriodValue)) {
    121                return false;
    122            }
    123
    124            TimePeriodValue timePeriodValue = (TimePeriodValue) obj;
    125
    126            if (this.period != null ? !this.period.equals(timePeriodValue.period)
    127                    : timePeriodValue.period != null) {
    128                return false;
    129            }
    130            if (this.value != null ? !this.value.equals(timePeriodValue.value)
    131                    : timePeriodValue.value != null) {
    132                return false;
    133            }
    134
    135            return true;
    136        }
    137
    138        /**
    139         * Returns a hash code value for the object.
    140         *
    141         * @return The hashcode
    142         */
    143        public int hashCode() {
    144            int result;
    145            result = (this.period != null ? this.period.hashCode() : 0);
    146            result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
    147            return result;
    148        }
    149
    150        /**
    151         * Clones the object.
    152         * <P>
    153         * Note: no need to clone the period or value since they are immutable
    154         * classes.
    155         *
    156         * @return A clone.
    157         */
    158        public Object clone() {
    159            Object clone = null;
    160            try {
    161                clone = super.clone();
    162            }
    163            catch (CloneNotSupportedException e) { // won't get here...
    164                System.err.println("Operation not supported.");
    165            }
    166            return clone;
    167
    168        }
    169
    170    }