Source for org.jfree.data.xy.OHLCDataItem

   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:  * OHLCDataItem.java
  29:  * -----------------
  30:  * (C) Copyright 2003-2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: OHLCDataItem.java,v 1.6.2.1 2005/10/25 21:36:51 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 03-Dec-2003 : Version 1 (DG);
  40:  * 29-Apr-2005 : Added equals() method (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.data.xy;
  45: 
  46: import java.io.Serializable;
  47: import java.util.Date;
  48: 
  49: /**
  50:  * Represents a single (open-high-low-close) data item in 
  51:  * an {@link DefaultOHLCDataset}.  This data item is commonly used 
  52:  * to summarise the trading activity of a financial commodity for 
  53:  * a fixed period (most often one day).
  54:  */
  55: public class OHLCDataItem implements Comparable, Serializable {
  56:     
  57:     /** For serialization. */
  58:     private static final long serialVersionUID = 7753817154401169901L;
  59:     
  60:     /** The date. */
  61:     private Date date;
  62:     
  63:     /** The open value. */
  64:     private Number open;
  65: 
  66:     /** The high value. */
  67:     private Number high;
  68:     
  69:     /** The low value. */
  70:     private Number low;
  71:     
  72:     /** The close value. */
  73:     private Number close;
  74:     
  75:     /** The trading volume (number of shares, contracts or whatever). */
  76:     private Number volume;
  77: 
  78:     /**
  79:      * Creates a new item.
  80:      * 
  81:      * @param date  the date (<code>null</code> not permitted).
  82:      * @param open  the open value.
  83:      * @param high  the high value.
  84:      * @param low  the low value.
  85:      * @param close  the close value.
  86:      * @param volume  the volume.
  87:      */
  88:     public OHLCDataItem(Date date, 
  89:                         double open, 
  90:                         double high, 
  91:                         double low, 
  92:                         double close,
  93:                         double volume) {
  94:         if (date == null) {
  95:             throw new IllegalArgumentException("Null 'date' argument.");
  96:         }
  97:         this.date = date;
  98:         this.open = new Double(open);
  99:         this.high = new Double(high);
 100:         this.low = new Double(low);
 101:         this.close = new Double(close);
 102:         this.volume = new Double(volume);
 103:     }
 104:     
 105:     /**
 106:      * Returns the date that the data item relates to.
 107:      * 
 108:      * @return The date (never <code>null</code>).
 109:      */
 110:     public Date getDate() {
 111:         return this.date;
 112:     }
 113:  
 114:     /**
 115:      * Returns the open value.
 116:      * 
 117:      * @return The open value.
 118:      */
 119:     public Number getOpen() {
 120:         return this.open;
 121:     }
 122:     
 123:     /**
 124:      * Returns the high value.
 125:      * 
 126:      * @return The high value.
 127:      */
 128:     public Number getHigh() {
 129:         return this.high;
 130:     }
 131:     
 132:     /**
 133:      * Returns the low value.
 134:      * 
 135:      * @return The low value.
 136:      */
 137:     public Number getLow() {
 138:         return this.low;
 139:     }
 140:     
 141:     /**
 142:      * Returns the close value.
 143:      * 
 144:      * @return The close value.
 145:      */
 146:     public Number getClose() {
 147:         return this.close;
 148:     }
 149:     
 150:     /**
 151:      * Returns the volume.
 152:      * 
 153:      * @return The volume.
 154:      */
 155:     public Number getVolume() {
 156:         return this.volume;
 157:     }
 158:     
 159:     /**
 160:      * Checks this instance for equality with an arbitrary object.
 161:      * 
 162:      * @param obj  the object (<code>null</code> permitted).
 163:      * 
 164:      * @return A boolean.
 165:      */
 166:     public boolean equals(Object obj) {
 167:         if (obj == this) {
 168:             return true;   
 169:         }
 170:         if (!(obj instanceof OHLCDataItem)) {
 171:             return false;
 172:         }
 173:         OHLCDataItem that = (OHLCDataItem) obj;
 174:         if (!this.date.equals(that.date)) {
 175:             return false;   
 176:         }
 177:         if (!this.high.equals(that.high)) {
 178:             return false;
 179:         }
 180:         if (!this.low.equals(that.low)) {
 181:             return false;   
 182:         }
 183:         if (!this.open.equals(that.open)) {
 184:             return false;   
 185:         }
 186:         if (!this.close.equals(that.close)) {
 187:             return false;   
 188:         }
 189:         return true;
 190:     }
 191:     
 192:     /**
 193:      * Compares this object with the specified object for order. Returns a 
 194:      * negative integer, zero, or a positive integer as this object is less 
 195:      * than, equal to, or greater than the specified object.
 196:      * 
 197:      * @param object  the object to compare to.
 198:      * 
 199:      * @return A negative integer, zero, or a positive integer as this object 
 200:      *         is less than, equal to, or greater than the specified object.
 201:      */
 202:     public int compareTo(Object object) {
 203:         if (object instanceof OHLCDataItem) {
 204:             OHLCDataItem item = (OHLCDataItem) object;
 205:             return this.date.compareTo(item.date);
 206:         }
 207:         else {
 208:             throw new ClassCastException("OHLCDataItem.compareTo().");
 209:         }
 210:     }
 211:     
 212: }