KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > statistics > DefaultBoxAndWhiskerCategoryDataset


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ----------------------------------------
27  * DefaultBoxAndWhiskerCategoryDataset.java
28  * ----------------------------------------
29  * (C) Copyright 2003-2005, by David Browning and Contributors.
30  *
31  * Original Author: David Browning (for Australian Institute of Marine
32  * Science);
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: DefaultBoxAndWhiskerCategoryDataset.java,v 1.9 2005/05/20 08:58:08 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 05-Aug-2003 : Version 1, contributed by David Browning (DG);
40  * 27-Aug-2003 : Moved from org.jfree.data --> org.jfree.data.statistics (DG);
41  * 12-Nov-2003 : Changed 'data' from private to protected and added a new 'add'
42  * method as proposed by Tim Bardzil. Also removed old code (DG);
43  * 01-Mar-2004 : Added equals() method (DG);
44  * 18-Nov-2004 : Updates for changes in RangeInfo interface (DG);
45  * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0
46  * release (DG);
47  *
48  */

49
50 package org.jfree.data.statistics;
51
52 import java.util.List JavaDoc;
53
54 import org.jfree.data.KeyedObjects2D;
55 import org.jfree.data.Range;
56 import org.jfree.data.RangeInfo;
57 import org.jfree.data.general.AbstractDataset;
58 import org.jfree.util.ObjectUtilities;
59
60 /**
61  * A convenience class that provides a default implementation of the
62  * {@link BoxAndWhiskerCategoryDataset} interface.
63  *
64  * @author David Browning (for Australian Institute of Marine Science)
65  */

66 public class DefaultBoxAndWhiskerCategoryDataset extends AbstractDataset
67     implements BoxAndWhiskerCategoryDataset, RangeInfo {
68
69     /** Storage for the data. */
70     protected KeyedObjects2D data;
71
72     /** The minimum range value. */
73     private Number JavaDoc minimumRangeValue;
74
75     /** The maximum range value. */
76     private Number JavaDoc maximumRangeValue;
77
78     /** The range of values. */
79     private Range rangeBounds;
80
81     /**
82      * Creates a new dataset.
83      */

84     public DefaultBoxAndWhiskerCategoryDataset() {
85         this.data = new KeyedObjects2D();
86         this.minimumRangeValue = null;
87         this.maximumRangeValue = null;
88         this.rangeBounds = new Range(0.0, 0.0);
89     }
90
91     /**
92      * Adds a list of values relating to one box-and-whisker entity to the
93      * table. The various median values are calculated.
94      *
95      * @param list a collection of values from which the various medians will
96      * be calculated.
97      * @param rowKey the row key.
98      * @param columnKey the column key.
99      */

100     public void add(List JavaDoc list, Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
101         BoxAndWhiskerItem item
102             = BoxAndWhiskerCalculator.calculateBoxAndWhiskerStatistics(list);
103         add(item, rowKey, columnKey);
104     }
105     
106     /**
107      * Adds a list of values relating to one Box and Whisker entity to the
108      * table. The various median values are calculated.
109      *
110      * @param item a box and whisker item.
111      * @param rowKey the row key.
112      * @param columnKey the column key.
113      */

114     public void add(BoxAndWhiskerItem item,
115                     Comparable JavaDoc rowKey,
116                     Comparable JavaDoc columnKey) {
117
118         this.data.addObject(item, rowKey, columnKey);
119         double minval = item.getMinOutlier().doubleValue();
120         double maxval = item.getMaxOutlier().doubleValue();
121         
122         if (this.maximumRangeValue == null) {
123             this.maximumRangeValue = new Double JavaDoc(maxval);
124         }
125         else if (maxval > this.maximumRangeValue.doubleValue()) {
126             this.maximumRangeValue = new Double JavaDoc(maxval);
127         }
128         
129         if (this.minimumRangeValue == null) {
130             this.minimumRangeValue = new Double JavaDoc(minval);
131         }
132         else if (minval < this.minimumRangeValue.doubleValue()) {
133             this.minimumRangeValue = new Double JavaDoc(minval);
134         }
135         
136         this.rangeBounds = new Range(
137             this.minimumRangeValue.doubleValue(),
138             this.maximumRangeValue.doubleValue()
139         );
140
141         fireDatasetChanged();
142
143     }
144
145     /**
146      * Return an item from within the dataset.
147      *
148      * @param row the row index.
149      * @param column the column index.
150      *
151      * @return The item.
152      */

153     public BoxAndWhiskerItem getItem(int row, int column) {
154         return (BoxAndWhiskerItem) this.data.getObject(row, column);
155     }
156
157     /**
158      * Returns the value for an item.
159      *
160      * @param row the row index.
161      * @param column the column index.
162      *
163      * @return The value.
164      */

165     public Number JavaDoc getValue(int row, int column) {
166         return getMedianValue(row, column);
167     }
168
169     /**
170      * Returns the value for an item.
171      *
172      * @param rowKey the row key.
173      * @param columnKey the columnKey.
174      *
175      * @return The value.
176      */

177     public Number JavaDoc getValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
178         return getMedianValue(rowKey, columnKey);
179     }
180
181     /**
182      * Returns the mean value for an item.
183      *
184      * @param row the row index (zero-based).
185      * @param column the column index (zero-based).
186      *
187      * @return The mean value.
188      */

