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