Source for org.jfree.data.xy.DefaultOHLCDataset

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2008, 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-2008, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 03-Dec-2003 : Version 1 (DG);
  38:  * 05-May-2004 : Now extends AbstractXYDataset (DG);
  39:  * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
  40:  *               getYValue() (DG);
  41:  * 29-Apr-2005 : Added equals() method (DG);
  42:  * 22-Apr-2008 : Implemented PublicCloneable, and fixed cloning bug (DG);
  43:  *
  44:  */
  45: 
  46: package org.jfree.data.xy;
  47: 
  48: import java.util.Arrays;
  49: import java.util.Date;
  50: 
  51: import org.jfree.util.PublicCloneable;
  52: 
  53: /**
  54:  * A simple implementation of the {@link OHLCDataset} interface.  This
  55:  * implementation supports only one series.
  56:  */
  57: public class DefaultOHLCDataset extends AbstractXYDataset
  58:         implements OHLCDataset, PublicCloneable {
  59: 
  60:     /** The series key. */
  61:     private Comparable key;
  62: 
  63:     /** Storage for the data items. */
  64:     private OHLCDataItem[] data;
  65: 
  66:     /**
  67:      * Creates a new dataset.
  68:      *
  69:      * @param key  the series key.
  70:      * @param data  the data items.
  71:      */
  72:     public DefaultOHLCDataset(Comparable key, OHLCDataItem[] data) {
  73:         this.key = key;
  74:         this.data = data;
  75:     }
  76: 
  77:     /**
  78:      * Returns the series key.
  79:      *
  80:      * @param series  the series index (ignored).
  81:      *
  82:      * @return The series key.
  83:      */
  84:     public Comparable getSeriesKey(int series) {
  85:         return this.key;
  86:     }
  87: 
  88:     /**
  89:      * Returns the x-value for a data item.
  90:      *
  91:      * @param series  the series index (ignored).
  92:      * @param item  the item index (zero-based).
  93:      *
  94:      * @return The x-value.
  95:      */
  96:     public Number getX(int series, int item) {
  97:         return new Long(this.data[item].getDate().getTime());
  98:     }
  99: 
 100:     /**
 101:      * Returns the x-value for a data item as a date.
 102:      *
 103:      * @param series  the series index (ignored).
 104:      * @param item  the item index (zero-based).
 105:      *
 106:      * @return The x-value as a date.
 107:      */
 108:     public Date getXDate(int series, int item) {
 109:         return this.data[item].getDate();
 110:     }
 111: 
 112:     /**
 113:      * Returns the y-value.
 114:      *
 115:      * @param series  the series index (ignored).
 116:      * @param item  the item index (zero-based).
 117:      *
 118:      * @return The y value.
 119:      */
 120:     public Number getY(int series, int item) {
 121:         return getClose(series, item);
 122:     }
 123: 
 124:     /**
 125:      * Returns the high value.
 126:      *
 127:      * @param series  the series index (ignored).
 128:      * @param item  the item index (zero-based).
 129:      *
 130:      * @return The high value.
 131:      */
 132:     public Number getHigh(int series, int item) {
 133:         return this.data[item].getHigh();
 134:     }
 135: 
 136:     /**
 137:      * Returns the high-value (as a double primitive) for an item within a
 138:      * series.
 139:      *
 140:      * @param series  the series (zero-based index).
 141:      * @param item  the item (zero-based index).
 142:      *
 143:      * @return The high-value.
 144:      */
 145:     public double getHighValue(int series, int item) {
 146:         double result = Double.NaN;
 147:         Number high = getHigh(series, item);
 148:         if (high != null) {
 149:             result = high.doubleValue();
 150:         }
 151:         return result;
 152:     }
 153: 
 154:     /**
 155:      * Returns the low value.
 156:      *
 157:      * @param series  the series index (ignored).
 158:      * @param item  the item index (zero-based).
 159:      *
 160:      * @return The low value.
 161:      */
 162:     public Number getLow(int series, int item) {
 163:         return this.data[item].getLow();
 164:     }
 165: 
 166:     /**
 167:      * Returns the low-value (as a double primitive) for an item within a
 168:      * series.
 169:      *
 170:      * @param series  the series (zero-based index).
 171:      * @param item  the item (zero-based index).
 172:      *
 173:      * @return The low-value.
 174:      */
 175:     public double getLowValue(int series, int item) {
 176:         double result = Double.NaN;
 177:         Number low = getLow(series, item);
 178:         if (low != null) {
 179:             result = low.doubleValue();
 180:         }
 181:         return result;
 182:     }
 183: 
 184:     /**
 185:      * Returns the open value.
 186:      *
 187:      * @param series  the series index (ignored).
 188:      * @param item  the item index (zero-based).
 189:      *
 190:      * @return The open value.
 191:      */
 192:     public Number getOpen(int series, int item) {
 193:         return this.data[item].getOpen();
 194:     }
 195: 
 196:     /**
 197:      * Returns the open-value (as a double primitive) for an item within a
 198:      * series.
 199:      *
 200:      * @param series  the series (zero-based index).
 201:      * @param item  the item (zero-based index).
 202:      *
 203:      * @return The open-value.
 204:      */
 205:     public double getOpenValue(int series, int item) {
 206:         double result = Double.NaN;
 207:         Number open = getOpen(series, item);
 208:         if (open != null) {
 209:             result = open.doubleValue();
 210:         }
 211:         return result;
 212:     }
 213: 
 214:     /**
 215:      * Returns the close value.
 216:      *
 217:      * @param series  the series index (ignored).
 218:      * @param item  the item index (zero-based).
 219:      *
 220:      * @return The close value.
 221:      */
 222:     public Number getClose(int series, int item) {
 223:         return this.data[item].getClose();
 224:     }
 225: 
 226:     /**
 227:      * Returns the close-value (as a double primitive) for an item within a
 228:      * series.
 229:      *
 230:      * @param series  the series (zero-based index).
 231:      * @param item  the item (zero-based index).
 232:      *
 233:      * @return The close-value.
 234:      */
 235:     public double getCloseValue(int series, int item) {
 236:         double result = Double.NaN;
 237:         Number close = getClose(series, item);
 238:         if (close != null) {
 239:             result = close.doubleValue();
 240:         }
 241:         return result;
 242:     }
 243: 
 244:     /**
 245:      * Returns the trading volume.
 246:      *
 247:      * @param series  the series index (ignored).
 248:      * @param item  the item index (zero-based).
 249:      *
 250:      * @return The trading volume.
 251:      */
 252:     public Number getVolume(int series, int item) {
 253:         return this.data[item].getVolume();
 254:     }
 255: 
 256:     /**
 257:      * Returns the volume-value (as a double primitive) for an item within a
 258:      * series.
 259:      *
 260:      * @param series  the series (zero-based index).
 261:      * @param item  the item (zero-based index).
 262:      *
 263:      * @return The volume-value.
 264:      */
 265:     public double getVolumeValue(int series, int item) {
 266:         double result = Double.NaN;
 267:         Number volume = getVolume(series, item);
 268:         if (volume != null) {
 269:             result = volume.doubleValue();
 270:         }
 271:         return result;
 272:     }
 273: 
 274:     /**
 275:      * Returns the series count.
 276:      *
 277:      * @return 1.
 278:      */
 279:     public int getSeriesCount() {
 280:         return 1;
 281:     }
 282: 
 283:     /**
 284:      * Returns the item count for the specified series.
 285:      *
 286:      * @param series  the series index (ignored).
 287:      *
 288:      * @return The item count.
 289:      */
 290:     public int getItemCount(int series) {
 291:         return this.data.length;
 292:     }
 293: 
 294:     /**
 295:      * Sorts the data into ascending order by date.
 296:      */
 297:     public void sortDataByDate() {
 298:         Arrays.sort(this.data);
 299:     }
 300: 
 301:     /**
 302:      * Tests this instance for equality with an arbitrary object.
 303:      *
 304:      * @param obj  the object (<code>null</code> permitted).
 305:      *
 306:      * @return A boolean.
 307:      */
 308:     public boolean equals(Object obj) {
 309:         if (this == obj) {
 310:             return true;
 311:         }
 312:         if (!(obj instanceof DefaultOHLCDataset)) {
 313:             return false;
 314:         }
 315:         DefaultOHLCDataset that = (DefaultOHLCDataset) obj;
 316:         if (!this.key.equals(that.key)) {
 317:             return false;
 318:         }
 319:         if (!Arrays.equals(this.data, that.data)) {
 320:             return false;
 321:         }
 322:         return true;
 323:     }
 324: 
 325:     /**
 326:      * Returns an independent copy of this dataset.
 327:      *
 328:      * @return A clone.
 329:      */
 330:     public Object clone() throws CloneNotSupportedException {
 331:         DefaultOHLCDataset clone = (DefaultOHLCDataset) super.clone();
 332:         clone.data = new OHLCDataItem[this.data.length];
 333:         System.arraycopy(this.data, 0, clone.data, 0, this.data.length);
 334:         return clone;
 335:     }
 336: 
 337: }