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