189     public Number JavaDoc getMeanValue(int row, int column) {
190
191         Number JavaDoc result = null;
192         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
193             row, column
194         );
195         if (item != null) {
196             result = item.getMean();
197         }
198         return result;
199
200     }
201
202     /**
203      * Returns the mean value for an item.
204      *
205      * @param rowKey the row key.
206      * @param columnKey the column key.
207      *
208      * @return The mean value.
209      */

210     public Number JavaDoc getMeanValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
211
212         Number JavaDoc result = null;
213         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
214             rowKey, columnKey
215         );
216         if (item != null) {
217             result = item.getMean();
218         }
219         return result;
220
221     }
222
223     /**
224      * Returns the median value for an item.
225      *
226      * @param row the row index (zero-based).
227      * @param column the column index (zero-based).
228      *
229      * @return The median value.
230      */

231     public Number JavaDoc getMedianValue(int row, int column) {
232         Number JavaDoc result = null;
233         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
234             row, column
235         );
236         if (item != null) {
237             result = item.getMedian();
238         }
239         return result;
240     }
241
242     /**
243      * Returns the median value for an item.
244      *
245      * @param rowKey the row key.
246      * @param columnKey the columnKey.
247      *
248      * @return The median value.
249      */

250     public Number JavaDoc getMedianValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
251         Number JavaDoc result = null;
252         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
253             rowKey, columnKey
254         );
255         if (item != null) {
256             result = item.getMedian();
257         }
258         return result;
259     }
260
261     /**
262      * Returns the first quartile value.
263      *
264      * @param row the row index (zero-based).
265      * @param column the column index (zero-based).
266      *
267      * @return The first quartile value.
268      */

269     public Number JavaDoc getQ1Value(int row, int column) {
270         Number JavaDoc result = null;
271         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
272             row, column
273         );
274         if (item != null) {
275             result = item.getQ1();
276         }
277         return result;
278     }
279
280     /**
281      * Returns the first quartile value.
282      *
283      * @param rowKey the row key.
284      * @param columnKey the column key.
285      *
286      * @return The first quartile value.
287      */

288     public Number JavaDoc getQ1Value(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
289         Number JavaDoc result = null;
290         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
291             rowKey, columnKey
292         );
293         if (item != null) {
294             result = item.getQ1();
295         }
296         return result;
297     }
298
299     /**
300      * Returns the third quartile value.
301      *
302      * @param row the row index (zero-based).
303      * @param column the column index (zero-based).
304      *
305      * @return The third quartile value.
306      */

307     public Number JavaDoc getQ3Value(int row, int column) {
308         Number JavaDoc result = null;
309         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
310             row, column
311         );
312         if (item != null) {
313             result = item.getQ3();
314         }
315         return result;
316     }
317
318     /**
319      * Returns the third quartile value.
320      *
321      * @param rowKey the row key.
322      * @param columnKey the column key.
323      *
324      * @return The third quartile value.
325      */

326     public Number JavaDoc getQ3Value(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
327         Number JavaDoc result = null;
328         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
329             rowKey, columnKey
330         );
331         if (item != null) {
332             result = item.getQ3();
333         }
334         return result;
335     }
336
337     /**
338      * Returns the column index for a given key.
339      *
340      * @param key the column key.
341      *
342      * @return The column index.
343      */

