Update of /cvsroot/jfreechart/jfreechart/source/org/jfree/data/time/ohlc In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv24222/source/org/jfree/data/time/ohlc Added Files: Tag: STABLE_1-0-0 OHLC.java OHLCItem.java OHLCSeries.java OHLCSeriesCollection.java package.html Log Message: 2006-12-04 David Gilbert <dav...@ob...> * source/org/jfree/data/time/ohlc/OHLC.java: New file, * source/org/jfree/data/time/ohlc/OHLCItem.java: New file, * source/org/jfree/data/time/ohlc/OHLCSeries.java: New file, * source/org/jfree/data/time/ohlc/OHLCSeriesCollection.java: New file, * source/org/jfree/data/time/ohlc/package.html: New file, * tests/org/jfree/data/time/ohlc/junit/OHLCItemTests.java: New file, * tests/org/jfree/data/time/ohlc/junit/OHLCPackageTests.java: New file, * tests/org/jfree/data/time/ohlc/junit/OHLCSeriesCollectionTests.java: New tests, * tests/org/jfree/data/time/ohlc/junit/OHLCSeriesTests.java: New file, * tests/org/jfree/data/time/ohlc/junit/OHLCTests.java: New file. --- NEW FILE: OHLCSeriesCollection.java --- /* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors. * * Project Info: http://www.jfree.org/jfreechart/index.html * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * ------------------------- * OHLCSeriesCollection.java * ------------------------- * (C) Copyright 2006, by Object Refinery Limited. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; * * $Id: OHLCSeriesCollection.java,v 1.1.2.1 2006/12/04 17:08:36 mungady Exp $ * * Changes * ------- * 04-Dec-2006 : Version 1 (DG); * */ package org.jfree.data.time.ohlc; import java.io.Serializable; import java.util.List; import org.jfree.data.general.DatasetChangeEvent; import org.jfree.data.time.RegularTimePeriod; import org.jfree.data.time.TimePeriodAnchor; import org.jfree.data.xy.AbstractXYDataset; import org.jfree.data.xy.OHLCDataset; import org.jfree.util.ObjectUtilities; /** * A collection of {@link OHLCSeries} objects. * * @since 1.0.4 * * @see OHLCSeries */ public class OHLCSeriesCollection extends AbstractXYDataset implements OHLCDataset, Serializable { /** Storage for the data series. */ private List data; private TimePeriodAnchor xPosition = TimePeriodAnchor.MIDDLE; /** * Creates a new instance of <code>OHLCSeriesCollection</code>. */ public OHLCSeriesCollection() { this.data = new java.util.ArrayList(); } /** * Adds a series to the collection and sends a {@link DatasetChangeEvent} * to all registered listeners. * * @param series the series (<code>null</code> not permitted). */ public void addSeries(OHLCSeries series) { if (series == null) { throw new IllegalArgumentException("Null 'series' argument."); } this.data.add(series); series.addChangeListener(this); fireDatasetChanged(); } /** * Returns the number of series in the collection. * * @return The series count. */ public int getSeriesCount() { return this.data.size(); } /** * Returns a series from the collection. * * @param series the series index (zero-based). * * @return The series. * * @throws IllegalArgumentException if <code>series</code> is not in the * range <code>0</code> to <code>getSeriesCount() - 1</code>. */ public OHLCSeries getSeries(int series) { if ((series < 0) || (series >= getSeriesCount())) { throw new IllegalArgumentException("Series index out of bounds"); } return (OHLCSeries) this.data.get(series); } /** * Returns the key for a series. * * @param series the series index (in the range <code>0</code> to * <code>getSeriesCount() - 1</code>). * * @return The key for a series. * * @throws IllegalArgumentException if <code>series</code> is not in the * specified range. */ public Comparable getSeriesKey(int series) { // defer argument checking return getSeries(series).getKey(); } /** * Returns the number of items in the specified series. * * @param series the series (zero-based index). * * @return The item count. * * @throws IllegalArgumentException if <code>series</code> is not in the * range <code>0</code> to <code>getSeriesCount() - 1</code>. */ public int getItemCount(int series) { // defer argument checking return getSeries(series).getItemCount(); } /** * Returns the x-value for a time period. * * @param period the time period (<code>null</code> not permitted). * * @return The x-value. */ protected synchronized long getX(RegularTimePeriod period) { long result = 0L; if (this.xPosition == TimePeriodAnchor.START) { result = period.getFirstMillisecond(); } else if (this.xPosition == TimePeriodAnchor.MIDDLE) { result = period.getMiddleMillisecond(); } else if (this.xPosition == TimePeriodAnchor.END) { result = period.getLastMillisecond(); } return result; } /** * Returns the x-value for an item within a series. * * @param series the series index. * @param item the item index. * * @return The x-value. */ public double getXValue(int series, int item) { OHLCSeries s = (OHLCSeries) this.data.get(series); OHLCItem di = (OHLCItem) s.getDataItem(item); RegularTimePeriod period = di.getPeriod(); return getX(period); } /** * Returns the x-value for an item within a series. * * @param series the series index. * @param item the item index. * * @return The x-value. */ public Number getX(int series, int item) { return new Double(getXValue(series, item)); } /** * Returns the y-value for an item within a series. * * @param series the series index. * @param item the item index. * * @return The y-value. */ public Number getY(int series, int item) { OHLCSeries s = (OHLCSeries) this.data.get(series); OHLCItem di = (OHLCItem) s.getDataItem(item); return new Double(di.getYValue()); } /** * Returns the open-value for an item within a series. * * @param series the series index. * @param item the item index. * * @return The open-value. */ public double getOpenValue(int series, int item) { OHLCSeries s = (OHLCSeries) this.data.get(series); OHLCItem di = (OHLCItem) s.getDataItem(item); return di.getOpenValue(); } /** * Returns the open-value for an item within a series. * * @param series the series index. * @param item the item index. * * @return The open-value. */ public Number getOpen(int series, int item) { return new Double(getOpenValue(series, item)); } /** * Returns the close-value for an item within a series. * * @param series the series index. * @param item the item index. * * @return The close-value. */ public double getCloseValue(int series, int item) { OHLCSeries s = (OHLCSeries) this.data.get(series); OHLCItem di = (OHLCItem) s.getDataItem(item); return di.getCloseValue(); } /** * Returns the close-value for an item within a series. * * @param series the series index. * @param item the item index. * * @return The close-value. */ public Number getClose(int series, int item) { return new Double(getCloseValue(series, item)); } /** * Returns the high-value for an item within a series. * * @param series the series index. * @param item the item index. * * @return The high-value. */ public double getHighValue(int series, int item) { OHLCSeries s = (OHLCSeries) this.data.get(series); OHLCItem di = (OHLCItem) s.getDataItem(item); return di.getHighValue(); } /** * Returns the high-value for an item within a series. * * @param series the series index. * @param item the item index. * * @return The high-value. */ public Number getHigh(int series, int item) { return new Double(getHighValue(series, item)); } /** * Returns the low-value for an item within a series. * * @param series the series index. * @param item the item index. * * @return The low-value. */ public double getLowValue(int series, int item) { OHLCSeries s = (OHLCSeries) this.data.get(series); OHLCItem di = (OHLCItem) s.getDataItem(item); return di.getLowValue(); } /** * Returns the low-value for an item within a series. * * @param series the series index. * @param item the item index. * * @return The low-value. */ public Number getLow(int series, int item) { return new Double(getLowValue(series, item)); } public Number getVolume(int series, int item) { return null; } public double getVolumeValue(int series, int item) { return Double.NaN; } /** * Tests this instance for equality with an arbitrary object. * * @param obj the object (<code>null</code> permitted). * * @return A boolean. */ public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof OHLCSeriesCollection)) { return false; } OHLCSeriesCollection that = (OHLCSeriesCollection) obj; return ObjectUtilities.equal(this.data, that.data); } /** * Returns a clone of this instance. * * @return A clone. * * @throws CloneNotSupportedException if there is a problem. */ public Object clone() throws CloneNotSupportedException { OHLCSeriesCollection clone = (OHLCSeriesCollection) super.clone(); clone.data = (List) ObjectUtilities.deepClone(this.data); return clone; } } --- NEW FILE: package.html --- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <html> <body bgcolor="white"> Classes for representing financial data in open-high-low-close form. </body> </html> --- NEW FILE: OHLCItem.java --- /* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors. * * Project Info: http://www.jfree.org/jfreechart/index.html * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * ------------- * OHLCItem.java * ------------- * (C) Copyright 2006, by Object Refinery Limited. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; * * $Id: OHLCItem.java,v 1.1.2.1 2006/12/04 17:08:36 mungady Exp $ * * Changes * ------- * 04-Dec-2006 : Version 1 (DG); * */ package org.jfree.data.time.ohlc; import org.jfree.data.ComparableObjectItem; import org.jfree.data.time.RegularTimePeriod; /** * An item representing data in the form (period, open, high, low, close). * * @since 1.0.4 */ public class OHLCItem extends ComparableObjectItem { /** * Creates a new instance of <code>OHLCItem</code>. * * @param period the time period. * @param open the open-value. * @param high the high-value. * @param low the low-value. * @param close the close-value. */ public OHLCItem(RegularTimePeriod period, double open, double high, double low, double close) { super(period, new OHLC(open, high, low, close)); } /** * Returns the period. * * @return The period (never <code>null</code>). */ public RegularTimePeriod getPeriod() { return (RegularTimePeriod) getComparable(); } /** * Returns the y-value. * * @return The y-value. */ public double getYValue() { return getCloseValue(); } /** * Returns the open value. * * @return The open value. */ public double getOpenValue() { OHLC ohlc = (OHLC) getObject(); if (ohlc != null) { return ohlc.getOpen(); } else { return Double.NaN; } } /** * Returns the high value. * * @return The high value. */ public double getHighValue() { OHLC ohlc = (OHLC) getObject(); if (ohlc != null) { return ohlc.getHigh(); } else { return Double.NaN; } } /** * Returns the low value. * * @return The low value. */ public double getLowValue() { OHLC ohlc = (OHLC) getObject(); if (ohlc != null) { return ohlc.getLow(); } else { return Double.NaN; } } /** * Returns the close value. * * @return The close value. */ public double getCloseValue() { OHLC ohlc = (OHLC) getObject(); if (ohlc != null) { return ohlc.getClose(); } else { return Double.NaN; } } } --- NEW FILE: OHLCSeries.java --- /* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors. * * Project Info: http://www.jfree.org/jfreechart/index.html * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * --------------- * OHLCSeries.java * --------------- * (C) Copyright 2006, by Object Refinery Limited. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; * * $Id: OHLCSeries.java,v 1.1.2.1 2006/12/04 17:08:36 mungady Exp $ * * Changes * ------- * 04-Dec-2006 : Version 1 (DG); * */ package org.jfree.data.time.ohlc; import org.jfree.data.ComparableObjectItem; import org.jfree.data.ComparableObjectSeries; import org.jfree.data.time.RegularTimePeriod; /** * A list of (RegularTimePeriod, open, high, low, close) data items. * * @since 1.0.4 * * @see OHLCSeriesCollection */ public class OHLCSeries extends ComparableObjectSeries { /** * Creates a new empty series. By default, items added to the series will * be sorted into ascending order by period, and duplicate periods will * not be allowed. * * @param key the series key (<code>null</code> not permitted). */ public OHLCSeries(Comparable key) { super(key, true, false); } /** * Returns the time period for the specified item. * * @param index the item index. * * @return The time period. */ public RegularTimePeriod getPeriod(int index) { final OHLCItem item = (OHLCItem) getDataItem(index); return item.getPeriod(); } /** * Returns the data item at the specified index. * * @param index the item index. * * @return The data item. */ public ComparableObjectItem getDataItem(int index) { return super.getDataItem(index); } /** * Adds a data item to the series. * * @param period the period. * @param open the open-value. * @param high the high-value. * @param low the low-value. * @param close the close-value. */ public void add(RegularTimePeriod period, double open, double high, double low, double close) { if (getItemCount() > 0) { OHLCItem item0 = (OHLCItem) this.getDataItem(0); if (!period.getClass().equals(item0.getPeriod().getClass())) { throw new IllegalArgumentException( "Can't mix RegularTimePeriod class types."); } } super.add(new OHLCItem(period, open, high, low, close), true); } } --- NEW FILE: OHLC.java --- /* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors. * * Project Info: http://www.jfree.org/jfreechart/index.html * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * --------- * OHLC.java * --------- * (C) Copyright 2006, by Object Refinery Limited. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; * * $Id: OHLC.java,v 1.1.2.1 2006/12/04 17:08:36 mungady Exp $ * * Changes * ------- * 04-Dec-2006 : Version 1 (DG); * */ package org.jfree.data.time.ohlc; import java.io.Serializable; /** * A high low data record (immutable). This class is used internally by the * {@link OHLCItem} class. * * @since 1.0.4 */ public class OHLC implements Serializable { /** The open value. */ private double open; /** The close value. */ private double close; /** The high value. */ private double high; /** The low value. */ private double low; /** * Creates a new instance of <code>OHLC</code>. * * @param open the open value. * @param close the close value. * @param high the high value. * @param low the low value. */ public OHLC(double open, double high, double low, double close) { this.open = open; this.close = close; this.high = high; this.low = low; } /** * Returns the open value. * * @return The open value. */ public double getOpen() { return this.open; } /** * Returns the close value. * * @return The close value. */ public double getClose() { return this.close; } /** * Returns the high value. * * @return The high value. */ public double getHigh() { return this.high; } /** * Returns the low value. * * @return The low value. */ public double getLow() { return this.low; } /** * Tests this instance for equality with an arbitrary object. * * @param obj the object (<code>null</code> permitted). * * @return A boolean. */ public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof OHLC)) { return false; } OHLC that = (OHLC) obj; if (this.open != that.open) { return false; } if (this.close != that.close) { return false; } if (this.high != that.high) { return false; } if (this.low != that.low) { return false; } return true; } } |