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