Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jfreechart/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ----------------------- 28: * DefaultOHLCDataset.java 29: * ----------------------- 30: * (C) Copyright 2003, 2004, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * $Id: DefaultOHLCDataset.java,v 1.5.2.1 2005/10/25 21:36:51 mungady Exp $ 36: * 37: * Changes 38: * ------- 39: * 03-Dec-2003 : Version 1 (DG); 40: * 05-May-2004 : Now extends AbstractXYDataset (DG); 41: * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 42: * getYValue() (DG); 43: * 29-Apr-2005 : Added equals() method (DG); 44: * 45: */ 46: 47: package org.jfree.data.xy; 48: 49: import java.util.Arrays; 50: import java.util.Date; 51: 52: /** 53: * A simple implementation of the {@link OHLCDataset} interface. This 54: * implementation supports only one series. 55: */ 56: public class DefaultOHLCDataset extends AbstractXYDataset 57: implements OHLCDataset { 58: 59: /** The series key. */ 60: private Comparable key; 61: 62: /** Storage for the data items. */ 63: private OHLCDataItem[] data; 64: 65: /** 66: * Creates a new dataset. 67: * 68: * @param key the series key. 69: * @param data the data items. 70: */ 71: public DefaultOHLCDataset(Comparable key, OHLCDataItem[] data) { 72: this.key = key; 73: this.data = data; 74: } 75: 76: /** 77: * Returns the series key. 78: * 79: * @param series the series index (ignored). 80: * 81: * @return The series key. 82: */ 83: public Comparable getSeriesKey(int series) { 84: return this.key; 85: } 86: 87: /** 88: * Returns the x-value for a data item. 89: * 90: * @param series the series index (ignored). 91: * @param item the item index (zero-based). 92: * 93: * @return The x-value. 94: */ 95: public Number getX(int series, int item) { 96: return new Long(this.data[item].getDate().getTime()); 97: } 98: 99: /** 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: }