344     public int getColumnIndex(Comparable JavaDoc key) {
345         return this.data.getColumnIndex(key);
346     }
347
348     /**
349      * Returns a column key.
350      *
351      * @param column the column index (zero-based).
352      *
353      * @return The column key.
354      */

355     public Comparable JavaDoc getColumnKey(int column) {
356         return this.data.getColumnKey(column);
357     }
358
359     /**
360      * Returns the column keys.
361      *
362      * @return The keys.
363      */

364     public List JavaDoc getColumnKeys() {
365         return this.data.getColumnKeys();
366     }
367
368     /**
369      * Returns the row index for a given key.
370      *
371      * @param key the row key.
372      *
373      * @return The row index.
374      */

375     public int getRowIndex(Comparable JavaDoc key) {
376         return this.data.getRowIndex(key);
377     }
378
379     /**
380      * Returns a row key.
381      *
382      * @param row the row index (zero-based).
383      *
384      * @return The row key.
385      */

386     public Comparable JavaDoc getRowKey(int row) {
387         return this.data.getRowKey(row);
388     }
389
390     /**
391      * Returns the row keys.
392      *
393      * @return The keys.
394      */

395     public List JavaDoc getRowKeys() {
396         return this.data.getRowKeys();
397     }
398
399     /**
400      * Returns the number of rows in the table.
401      *
402      * @return The row count.
403      */

404     public int getRowCount() {
405         return this.data.getRowCount();
406     }
407
408     /**
409      * Returns the number of columns in the table.
410      *
411      * @return The column count.
412      */

413     public int getColumnCount() {
414         return this.data.getColumnCount();
415     }
416
417     /**
418      * Returns the minimum y-value in the dataset.
419      *
420      * @param includeInterval a flag that determines whether or not the
421      * y-interval is taken into account.
422      *
423      * @return The minimum value.
424      */

425     public double getRangeLowerBound(boolean includeInterval) {
426         double result = Double.NaN;
427         if (this.minimumRangeValue != null) {
428             result = this.minimumRangeValue.doubleValue();
429         }
430         return result;
431     }
432
433     /**
434      * Returns the maximum y-value in the dataset.
435      *
436      * @param includeInterval a flag that determines whether or not the
437      * y-interval is taken into account.
438      *
439      * @return The maximum value.
440      */

441     public double getRangeUpperBound(boolean includeInterval) {
442         double result = Double.NaN;
443         if (this.maximumRangeValue != null) {
444             result = this.maximumRangeValue.doubleValue();
445         }
446         return result;
447     }
448
449     /**
450      * Returns the range of the values in this dataset's range.
451      *
452      * @param includeInterval a flag that determines whether or not the
453      * y-interval is taken into account.
454      *
455      * @return The range.
456      */

457     public Range getRangeBounds(boolean includeInterval) {
458         return this.rangeBounds;
459     }
460     
461     /**
462      * Returns the minimum regular (non outlier) value for an item.
463      *
464      * @param row the row index (zero-based).
465      * @param column the column index (zero-based).
466      *
467      * @return The minimum regular value.
468      */

469     public Number JavaDoc getMinRegularValue(int row, int column) {
470
471         Number JavaDoc result = null;
472         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
473             row, column
474         );
475         if (item != null) {
476             result = item.getMinRegularValue();
477         }
478         return result;
479
480     }
481
482     /**
483      * Returns the minimum regular (non outlier) value for an item.
484      *
485      * @param rowKey the row key.
486      * @param columnKey the column key.
487      *
488      * @return The minimum regular value.
489      */

490     public Number JavaDoc getMinRegularValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
491
492         Number JavaDoc result = null;
493         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
494             rowKey, columnKey
495         );
496         if (item != null) {
497             result = item.getMinRegularValue();
498         }
499         return result;
500
501     }
502
503     /**
504      * Returns the maximum regular (non outlier) value for an item.
505      *
506      * @param row the row index (zero-based).
507      * @param column the column index (zero-based).
508      *
509      * @return The maximum regular value.
510      */

