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