    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     * TimeSeriesDataItem.java
    029     * -----------------------
    030     * (C) Copyright 2001-2007, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * Changes
    036     * -------
    037     * 11-Oct-2001 : Version 1 (DG);
    038     * 15-Nov-2001 : Updated Javadoc comments (DG);
    039     * 29-Nov-2001 : Added cloning (DG);
    040     * 24-Jun-2002 : Removed unnecessary import (DG);
    041     * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    042     * 13-Mar-2003 : Renamed TimeSeriesDataPair --> TimeSeriesDataItem, moved to
    043     *               com.jrefinery.data.time package, implemented Serializable (DG)
    044     */
    045
    046    package org.jfree.data.time;
    047
    048    import java.io.Serializable;
    049
    050    /**
    051     * Represents one data item in a time series.
    052     * <P>
    053     * The time period can be any of the following:
    054     * <ul>
    055     * <li>{@link Year}</li>
    056     * <li>{@link Quarter}</li>
    057     * <li>{@link Month}</li>
    058     * <li>{@link Week}</li>
    059     * <li>{@link Day}</li>
    060     * <li>{@link Hour}</li>
    061     * <li>{@link Minute}</li>
    062     * <li>{@link Second}</li>
    063     * <li>{@link Millisecond}</li>
    064     * <li>{@link FixedMillisecond}</li>
    065     * </ul>
    066     *
    067     * The time period is an immutable property of the data item.  Data items will
    068     * often be sorted within a list, and allowing the time period to be changed
    069     * could destroy the sort order.
    070     * <P>
    071     * Implements the <code>Comparable</code> interface so that standard Java
    072     * sorting can be used to keep the data items in order.
    073     *
    074     */
    075    public class TimeSeriesDataItem implements Cloneable, Comparable, Serializable {
    076
    077        /** For serialization. */
    078        private static final long serialVersionUID = -2235346966016401302L;
    079
    080        /** The time period. */
    081        private RegularTimePeriod period;
    082
    083        /** The value associated with the time period. */
    084        private Number value;
    085
    086        /**
    087         * Constructs a new data item that associates a value with a time period.
    088         *
    089         * @param period  the time period (<code>null</code> not permitted).
    090         * @param value  the value (<code>null</code> permitted).
    091         */
    092        public TimeSeriesDataItem(RegularTimePeriod period, Number value) {
    093            if (period == null) {
    094                throw new IllegalArgumentException("Null 'period' argument.");
    095            }
    096            this.period = period;
    097            this.value = value;
    098        }
    099
    100        /**
    101         * Constructs a new data item that associates a value with a time period.
    102         *
    103         * @param period  the time period (<code>null</code> not permitted).
    104         * @param value  the value associated with the time period.
    105         */
    106        public TimeSeriesDataItem(RegularTimePeriod period, double value) {
    107            this(period, new Double(value));
    108        }
    109
    110        /**
    111         * Returns the time period.
    112         *
    113         * @return The time period (never <code>null</code>).
    114         */
    115        public RegularTimePeriod getPeriod() {
    116            return this.period;
    117        }
    118
    119        /**
    120         * Returns the value.
    121         *
    122         * @return The value (<code>null</code> possible).
    123         */
    124        public Number getValue() {
    125            return this.value;
    126        }
    127
    128        /**
    129         * Sets the value for this data item.
    130         *
    131         * @param value  the value (<code>null</code> permitted).
    132         */
    133        public void setValue(Number value) {
    134            this.value = value;
    135        }
    136
    137        /**
    138         * Tests this object for equality with an arbitrary object.
    139         *
    140         * @param o  the other object.
    141         *
    142         * @return A boolean.
    143         */
    144        public boolean equals(Object o) {
    145            if (this == o) {
    146                return true;
    147            }
    148            if (!(o instanceof TimeSeriesDataItem)) {
    149                return false;
    150            }
    151            TimeSeriesDataItem timeSeriesDataItem = (TimeSeriesDataItem) o;
    152            if (this.period != null) {
    153                if (!this.period.equals(timeSeriesDataItem.period)) {
    154                    return false;
    155                }
    156            }
    157            else if (timeSeriesDataItem.period != null) {
    158               return false;
    159            }
    160
    161            if (this.value != null) {
    162                if (!this.value.equals(timeSeriesDataItem.value)) {
    163                    return false;
    164                }
    165            }
    166            else if (timeSeriesDataItem.value != null) {
    167                return false;
    168            }
    169
    170            return true;
    171        }
    172
    173        /**
    174         * Returns a hash code.
    175         *
    176         * @return A hash code.
    177         */
    178        public int hashCode() {
    179            int result;
    180            result = (this.period != null ? this.period.hashCode() : 0);
    181            result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
    182            return result;
    183        }
    184
    185        /**
    186         * Returns an integer indicating the order of this data pair object
    187         * relative to another object.
    188         * <P>
    189         * For the order we consider only the timing:
    190         * negative == before, zero == same, positive == after.
    191         *
    192         * @param o1  The object being compared to.
    193         *
    194         * @return An integer indicating the order of the data item object
    195         *         relative to another object.
    196         */
    197        public int compareTo(Object o1) {
    198
    199            int result;
    200
    201            // CASE 1 : Comparing to another TimeSeriesDataItem object
    202            // -------------------------------------------------------
    203            if (o1 instanceof TimeSeriesDataItem) {
    204                TimeSeriesDataItem datapair = (TimeSeriesDataItem) o1;
    205                result = getPeriod().compareTo(datapair.getPeriod());
    206            }
    207
    208            // CASE 2 : Comparing to a general object
    209            // ---------------------------------------------
    210            else {
    211                // consider time periods to be ordered after general objects
    212                result = 1;
    213            }
    214
    215            return result;
    216
    217        }
    218
    219        /**
    220         * Clones the data item.  Note: there is no need to clone the period or
    221         * value since they are immutable classes.
    222         *
    223         * @return A clone of the data item.
    224         */
    225        public Object clone() {
    226            Object clone = null;
    227            try {
    228                clone = super.clone();
    229            }
    230            catch (CloneNotSupportedException e) { // won't get here...
    231                e.printStackTrace();
    232            }
    233            return clone;
    234        }
    235
    236    }