001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, 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 * OHLCSeries.java
029 * ---------------
030 * (C) Copyright 2006, 2007, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 04-Dec-2006 : Version 1 (DG);
038 *
039 */
040
041package org.jfree.data.time.ohlc;
042
043import org.jfree.data.ComparableObjectItem;
044import org.jfree.data.ComparableObjectSeries;
045import org.jfree.data.time.RegularTimePeriod;
046
047/**
048 * A list of (RegularTimePeriod, open, high, low, close) data items.
049 *
050 * @since 1.0.4
051 *
052 * @see OHLCSeriesCollection
053 */
054public class OHLCSeries extends ComparableObjectSeries {
055
056    /**
057     * Creates a new empty series.  By default, items added to the series will
058     * be sorted into ascending order by period, and duplicate periods will
059     * not be allowed.
060     *
061     * @param key  the series key (<code>null</code> not permitted).
062     */
063    public OHLCSeries(Comparable key) {
064        super(key, true, false);
065    }
066
067    /**
068     * Returns the time period for the specified item.
069     *
070     * @param index  the item index.
071     *
072     * @return The time period.
073     */
074    public RegularTimePeriod getPeriod(int index) {
075        final OHLCItem item = (OHLCItem) getDataItem(index);
076        return item.getPeriod();
077    }
078
079    /**
080     * Returns the data item at the specified index.
081     *
082     * @param index  the item index.
083     *
084     * @return The data item.
085     */
086    public ComparableObjectItem getDataItem(int index) {
087        return super.getDataItem(index);
088    }
089
090    /**
091     * Adds a data item to the series.
092     *
093     * @param period  the period.
094     * @param open  the open-value.
095     * @param high  the high-value.
096     * @param low  the low-value.
097     * @param close  the close-value.
098     */
099    public void add(RegularTimePeriod period, double open, double high,
100            double low, double close) {
101        if (getItemCount() > 0) {
102            OHLCItem item0 = (OHLCItem) this.getDataItem(0);
103            if (!period.getClass().equals(item0.getPeriod().getClass())) {
104                throw new IllegalArgumentException(
105                        "Can't mix RegularTimePeriod class types.");
106            }
107        }
108        super.add(new OHLCItem(period, open, high, low, close), true);
109    }
110
111}