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