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 * DefaultOHLCDataset.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.util.Arrays;
040import java.util.Date;
041import org.jfree.chart.util.PublicCloneable;
042
043/**
044 * A simple implementation of the {@link OHLCDataset} interface.  This
045 * implementation supports only one series.
046 */
047public class DefaultOHLCDataset extends AbstractXYDataset
048        implements OHLCDataset, PublicCloneable {
049
050    /** The series key. */
051    private Comparable key;
052
053    /** Storage for the data items. */
054    private OHLCDataItem[] data;
055
056    /**
057     * Creates a new dataset.
058     *
059     * @param key  the series key.
060     * @param data  the data items.
061     */
062    public DefaultOHLCDataset(Comparable key, OHLCDataItem[] data) {
063        this.key = key;
064        this.data = data;
065    }
066
067    /**
068     * Returns the series key.
069     *
070     * @param series  the series index (ignored).
071     *
072     * @return The series key.
073     */
074    @Override
075    public Comparable getSeriesKey(int series) {
076        return this.key;
077    }
078
079    /**
080     * Returns the x-value for a data item.
081     *
082     * @param series  the series index (ignored).
083     * @param item  the item index (zero-based).
084     *
085     * @return The x-value.
086     */
087    @Override
088    public Number getX(int series, int item) {
089        return this.data[item].getDate().getTime();
090    }
091
092    /**
093     * Returns the x-value for a data item as a date.
094     *
095     * @param series  the series index (ignored).
096     * @param item  the item index (zero-based).
097     *
098     * @return The x-value as a date.
099     */
100    public Date getXDate(int series, int item) {
101        return this.data[item].getDate();
102    }
103
104    /**
105     * Returns the y-value.
106     *
107     * @param series  the series index (ignored).
108     * @param item  the item index (zero-based).
109     *
110     * @return The y value.
111     */
112    @Override
113    public Number getY(int series, int item) {
114        return getClose(series, item);
115    }
116
117    /**
118     * Returns the high value.
119     *
120     * @param series  the series index (ignored).
121     * @param item  the item index (zero-based).
122     *
123     * @return The high value.
124     */
125    @Override
126    public Number getHigh(int series, int item) {
127        return this.data[item].getHigh();
128    }
129
130    /**
131     * Returns the high-value (as a double primitive) for an item within a
132     * series.
133     *
134     * @param series  the series (zero-based index).
135     * @param item  the item (zero-based index).
136     *
137     * @return The high-value.
138     */
139    @Override
140    public double getHighValue(int series, int item) {
141        double result = Double.NaN;
142        Number high = getHigh(series, item);
143        if (high != null) {
144            result = high.doubleValue();
145        }
146        return result;
147    }
148
149    /**
150     * Returns the low value.
151     *
152     * @param series  the series index (ignored).
153     * @param item  the item index (zero-based).
154     *
155     * @return The low value.
156     */
157    @Override
158    public Number getLow(int series, int item) {
159        return this.data[item].getLow();
160    }
161
162    /**
163     * Returns the low-value (as a double primitive) for an item within a
164     * series.
165     *
166     * @param series  the series (zero-based index).
167     * @param item  the item (zero-based index).
168     *
169     * @return The low-value.
170     */
171    @Override
172    public double getLowValue(int series, int item) {
173        double result = Double.NaN;
174        Number low = getLow(series, item);
175        if (low != null) {
176            result = low.doubleValue();
177        }
178        return result;
179    }
180
181    /**
182     * Returns the open value.
183     *
184     * @param series  the series index (ignored).
185     * @param item  the item index (zero-based).
186     *
187     * @return The open value.
188     */
189    @Override
190    public Number getOpen(int series, int item) {
191        return this.data[item].getOpen();
192    }
193
194    /**
195     * Returns the open-value (as a double primitive) for an item within a
196     * series.
197     *
198     * @param series  the series (zero-based index).
199     * @param item  the item (zero-based index).
200     *
201     * @return The open-value.
202     */
203    @Override
204    public double getOpenValue(int series, int item) {
205        double result = Double.NaN;
206        Number open = getOpen(series, item);
207        if (open != null) {
208            result = open.doubleValue();
209        }
210        return result;
211    }
212
213    /**
214     * Returns the close value.
215     *
216     * @param series  the series index (ignored).
217     * @param item  the item index (zero-based).
218     *
219     * @return The close value.
220     */
221    @Override
222    public Number getClose(int series, int item) {
223        return this.data[item].getClose();
224    }
225
226    /**
227     * Returns the close-value (as a double primitive) for an item within a
228     * series.
229     *
230     * @param series  the series (zero-based index).
231     * @param item  the item (zero-based index).
232     *
233     * @return The close-value.
234     */
235    @Override
236    public double getCloseValue(int series, int item) {
237        double result = Double.NaN;
238        Number close = getClose(series, item);
239        if (close != null) {
240            result = close.doubleValue();
241        }
242        return result;
243    }
244
245    /**
246     * Returns the trading volume.
247     *
248     * @param series  the series index (ignored).
249     * @param item  the item index (zero-based).
250     *
251     * @return The trading volume.
252     */
253    @Override
254    public Number getVolume(int series, int item) {
255        return this.data[item].getVolume();
256    }
257
258    /**
259     * Returns the volume-value (as a double primitive) for an item within a
260     * series.
261     *
262     * @param series  the series (zero-based index).
263     * @param item  the item (zero-based index).
264     *
265     * @return The volume-value.
266     */
267    @Override
268    public double getVolumeValue(int series, int item) {
269        double result = Double.NaN;
270        Number volume = getVolume(series, item);
271        if (volume != null) {
272            result = volume.doubleValue();
273        }
274        return result;
275    }
276
277    /**
278     * Returns the series count.
279     *
280     * @return 1.
281     */
282    @Override
283    public int getSeriesCount() {
284        return 1;
285    }
286
287    /**
288     * Returns the item count for the specified series.
289     *
290     * @param series  the series index (ignored).
291     *
292     * @return The item count.
293     */
294    @Override
295    public int getItemCount(int series) {
296        return this.data.length;
297    }
298
299    /**
300     * Sorts the data into ascending order by date.
301     */
302    public void sortDataByDate() {
303        Arrays.sort(this.data);
304    }
305
306    /**
307     * Tests this instance for equality with an arbitrary object.
308     *
309     * @param obj  the object ({@code null} permitted).
310     *
311     * @return A boolean.
312     */
313    @Override
314    public boolean equals(Object obj) {
315        if (this == obj) {
316            return true;
317        }
318        if (!(obj instanceof DefaultOHLCDataset)) {
319            return false;
320        }
321        DefaultOHLCDataset that = (DefaultOHLCDataset) obj;
322        if (!this.key.equals(that.key)) {
323            return false;
324        }
325        if (!Arrays.equals(this.data, that.data)) {
326            return false;
327        }
328        return true;
329    }
330
331    /**
332     * Returns an independent copy of this dataset.
333     *
334     * @return A clone.
335     *
336     * @throws CloneNotSupportedException if there is a cloning problem.
337     */
338    @Override
339    public Object clone() throws CloneNotSupportedException {
340        DefaultOHLCDataset clone = (DefaultOHLCDataset) super.clone();
341        clone.data = new OHLCDataItem[this.data.length];
342        System.arraycopy(this.data, 0, clone.data, 0, this.data.length);
343        return clone;
344    }
345
346}