001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, 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: * DefaultOHLCDataset.java
029: * -----------------------
030: * (C) Copyright 2003, 2004, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DefaultOHLCDataset.java,v 1.5.2.1 2005/10/25 21:36:51 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 03-Dec-2003 : Version 1 (DG);
040: * 05-May-2004 : Now extends AbstractXYDataset (DG);
041: * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
042: * getYValue() (DG);
043: * 29-Apr-2005 : Added equals() method (DG);
044: *
045: */
046:
047: package org.jfree.data.xy;
048:
049: import java.util.Arrays;
050: import java.util.Date;
051:
052: /**
053: * A simple implementation of the {@link OHLCDataset} interface. This
054: * implementation supports only one series.
055: */
056: public class DefaultOHLCDataset extends AbstractXYDataset implements
057: OHLCDataset {
058:
059: /** The series key. */
060: private Comparable key;
061:
062: /** Storage for the data items. */
063: private OHLCDataItem[] data;
064:
065: /**
066: * Creates a new dataset.
067: *
068: * @param key the series key.
069: * @param data the data items.
070: */
071: public DefaultOHLCDataset(Comparable key, OHLCDataItem[] data) {
072: this .key = key;
073: this .data = data;
074: }
075:
076: /**
077: * Returns the series key.
078: *
079: * @param series the series index (ignored).
080: *
081: * @return The series key.
082: */
083: public Comparable getSeriesKey(int series) {
084: return this .key;
085: }
086:
087: /**
088: * Returns the x-value for a data item.
089: *
090: * @param series the series index (ignored).
091: * @param item the item index (zero-based).
092: *
093: * @return The x-value.
094: */
095: public Number getX(int series, int item) {
096: return new Long(this .data[item].getDate().getTime());
097: }
098:
099: /**
100: * Returns the x-value for a data item as a date.
101: *
102: * @param series the series index (ignored).
103: * @param item the item index (zero-based).
104: *
105: * @return The x-value as a date.
106: */
107: public Date getXDate(int series, int item) {
108: return this .data[item].getDate();
109: }
110:
111: /**
112: * Returns the y-value.
113: *
114: * @param series the series index (ignored).
115: * @param item the item index (zero-based).
116: *
117: * @return The y value.
118: */
119: public Number getY(int series, int item) {
120: return getClose(series, item);
121: }
122:
123: /**
124: * Returns the high value.
125: *
126: * @param series the series index (ignored).
127: * @param item the item index (zero-based).
128: *
129: * @return The high value.
130: */
131: public Number getHigh(int series, int item) {
132: return this .data[item].getHigh();
133: }
134:
135: /**
136: * Returns the high-value (as a double primitive) for an item within a
137: * series.
138: *
139: * @param series the series (zero-based index).
140: * @param item the item (zero-based index).
141: *
142: * @return The high-value.
143: */
144: public double getHighValue(int series, int item) {
145: double result = Double.NaN;
146: Number high = getHigh(series, item);
147: if (high != null) {
148: result = high.doubleValue();
149: }
150: return result;
151: }
152:
153: /**
154: * Returns the low value.
155: *
156: * @param series the series index (ignored).
157: * @param item the item index (zero-based).
158: *
159: * @return The low value.
160: */
161: public Number getLow(int series, int item) {
162: return this .data[item].getLow();
163: }
164:
165: /**
166: * Returns the low-value (as a double primitive) for an item within a
167: * series.
168: *
169: * @param series the series (zero-based index).
170: * @param item the item (zero-based index).
171: *
172: * @return The low-value.
173: */
174: public double getLowValue(int series, int item) {
175: double result = Double.NaN;
176: Number low = getLow(series, item);
177: if (low != null) {
178: result = low.doubleValue();
179: }
180: return result;
181: }
182:
183: /**
184: * Returns the open value.
185: *
186: * @param series the series index (ignored).
187: * @param item the item index (zero-based).
188: *
189: * @return The open value.
190: */
191: public Number getOpen(int series, int item) {
192: return this .data[item].getOpen();
193: }
194:
195: /**
196: * Returns the open-value (as a double primitive) for an item within a
197: * series.
198: *
199: * @param series the series (zero-based index).
200: * @param item the item (zero-based index).
201: *
202: * @return The open-value.
203: */
204: public double getOpenValue(int series, int item) {
205: double result = Double.NaN;
206: Number open = getOpen(series, item);
207: if (open != null) {
208: result = open.doubleValue();
209: }
210: return result;
211: }
212:
213: /**
214: * Returns the close value.
215: *
216: * @param series the series index (ignored).
217: * @param item the item index (zero-based).
218: *
219: * @return The close value.
220: */
221: public Number getClose(int series, int item) {
222: return this .data[item].getClose();
223: }
224:
225: /**
226: * Returns the close-value (as a double primitive) for an item within a
227: * series.
228: *
229: * @param series the series (zero-based index).
230: * @param item the item (zero-based index).
231: *
232: * @return The close-value.
233: */
234: public double getCloseValue(int series, int item) {
235: double result = Double.NaN;
236: Number close = getClose(series, item);
237: if (close != null) {
238: result = close.doubleValue();
239: }
240: return result;
241: }
242:
243: /**
244: * Returns the trading volume.
245: *
246: * @param series the series index (ignored).
247: * @param item the item index (zero-based).
248: *
249: * @return The trading volume.
250: */
251: public Number getVolume(int series, int item) {
252: return this .data[item].getVolume();
253: }
254:
255: /**
256: * Returns the volume-value (as a double primitive) for an item within a
257: * series.
258: *
259: * @param series the series (zero-based index).
260: * @param item the item (zero-based index).
261: *
262: * @return The volume-value.
263: */
264: public double getVolumeValue(int series, int item) {
265: double result = Double.NaN;
266: Number volume = getVolume(series, item);
267: if (volume != null) {
268: result = volume.doubleValue();
269: }
270: return result;
271: }
272:
273: /**
274: * Returns the series count.
275: *
276: * @return 1.
277: */
278: public int getSeriesCount() {
279: return 1;
280: }
281:
282: /**
283: * Returns the item count for the specified series.
284: *
285: * @param series the series index (ignored).
286: *
287: * @return The item count.
288: */
289: public int getItemCount(int series) {
290: return this .data.length;
291: }
292:
293: /**
294: * Sorts the data into ascending order by date.
295: */
296: public void sortDataByDate() {
297: Arrays.sort(this .data);
298: }
299:
300: /**
301: * Tests this instance for equality with an arbitrary object.
302: *
303: * @param obj the object (<code>null</code> permitted).
304: *
305: * @return A boolean.
306: */
307: public boolean equals(Object obj) {
308: if (this == obj) {
309: return true;
310: }
311: if (!(obj instanceof DefaultOHLCDataset)) {
312: return false;
313: }
314: DefaultOHLCDataset that = (DefaultOHLCDataset) obj;
315: if (!this .key.equals(that.key)) {
316: return false;
317: }
318: if (!Arrays.equals(this .data, that.data)) {
319: return false;
320: }
321: return true;
322: }
323:
324: }
|