Source for org.jfree.data.statistics.SimpleHistogramBin

   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:  * SimpleHistogramBin.java
  29:  * -----------------------
  30:  * (C) Copyright 2005-2007, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 10-Jan-2005 : Version 1 (DG);
  38:  *
  39:  */
  40: 
  41: package org.jfree.data.statistics;
  42: 
  43: import java.io.Serializable;
  44: 
  45: import org.jfree.util.PublicCloneable;
  46: 
  47: /**
  48:  * A bin for the {@link SimpleHistogramDataset}.
  49:  */
  50: public class SimpleHistogramBin implements Comparable, 
  51:                                            Cloneable, PublicCloneable, 
  52:                                            Serializable {
  53: 
  54:     /** For serialization. */
  55:     private static final long serialVersionUID = 3480862537505941742L;
  56:     
  57:     /** The lower bound for the bin. */
  58:     private double lowerBound;
  59:     
  60:     /** The upper bound for the bin. */
  61:     private double upperBound;
  62:     
  63:     /** 
  64:      * A flag that controls whether the lower bound is included in the bin 
  65:      * range. 
  66:      */
  67:     private boolean includeLowerBound;
  68:     
  69:     /** 
  70:      * A flag that controls whether the upper bound is included in the bin 
  71:      * range. 
  72:      */
  73:     private boolean includeUpperBound;
  74:     
  75:     /** The item count. */
  76:     private int itemCount;
  77:     
  78:     /**
  79:      * Creates a new bin.
  80:      * 
  81:      * @param lowerBound  the lower bound (inclusive).
  82:      * @param upperBound  the upper bound (inclusive);
  83:      */
  84:     public SimpleHistogramBin(double lowerBound, double upperBound) {
  85:         this(lowerBound, upperBound, true, true);
  86:     }
  87: 
  88:     /**
  89:      * Creates a new bin.
  90:      * 
  91:      * @param lowerBound  the lower bound.
  92:      * @param upperBound  the upper bound.
  93:      * @param includeLowerBound  include the lower bound?
  94:      * @param includeUpperBound  include the upper bound?
  95:      */
  96:     public SimpleHistogramBin(double lowerBound, double upperBound,
  97:                               boolean includeLowerBound, 
  98:                               boolean includeUpperBound) {
  99:         if (lowerBound >= upperBound) {
 100:             throw new IllegalArgumentException("Invalid bounds");
 101:         }
 102:         this.lowerBound = lowerBound;
 103:         this.upperBound = upperBound;
 104:         this.includeLowerBound = includeLowerBound;
 105:         this.includeUpperBound = includeUpperBound;
 106:         this.itemCount = 0;
 107:     }
 108:     
 109:     /**
 110:      * Returns the lower bound.
 111:      * 
 112:      * @return The lower bound.
 113:      */
 114:     public double getLowerBound() {
 115:         return this.lowerBound;
 116:     }
 117:     
 118:     /**
 119:      * Return the upper bound.
 120:      * 
 121:      * @return The upper bound.
 122:      */
 123:     public double getUpperBound() {
 124:         return this.upperBound;
 125:     }
 126:     
 127:     /**
 128:      * Returns the item count.
 129:      * 
 130:      * @return The item count.
 131:      */
 132:     public int getItemCount() {
 133:         return this.itemCount;
 134:     }
 135:    
 136:     /**
 137:      * Sets the item count.
 138:      * 
 139:      * @param count  the item count.
 140:      */
 141:     public void setItemCount(int count) {
 142:         this.itemCount = count;
 143:     }
 144: 
 145:     /**
 146:      * Returns <code>true</code> if the specified value belongs in the bin, 
 147:      * and <code>false</code> otherwise.
 148:      * 
 149:      * @param value  the value.
 150:      * 
 151:      * @return A boolean.
 152:      */
 153:     public boolean accepts(double value) {
 154:         if (Double.isNaN(value)) {
 155:             return false;
 156:         }
 157:         if (value < this.lowerBound) {
 158:             return false;
 159:         }
 160:         if (value > this.upperBound) {
 161:             return false;
 162:         }
 163:         if (value == this.lowerBound) {
 164:             return this.includeLowerBound;
 165:         }
 166:         if (value == this.upperBound) {
 167:             return this.includeUpperBound;
 168:         }
 169:         return true;
 170:     }
 171:     
 172:     /**
 173:      * Returns <code>true</code> if this bin overlaps with the specified bin,
 174:      * and <code>false</code> otherwise.
 175:      * 
 176:      * @param bin  the other bin (<code>null</code> not permitted).
 177:      * 
 178:      * @return A boolean.
 179:      */
 180:     public boolean overlapsWith(SimpleHistogramBin bin) {
 181:         if (this.upperBound < bin.lowerBound) {
 182:             return false;
 183:         }
 184:         if (this.lowerBound > bin.upperBound) {
 185:             return false;
 186:         }
 187:         if (this.upperBound == bin.lowerBound) {
 188:             return this.includeUpperBound && bin.includeLowerBound;
 189:         }
 190:         if (this.lowerBound == bin.upperBound) {
 191:             return this.includeLowerBound && bin.includeUpperBound;
 192:         }
 193:         return true;
 194:     }
 195:     
 196:     /**
 197:      * Compares the bin to an arbitrary object and returns the relative 
 198:      * ordering.
 199:      * 
 200:      * @param obj  the object.
 201:      * 
 202:      * @return An integer indicating the relative ordering of the this bin and 
 203:      *         the given object.
 204:      */
 205:     public int compareTo(Object obj) {
 206:         if (!(obj instanceof SimpleHistogramBin)) {
 207:             return 0;
 208:         }
 209:         SimpleHistogramBin bin = (SimpleHistogramBin) obj;
 210:         if (this.lowerBound < bin.lowerBound) {
 211:             return -1;
 212:         }
 213:         if (this.lowerBound > bin.lowerBound) {
 214:             return 1;
 215:         }
 216:         // lower bounds are the same
 217:         if (this.upperBound < bin.upperBound) {
 218:             return -1;
 219:         }
 220:         if (this.upperBound > bin.upperBound) {
 221:             return 1;
 222:         }
 223:         return 0;
 224:     }
 225:     
 226:     /**
 227:      * Tests this bin for equality with an arbitrary object.
 228:      * 
 229:      * @param obj  the object (<code>null</code> permitted).
 230:      * 
 231:      * @return A boolean.
 232:      */
 233:     public boolean equals(Object obj) {
 234:         if (!(obj instanceof SimpleHistogramBin)) {
 235:             return false;
 236:         }
 237:         SimpleHistogramBin that = (SimpleHistogramBin) obj;
 238:         if (this.lowerBound != that.lowerBound) {
 239:             return false;
 240:         }
 241:         if (this.upperBound != that.upperBound) {
 242:             return false;
 243:         }
 244:         if (this.includeLowerBound != that.includeLowerBound) {
 245:             return false;
 246:         }
 247:         if (this.includeUpperBound != that.includeUpperBound) {
 248:             return false;
 249:         }
 250:         if (this.itemCount != that.itemCount) {
 251:             return false;
 252:         }
 253:         return true;
 254:     }
 255:     
 256:     /**
 257:      * Returns a clone of the bin.
 258:      * 
 259:      * @return A clone.
 260:      * 
 261:      * @throws CloneNotSupportedException not thrown by this class.
 262:      */
 263:     public Object clone() throws CloneNotSupportedException {
 264:         return super.clone();   
 265:     }
 266:     
 267: }