    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2011, 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     * OHLCDataItem.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     * 03-Dec-2003 : Version 1 (DG);
    038     * 29-Apr-2005 : Added equals() method (DG);
    039     *
    040     */
    041
    042    package org.jfree.data.xy;
    043
    044    import java.io.Serializable;
    045    import java.util.Date;
    046
    047    /**
    048     * Represents a single (open-high-low-close) data item in
    049     * an {@link DefaultOHLCDataset}.  This data item is commonly used
    050     * to summarise the trading activity of a financial commodity for
    051     * a fixed period (most often one day).
    052     */
    053    public class OHLCDataItem implements Comparable, Serializable {
    054
    055        /** For serialization. */
    056        private static final long serialVersionUID = 7753817154401169901L;
    057
    058        /** The date. */
    059        private Date date;
    060
    061        /** The open value. */
    062        private Number open;
    063
    064        /** The high value. */
    065        private Number high;
    066
    067        /** The low value. */
    068        private Number low;
    069
    070        /** The close value. */
    071        private Number close;
    072
    073        /** The trading volume (number of shares, contracts or whatever). */
    074        private Number volume;
    075
    076        /**
    077         * Creates a new item.
    078         *
    079         * @param date  the date (<code>null</code> not permitted).
    080         * @param open  the open value.
    081         * @param high  the high value.
    082         * @param low  the low value.
    083         * @param close  the close value.
    084         * @param volume  the volume.
    085         */
    086        public OHLCDataItem(Date date,
    087                            double open,
    088                            double high,
    089                            double low,
    090                            double close,
    091                            double volume) {
    092            if (date == null) {
    093                throw new IllegalArgumentException("Null 'date' argument.");
    094            }
    095            this.date = date;
    096            this.open = new Double(open);
    097            this.high = new Double(high);
    098            this.low = new Double(low);
    099            this.close = new Double(close);
    100            this.volume = new Double(volume);
    101        }
    102
    103        /**
    104         * Returns the date that the data item relates to.
    105         *
    106         * @return The date (never <code>null</code>).
    107         */
    108        public Date getDate() {
    109            return this.date;
    110        }
    111
    112        /**
    113         * Returns the open value.
    114         *
    115         * @return The open value.
    116         */
    117        public Number getOpen() {
    118            return this.open;
    119        }
    120
    121        /**
    122         * Returns the high value.
    123         *
    124         * @return The high value.
    125         */
    126        public Number getHigh() {
    127            return this.high;
    128        }
    129
    130        /**
    131         * Returns the low value.
    132         *
    133         * @return The low value.
    134         */
    135        public Number getLow() {
    136            return this.low;
    137        }
    138
    139        /**
    140         * Returns the close value.
    141         *
    142         * @return The close value.
    143         */
    144        public Number getClose() {
    145            return this.close;
    146        }
    147
    148        /**
    149         * Returns the volume.
    150         *
    151         * @return The volume.
    152         */
    153        public Number getVolume() {
    154            return this.volume;
    155        }
    156
    157        /**
    158         * Checks this instance for equality with an arbitrary object.
    159         *
    160         * @param obj  the object (<code>null</code> permitted).
    161         *
    162         * @return A boolean.
    163         */
    164        public boolean equals(Object obj) {
    165            if (obj == this) {
    166                return true;
    167            }
    168            if (!(obj instanceof OHLCDataItem)) {
    169                return false;
    170            }
    171            OHLCDataItem that = (OHLCDataItem) obj;
    172            if (!this.date.equals(that.date)) {
    173                return false;
    174            }
    175            if (!this.high.equals(that.high)) {
    176                return false;
    177            }
    178            if (!this.low.equals(that.low)) {
    179                return false;
    180            }
    181            if (!this.open.equals(that.open)) {
    182                return false;
    183            }
    184            if (!this.close.equals(that.close)) {
    185                return false;
    186            }
    187            return true;
    188        }
    189
    190        /**
    191         * Compares this object with the specified object for order. Returns a
    192         * negative integer, zero, or a positive integer as this object is less
    193         * than, equal to, or greater than the specified object.
    194         *
    195         * @param object  the object to compare to.
    196         *
    197         * @return A negative integer, zero, or a positive integer as this object
    198         *         is less than, equal to, or greater than the specified object.
    199         */
    200        public int compareTo(Object object) {
    201            if (object instanceof OHLCDataItem) {
    202                OHLCDataItem item = (OHLCDataItem) object;
    203                return this.date.compareTo(item.date);
    204            }
    205            else {
    206                throw new ClassCastException("OHLCDataItem.compareTo().");
    207            }
    208        }
    209
    210    }