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