511     public Number JavaDoc getMaxRegularValue(int row, int column) {
512
513         Number JavaDoc result = null;
514         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
515             row, column
516         );
517         if (item != null) {
518             result = item.getMaxRegularValue();
519         }
520         return result;
521
522     }
523
524     /**
525      * Returns the maximum regular (non outlier) value for an item.
526      *
527      * @param rowKey the row key.
528      * @param columnKey the column key.
529      *
530      * @return The maximum regular value.
531      */

532     public Number JavaDoc getMaxRegularValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
533
534         Number JavaDoc result = null;
535         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
536             rowKey, columnKey
537         );
538         if (item != null) {
539             result = item.getMaxRegularValue();
540         }
541         return result;
542
543     }
544
545     /**
546      * Returns the minimum outlier (non farout) value for an item.
547      *
548      * @param row the row index (zero-based).
549      * @param column the column index (zero-based).
550      *
551      * @return The minimum outlier.
552      */

553     public Number JavaDoc getMinOutlier(int row, int column) {
554
555         Number JavaDoc result = null;
556         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
557             row, column
558         );
559         if (item != null) {
560             result = item.getMinOutlier();
561         }
562         return result;
563
564     }
565
566     /**
567      * Returns the minimum outlier (non farout) value for an item.
568      *
569      * @param rowKey the row key.
570      * @param columnKey the column key.
571      *
572      * @return The minimum outlier.
573      */

574     public Number JavaDoc getMinOutlier(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
575
576         Number JavaDoc result = null;
577         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
578             rowKey, columnKey
579         );
580         if (item != null) {
581             result = item.getMinOutlier();
582         }
583         return result;
584
585     }
586
587     /**
588      * Returns the maximum outlier (non farout) value for an item.
589      *
590      * @param row the row index (zero-based).
591      * @param column the column index (zero-based).
592      *
593      * @return The maximum outlier.
594      */

595     public Number JavaDoc getMaxOutlier(int row, int column) {
596
597         Number JavaDoc result = null;
598         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
599             row, column
600         );
601         if (item != null) {
602             result = item.getMaxOutlier();
603         }
604         return result;
605
606     }
607
608     /**
609      * Returns the maximum outlier (non farout) value for an item.
610      *
611      * @param rowKey the row key.
612      * @param columnKey the column key.
613      *
614      * @return The maximum outlier.
615      */

616     public Number JavaDoc getMaxOutlier(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
617
618         Number JavaDoc result = null;
619         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
620             rowKey, columnKey
621         );
622         if (item != null) {
623             result = item.getMaxOutlier();
624         }
625         return result;
626
627     }
628
629     /**
630      * Returns a list of outlier values for an item.
631      *
632      * @param row the row index (zero-based).
633      * @param column the column index (zero-based).
634      *
635      * @return A list of outlier values.
636      */

637     public List JavaDoc getOutliers(int row, int column) {
638
639         List JavaDoc result = null;
640         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
641             row, column
642         );
643         if (item != null) {
644             result = item.getOutliers();
645         }
646         return result;
647
648     }
649
650     /**
651      * Returns a list of outlier values for an item.
652      *
653      * @param rowKey the row key.
654      * @param columnKey the column key.
655      *
656      * @return A list of outlier values.
657      */

658     public List JavaDoc getOutliers(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
659
660         List JavaDoc result = null;
661         BoxAndWhiskerItem item = (BoxAndWhiskerItem) this.data.getObject(
662             rowKey, columnKey
663         );
664         if (item != null) {
665             result = item.getOutliers();
666         }
667         return result;
668
669     }
670     
671     /**
672      * Tests this dataset for equality with an arbitrary object.
673      *
674      * @param obj the object to test against (<code>null</code> permitted).
675      *
676      * @return A boolean.
677      */

678     public boolean equals(Object JavaDoc obj) {
679         
680         if (obj == null) {
681             return false;
682         }
683         
684         if (obj == this) {
685             return true;
686         }
687         
688         if (obj instanceof DefaultBoxAndWhiskerCategoryDataset) {
689             DefaultBoxAndWhiskerCategoryDataset dataset
690                 = (DefaultBoxAndWhiskerCategoryDataset) obj;
691             return ObjectUtilities.equal(this.data, dataset.data);
692         }
693         
694         return false;
695     }
696
697 }
698
Popular Tags