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 * OHLCSeries.java
029 * ---------------
030 * (C) Copyright 2006-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data.time.ohlc;
038
039import org.jfree.chart.util.Args;
040import org.jfree.data.ComparableObjectItem;
041import org.jfree.data.ComparableObjectSeries;
042import org.jfree.data.time.RegularTimePeriod;
043
044/**
045 * A list of ({@link RegularTimePeriod}, open, high, low, close) data items.
046 *
047 * @see OHLCSeriesCollection
048 */
049public class OHLCSeries extends ComparableObjectSeries {
050
051    /**
052     * Creates a new empty series.  By default, items added to the series will
053     * be sorted into ascending order by period, and duplicate periods will
054     * not be allowed.
055     *
056     * @param key  the series key ({@code null} not permitted).
057     */
058    public OHLCSeries(Comparable key) {
059        super(key, true, false);
060    }
061
062    /**
063     * Returns the time period for the specified item.
064     *
065     * @param index  the item index.
066     *
067     * @return The time period.
068     */
069    public RegularTimePeriod getPeriod(int index) {
070        OHLCItem item = (OHLCItem) getDataItem(index);
071        return item.getPeriod();
072    }
073
074    /**
075     * Returns the data item at the specified index.
076     *
077     * @param index  the item index.
078     *
079     * @return The data item.
080     */
081    @Override
082    public ComparableObjectItem getDataItem(int index) {
083        return super.getDataItem(index);
084    }
085
086    /**
087     * Adds a data item to the series.
088     *
089     * @param period  the period.
090     * @param open  the open-value.
091     * @param high  the high-value.
092     * @param low  the low-value.
093     * @param close  the close-value.
094     */
095    public void add(RegularTimePeriod period, double open, double high,
096            double low, double close) {
097        if (getItemCount() > 0) {
098            OHLCItem item0 = (OHLCItem) this.getDataItem(0);
099            if (!period.getClass().equals(item0.getPeriod().getClass())) {
100                throw new IllegalArgumentException(
101                        "Can't mix RegularTimePeriod class types.");
102            }
103        }
104        super.add(new OHLCItem(period, open, high, low, close), true);
105    }
106    
107    /**
108     * Adds a data item to the series.  The values from the item passed to
109     * this method will be copied into a new object.
110     * 
111     * @param item  the item ({@code null} not permitted).
112     */
113    public void add(OHLCItem item) {
114        Args.nullNotPermitted(item, "item");
115        add(item.getPeriod(), item.getOpenValue(), item.getHighValue(),
116                item.getLowValue(), item.getCloseValue());
117    }
118
119    /**
120     * Removes the item with the specified index.
121     *
122     * @param index  the item index.
123     * 
124     * @return The item removed.
125     */
126    @Override
127    public ComparableObjectItem remove(int index) {
128        return super.remove(index);
129    }
130
131}