001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * SubseriesDataset.java
029: * ---------------------
030: * (C) Copyright 2001-2007, by Bill Kelemen and Contributors.
031: *
032: * Original Author: Bill Kelemen;
033: * Contributor(s): Sylvain Vieujot;
034: * David Gilbert (for Object Refinery Limited);
035: *
036: * $Id: SubSeriesDataset.java,v 1.5.2.3 2007/02/02 15:50:44 mungady Exp $
037: *
038: * Changes
039: * -------
040: * 06-Dec-2001 : Version 1 (BK);
041: * 05-Feb-2002 : Added SignalsDataset (and small change to HighLowDataset
042: * interface) as requested by Sylvain Vieujot (DG);
043: * 28-Feb-2002 : Fixed bug: missing map[series] in IntervalXYDataset and
044: * SignalsDataset methods (BK);
045: * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
046: * 06-May-2004 : Now extends AbstractIntervalXYDataset (DG);
047: * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
048: * getYValue() (DG);
049: * 29-Nov-2005 : Removed SignalsDataset (DG);
050: * ------------- JFREECHART 1.0.x ---------------------------------------------
051: * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
052: *
053: */
054:
055: package org.jfree.data.general;
056:
057: import org.jfree.data.xy.AbstractIntervalXYDataset;
058: import org.jfree.data.xy.OHLCDataset;
059: import org.jfree.data.xy.IntervalXYDataset;
060: import org.jfree.data.xy.XYDataset;
061:
062: /**
063: * This class will create a dataset with one or more series from another
064: * {@link SeriesDataset}.
065: */
066: public class SubSeriesDataset extends AbstractIntervalXYDataset
067: implements OHLCDataset, IntervalXYDataset, CombinationDataset {
068:
069: /** The parent dataset. */
070: private SeriesDataset parent = null;
071:
072: /** Storage for map. */
073: private int[] map; // maps our series into our parent's
074:
075: /**
076: * Creates a SubSeriesDataset using one or more series from
077: * <code>parent</code>. The series to use are passed as an array of int.
078: *
079: * @param parent underlying dataset
080: * @param map int[] of series from parent to include in this Dataset
081: */
082: public SubSeriesDataset(SeriesDataset parent, int[] map) {
083: this .parent = parent;
084: this .map = map;
085: }
086:
087: /**
088: * Creates a SubSeriesDataset using one series from <code>parent</code>.
089: * The series to is passed as an int.
090: *
091: * @param parent underlying dataset
092: * @param series series from parent to include in this Dataset
093: */
094: public SubSeriesDataset(SeriesDataset parent, int series) {
095: this (parent, new int[] { series });
096: }
097:
098: ///////////////////////////////////////////////////////////////////////////
099: // From HighLowDataset
100: ///////////////////////////////////////////////////////////////////////////
101:
102: /**
103: * Returns the high-value for the specified series and item.
104: * <p>
105: * Note: throws <code>ClassCastException</code> if the series if not from a
106: * {@link OHLCDataset}.
107: *
108: * @param series the index of the series of interest (zero-based).
109: * @param item the index of the item of interest (zero-based).
110: *
111: * @return The high-value for the specified series and item.
112: */
113: public Number getHigh(int series, int item) {
114: return ((OHLCDataset) this .parent).getHigh(this .map[series],
115: item);
116: }
117:
118: /**
119: * Returns the high-value (as a double primitive) for an item within a
120: * series.
121: *
122: * @param series the series (zero-based index).
123: * @param item the item (zero-based index).
124: *
125: * @return The high-value.
126: */
127: public double getHighValue(int series, int item) {
128: double result = Double.NaN;
129: Number high = getHigh(series, item);
130: if (high != null) {
131: result = high.doubleValue();
132: }
133: return result;
134: }
135:
136: /**
137: * Returns the low-value for the specified series and item.
138: * <p>
139: * Note: throws <code>ClassCastException</code> if the series if not from a
140: * {@link OHLCDataset}.
141: *
142: * @param series the index of the series of interest (zero-based).
143: * @param item the index of the item of interest (zero-based).
144: *
145: * @return The low-value for the specified series and item.
146: */
147: public Number getLow(int series, int item) {
148: return ((OHLCDataset) this .parent).getLow(this .map[series],
149: item);
150: }
151:
152: /**
153: * Returns the low-value (as a double primitive) for an item within a
154: * series.
155: *
156: * @param series the series (zero-based index).
157: * @param item the item (zero-based index).
158: *
159: * @return The low-value.
160: */
161: public double getLowValue(int series, int item) {
162: double result = Double.NaN;
163: Number low = getLow(series, item);
164: if (low != null) {
165: result = low.doubleValue();
166: }
167: return result;
168: }
169:
170: /**
171: * Returns the open-value for the specified series and item.
172: * <p>
173: * Note: throws <code>ClassCastException</code> if the series if not from a
174: * {@link OHLCDataset}.
175: *
176: * @param series the index of the series of interest (zero-based).
177: * @param item the index of the item of interest (zero-based).
178: *
179: * @return The open-value for the specified series and item.
180: */
181: public Number getOpen(int series, int item) {
182: return ((OHLCDataset) this .parent).getOpen(this .map[series],
183: item);
184: }
185:
186: /**
187: * Returns the open-value (as a double primitive) for an item within a
188: * series.
189: *
190: * @param series the series (zero-based index).
191: * @param item the item (zero-based index).
192: *
193: * @return The open-value.
194: */
195: public double getOpenValue(int series, int item) {
196: double result = Double.NaN;
197: Number open = getOpen(series, item);
198: if (open != null) {
199: result = open.doubleValue();
200: }
201: return result;
202: }
203:
204: /**
205: * Returns the close-value for the specified series and item.
206: * <p>
207: * Note: throws <code>ClassCastException</code> if the series if not from a
208: * {@link OHLCDataset}.
209: *
210: * @param series the index of the series of interest (zero-based).
211: * @param item the index of the item of interest (zero-based).
212: *
213: * @return The close-value for the specified series and item.
214: */
215: public Number getClose(int series, int item) {
216: return ((OHLCDataset) this .parent).getClose(this .map[series],
217: item);
218: }
219:
220: /**
221: * Returns the close-value (as a double primitive) for an item within a
222: * series.
223: *
224: * @param series the series (zero-based index).
225: * @param item the item (zero-based index).
226: *
227: * @return The close-value.
228: */
229: public double getCloseValue(int series, int item) {
230: double result = Double.NaN;
231: Number close = getClose(series, item);
232: if (close != null) {
233: result = close.doubleValue();
234: }
235: return result;
236: }
237:
238: /**
239: * Returns the volume.
240: * <p>
241: * Note: throws <code>ClassCastException</code> if the series if not from a
242: * {@link OHLCDataset}.
243: *
244: * @param series the series (zero based index).
245: * @param item the item (zero based index).
246: *
247: * @return The volume.
248: */
249: public Number getVolume(int series, int item) {
250: return ((OHLCDataset) this .parent).getVolume(this .map[series],
251: item);
252: }
253:
254: /**
255: * Returns the volume-value (as a double primitive) for an item within a
256: * series.
257: *
258: * @param series the series (zero-based index).
259: * @param item the item (zero-based index).
260: *
261: * @return The volume-value.
262: */
263: public double getVolumeValue(int series, int item) {
264: double result = Double.NaN;
265: Number volume = getVolume(series, item);
266: if (volume != null) {
267: result = volume.doubleValue();
268: }
269: return result;
270: }
271:
272: ///////////////////////////////////////////////////////////////////////////
273: // From XYDataset
274: ///////////////////////////////////////////////////////////////////////////
275:
276: /**
277: * Returns the X-value for the specified series and item.
278: * <p>
279: * Note: throws <code>ClassCastException</code> if the series if not from a
280: * {@link XYDataset}.
281: *
282: * @param series the index of the series of interest (zero-based);
283: * @param item the index of the item of interest (zero-based).
284: *
285: * @return The X-value for the specified series and item.
286: */
287: public Number getX(int series, int item) {
288: return ((XYDataset) this .parent).getX(this .map[series], item);
289: }
290:
291: /**
292: * Returns the Y-value for the specified series and item.
293: * <p>
294: * Note: throws <code>ClassCastException</code> if the series if not from a
295: * {@link XYDataset}.
296: *
297: * @param series the index of the series of interest (zero-based).
298: * @param item the index of the item of interest (zero-based).
299: *
300: * @return The Y-value for the specified series and item.
301: */
302: public Number getY(int series, int item) {
303: return ((XYDataset) this .parent).getY(this .map[series], item);
304: }
305:
306: /**
307: * Returns the number of items in a series.
308: * <p>
309: * Note: throws <code>ClassCastException</code> if the series if not from a
310: * {@link XYDataset}.
311: *
312: * @param series the index of the series of interest (zero-based).
313: *
314: * @return The number of items in a series.
315: */
316: public int getItemCount(int series) {
317: return ((XYDataset) this .parent).getItemCount(this .map[series]);
318: }
319:
320: ///////////////////////////////////////////////////////////////////////////
321: // From SeriesDataset
322: ///////////////////////////////////////////////////////////////////////////
323:
324: /**
325: * Returns the number of series in the dataset.
326: *
327: * @return The number of series in the dataset.
328: */
329: public int getSeriesCount() {
330: return this .map.length;
331: }
332:
333: /**
334: * Returns the key for a series.
335: *
336: * @param series the series (zero-based index).
337: *
338: * @return The name of a series.
339: */
340: public Comparable getSeriesKey(int series) {
341: return this .parent.getSeriesKey(this .map[series]);
342: }
343:
344: ///////////////////////////////////////////////////////////////////////////
345: // From IntervalXYDataset
346: ///////////////////////////////////////////////////////////////////////////
347:
348: /**
349: * Returns the starting X value for the specified series and item.
350: *
351: * @param series the index of the series of interest (zero-based).
352: * @param item the index of the item of interest (zero-based).
353: *
354: * @return The starting X value for the specified series and item.
355: */
356: public Number getStartX(int series, int item) {
357: if (this .parent instanceof IntervalXYDataset) {
358: return ((IntervalXYDataset) this .parent).getStartX(
359: this .map[series], item);
360: } else {
361: return getX(series, item);
362: }
363: }
364:
365: /**
366: * Returns the ending X value for the specified series and item.
367: *
368: * @param series the index of the series of interest (zero-based).
369: * @param item the index of the item of interest (zero-based).
370: *
371: * @return The ending X value for the specified series and item.
372: */
373: public Number getEndX(int series, int item) {
374: if (this .parent instanceof IntervalXYDataset) {
375: return ((IntervalXYDataset) this .parent).getEndX(
376: this .map[series], item);
377: } else {
378: return getX(series, item);
379: }
380: }
381:
382: /**
383: * Returns the starting Y value for the specified series and item.
384: *
385: * @param series the index of the series of interest (zero-based).
386: * @param item the index of the item of interest (zero-based).
387: *
388: * @return The starting Y value for the specified series and item.
389: */
390: public Number getStartY(int series, int item) {
391: if (this .parent instanceof IntervalXYDataset) {
392: return ((IntervalXYDataset) this .parent).getStartY(
393: this .map[series], item);
394: } else {
395: return getY(series, item);
396: }
397: }
398:
399: /**
400: * Returns the ending Y value for the specified series and item.
401: *
402: * @param series the index of the series of interest (zero-based).
403: * @param item the index of the item of interest (zero-based).
404: *
405: * @return The ending Y value for the specified series and item.
406: */
407: public Number getEndY(int series, int item) {
408: if (this .parent instanceof IntervalXYDataset) {
409: return ((IntervalXYDataset) this .parent).getEndY(
410: this .map[series], item);
411: } else {
412: return getY(series, item);
413: }
414: }
415:
416: ///////////////////////////////////////////////////////////////////////////
417: // New methods from CombinationDataset
418: ///////////////////////////////////////////////////////////////////////////
419:
420: /**
421: * Returns the parent Dataset of this combination.
422: *
423: * @return The parent Dataset of this combination.
424: */
425: public SeriesDataset getParent() {
426: return this .parent;
427: }
428:
429: /**
430: * Returns a map or indirect indexing form our series into parent's series.
431: *
432: * @return A map or indirect indexing form our series into parent's series.
433: */
434: public int[] getMap() {
435: return (int[]) this.map.clone();
436: }
437:
438: }
|