Source for org.jfree.data.statistics.DefaultStatisticalCategoryDataset

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2006, 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:  * DefaultStatisticalCategoryDataset.java
  29:  * --------------------------------------
  30:  * (C) Copyright 2002-2006, by Pascal Collet and Contributors.
  31:  *
  32:  * Original Author:  Pascal Collet;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: DefaultStatisticalCategoryDataset.java,v 1.8.2.3 2006/08/03 15:36:20 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 21-Aug-2002 : Version 1, contributed by Pascal Collet (DG);
  40:  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41:  * 05-Feb-2003 : Revised implementation to use KeyedObjects2D (DG);
  42:  * 28-Aug-2003 : Moved from org.jfree.data --> org.jfree.data.statistics (DG);
  43:  * 06-Oct-2003 : Removed incorrect Javadoc text (DG);
  44:  * 18-Nov-2004 : Updated for changes in RangeInfo interface (DG);
  45:  * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0 
  46:  *               release (DG);
  47:  * 01-Feb-2005 : Changed minimumRangeValue and maximumRangeValue from Double
  48:  *               to double (DG);
  49:  * 05-Feb-2005 : Implemented equals() method (DG);
  50:  * ------------- JFREECHART 1.0.0 ---------------------------------------------
  51:  * 08-Aug-2006 : Reworked implementation of RangeInfo methods (DG);
  52:  * 
  53:  */
  54: 
  55: package org.jfree.data.statistics;
  56: 
  57: import java.util.List;
  58: 
  59: import org.jfree.data.KeyedObjects2D;
  60: import org.jfree.data.Range;
  61: import org.jfree.data.RangeInfo;
  62: import org.jfree.data.general.AbstractDataset;
  63: 
  64: /**
  65:  * A convenience class that provides a default implementation of the
  66:  * {@link StatisticalCategoryDataset} interface.
  67:  *
  68:  * @author Pascal Collet
  69:  */
  70: public class DefaultStatisticalCategoryDataset extends AbstractDataset
  71:     implements StatisticalCategoryDataset, RangeInfo {
  72: 
  73:     /** Storage for the data. */
  74:     private KeyedObjects2D data;
  75: 
  76:     /** The minimum range value. */
  77:     private double minimumRangeValue;
  78: 
  79:     /** The minimum range value including the standard deviation. */
  80:     private double minimumRangeValueIncStdDev;
  81:     
  82:     /** The maximum range value. */
  83:     private double maximumRangeValue;
  84: 
  85:     /** The maximum range value including the standard deviation. */
  86:     private double maximumRangeValueIncStdDev;
  87: 
  88:     /**
  89:      * Creates a new dataset.
  90:      */
  91:     public DefaultStatisticalCategoryDataset() {
  92:         this.data = new KeyedObjects2D();
  93:         this.minimumRangeValue = Double.NaN;
  94:         this.maximumRangeValue = Double.NaN;
  95:         this.minimumRangeValueIncStdDev = Double.NaN;
  96:         this.maximumRangeValueIncStdDev = Double.NaN;
  97:     }
  98: 
  99:     /**
 100:      * Returns the mean value for an item.
 101:      *
 102:      * @param row  the row index (zero-based).
 103:      * @param column  the column index (zero-based).
 104:      *
 105:      * @return The mean value.
 106:      */
 107:     public Number getMeanValue(int row, int column) {
 108:         Number result = null;
 109:         MeanAndStandardDeviation masd 
 110:             = (MeanAndStandardDeviation) this.data.getObject(row, column);
 111:         if (masd != null) {
 112:             result = masd.getMean();
 113:         }
 114:         return result;
 115:     }
 116: 
 117:     /**
 118:      * Returns the value for an item (for this dataset, the mean value is
 119:      * returned).
 120:      *
 121:      * @param row  the row index.
 122:      * @param column  the column index.
 123:      *
 124:      * @return The value.
 125:      */
 126:     public Number getValue(int row, int column) {
 127:         return getMeanValue(row, column);
 128:     }
 129: 
 130:     /**
 131:      * Returns the value for an item (for this dataset, the mean value is
 132:      * returned).
 133:      *
 134:      * @param rowKey  the row key.
 135:      * @param columnKey  the columnKey.
 136:      *
 137:      * @return The value.
 138:      */
 139:     public Number getValue(Comparable rowKey, Comparable columnKey) {
 140:         return getMeanValue(rowKey, columnKey);
 141:     }
 142: 
 143:     /**
 144:      * Returns the mean value for an item.
 145:      *
 146:      * @param rowKey  the row key.
 147:      * @param columnKey  the columnKey.
 148:      *
 149:      * @return The mean value.
 150:      */
 151:     public Number getMeanValue(Comparable rowKey, Comparable columnKey) {
 152:         Number result = null;
 153:         MeanAndStandardDeviation masd
 154:             = (MeanAndStandardDeviation) this.data.getObject(rowKey, columnKey);
 155:         if (masd != null) {
 156:             result = masd.getMean();
 157:         }
 158:         return result;
 159:     }
 160: 
 161:     /**
 162:      * Returns the standard deviation value for an item.
 163:      *
 164:      * @param row  the row index (zero-based).
 165:      * @param column  the column index (zero-based).
 166:      *
 167:      * @return The standard deviation.
 168:      */
 169:     public Number getStdDevValue(int row, int column) {
 170:         Number result = null;
 171:         MeanAndStandardDeviation masd 
 172:             = (MeanAndStandardDeviation) this.data.getObject(row, column);
 173:         if (masd != null) {
 174:             result = masd.getStandardDeviation();
 175:         }
 176:         return result;
 177:     }
 178: 
 179:     /**
 180:      * Returns the standard deviation value for an item.
 181:      *
 182:      * @param rowKey  the row key.
 183:      * @param columnKey  the columnKey.
 184:      *
 185:      * @return The standard deviation.
 186:      */
 187:     public Number getStdDevValue(Comparable rowKey, Comparable columnKey) {
 188:         Number result = null;
 189:         MeanAndStandardDeviation masd
 190:             = (MeanAndStandardDeviation) this.data.getObject(rowKey, columnKey);
 191:         if (masd != null) {
 192:             result = masd.getStandardDeviation();
 193:         }
 194:         return result;
 195:     }
 196: 
 197:     /**
 198:      * Returns the column index for a given key.
 199:      *
 200:      * @param key  the column key.
 201:      *
 202:      * @return The column index.
 203:      */
 204:     public int getColumnIndex(Comparable key) {
 205:         return this.data.getColumnIndex(key);
 206:     }
 207: 
 208:     /**
 209:      * Returns a column key.
 210:      *
 211:      * @param column  the column index (zero-based).
 212:      *
 213:      * @return The column key.
 214:      */
 215:     public Comparable getColumnKey(int column) {
 216:         return this.data.getColumnKey(column);
 217:     }
 218: 
 219:     /**
 220:      * Returns the column keys.
 221:      *
 222:      * @return The keys.
 223:      */
 224:     public List getColumnKeys() {
 225:         return this.data.getColumnKeys();
 226:     }
 227: 
 228:     /**
 229:      * Returns the row index for a given key.
 230:      *
 231:      * @param key  the row key.
 232:      *
 233:      * @return The row index.
 234:      */
 235:     public int getRowIndex(Comparable key) {
 236:         return this.data.getRowIndex(key);
 237:     }
 238: 
 239:     /**
 240:      * Returns a row key.
 241:      *
 242:      * @param row  the row index (zero-based).
 243:      *
 244:      * @return The row key.
 245:      */
 246:     public Comparable getRowKey(int row) {
 247:         return this.data.getRowKey(row);
 248:     }
 249: 
 250:     /**
 251:      * Returns the row keys.
 252:      *
 253:      * @return The keys.
 254:      */
 255:     public List getRowKeys() {
 256:         return this.data.getRowKeys();
 257:     }
 258: 
 259:     /**
 260:      * Returns the number of rows in the table.
 261:      *
 262:      * @return The row count.
 263:      */
 264:     public int getRowCount() {
 265:         return this.data.getRowCount();
 266:     }
 267: 
 268:     /**
 269:      * Returns the number of columns in the table.
 270:      *
 271:      * @return The column count.
 272:      */
 273:     public int getColumnCount() {
 274:         return this.data.getColumnCount();
 275:     }
 276: 
 277:     /**
 278:      * Adds a mean and standard deviation to the table.
 279:      *
 280:      * @param mean  the mean.
 281:      * @param standardDeviation  the standard deviation.
 282:      * @param rowKey  the row key.
 283:      * @param columnKey  the column key.
 284:      */
 285:     public void add(double mean, double standardDeviation,
 286:                     Comparable rowKey, Comparable columnKey) {
 287:         add(new Double(mean), new Double(standardDeviation), rowKey, columnKey);
 288:     }
 289: 
 290:     /**
 291:      * Adds a mean and standard deviation to the table.
 292:      *
 293:      * @param mean  the mean.
 294:      * @param standardDeviation  the standard deviation.
 295:      * @param rowKey  the row key.
 296:      * @param columnKey  the column key.
 297:      */
 298:     public void add(Number mean, Number standardDeviation,
 299:                     Comparable rowKey, Comparable columnKey) {
 300:         MeanAndStandardDeviation item = new MeanAndStandardDeviation(
 301:                 mean, standardDeviation);
 302:         this.data.addObject(item, rowKey, columnKey);
 303:         double m = 0.0;
 304:         double sd = 0.0;
 305:         if (mean != null) {
 306:             m = mean.doubleValue();
 307:         }
 308:         if (standardDeviation != null) {
 309:             sd = standardDeviation.doubleValue();   
 310:         }
 311:         
 312:         if (!Double.isNaN(m)) {
 313:             if (Double.isNaN(this.maximumRangeValue) 
 314:                     || m > this.maximumRangeValue) {
 315:                 this.maximumRangeValue = m;
 316:             }
 317:         }
 318:         
 319:         if (!Double.isNaN(m + sd)) {
 320:             if (Double.isNaN(this.maximumRangeValueIncStdDev) 
 321:                     || (m + sd) > this.maximumRangeValueIncStdDev) {
 322:                 this.maximumRangeValueIncStdDev = m + sd;
 323:             }
 324:         }
 325: 
 326:         if (!Double.isNaN(m)) {
 327:             if (Double.isNaN(this.minimumRangeValue) 
 328:                     || m < this.minimumRangeValue) {
 329:                 this.minimumRangeValue = m;
 330:             }
 331:         }
 332: 
 333:         if (!Double.isNaN(m - sd)) {
 334:             if (Double.isNaN(this.minimumRangeValueIncStdDev) 
 335:                     || (m - sd) < this.minimumRangeValueIncStdDev) {
 336:                 this.minimumRangeValueIncStdDev = m - sd;
 337:             }
 338:         }
 339: 
 340:         fireDatasetChanged();
 341:     }
 342: 
 343:     /**
 344:      * Returns the minimum y-value in the dataset.
 345:      *
 346:      * @param includeInterval  a flag that determines whether or not the
 347:      *                         y-interval is taken into account (ignored for
 348:      *                         this dataset).
 349:      * 
 350:      * @return The minimum value.
 351:      */
 352:     public double getRangeLowerBound(boolean includeInterval) {
 353:         return this.minimumRangeValue;        
 354:     }
 355: 
 356:     /**
 357:      * Returns the maximum y-value in the dataset.
 358:      *
 359:      * @param includeInterval  a flag that determines whether or not the
 360:      *                         y-interval is taken into account (ignored for
 361:      *                         this dataset).
 362:      * 
 363:      * @return The maximum value.
 364:      */
 365:     public double getRangeUpperBound(boolean includeInterval) {
 366:         return this.maximumRangeValue;        
 367:     }
 368: 
 369:     /**
 370:      * Returns the range of the values in this dataset's range.
 371:      *
 372:      * @param includeInterval  a flag that determines whether or not the
 373:      *                         y-interval is taken into account.
 374:      * 
 375:      * @return The range.
 376:      */
 377:     public Range getRangeBounds(boolean includeInterval) {
 378:         Range result = null;
 379:         if (includeInterval) {
 380:             if (!Double.isNaN(this.minimumRangeValueIncStdDev) 
 381:                     && !Double.isNaN(this.maximumRangeValueIncStdDev))
 382:             result = new Range(this.minimumRangeValueIncStdDev, 
 383:                     this.maximumRangeValueIncStdDev);
 384:         }
 385:         else {
 386:             if (!Double.isNaN(this.minimumRangeValue) 
 387:                     && !Double.isNaN(this.maximumRangeValue))
 388:             result = new Range(this.minimumRangeValue, this.maximumRangeValue);            
 389:         }
 390:         return result;
 391:     }
 392: 
 393:     /**
 394:      * Tests this instance for equality with an arbitrary object.
 395:      * 
 396:      * @param obj  the object (<code>null</code> permitted).
 397:      * 
 398:      * @return A boolean.
 399:      */
 400:     public boolean equals(Object obj) {
 401:         if (obj == this) {
 402:             return true;   
 403:         }
 404:         if (!(obj instanceof DefaultStatisticalCategoryDataset)) {
 405:             return false;   
 406:         }
 407:         DefaultStatisticalCategoryDataset that 
 408:             = (DefaultStatisticalCategoryDataset) obj;
 409:         if (!this.data.equals(that.data)) {
 410:             return false;   
 411:         }
 412:         return true;
 413:     }
 414: }