KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultStatisticalCategoryDataset.java
28  * --------------------------------------
29  * (C) Copyright 2002-2005, by Pascal Collet and Contributors.
30  *
31  * Original Author: Pascal Collet;
32  * Contributor(s): David Gilbert (for Object Refinery Limited);
33  *
34  * $Id: DefaultStatisticalCategoryDataset.java,v 1.8 2005/02/10 10:07:03 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 21-Aug-2002 : Version 1, contributed by Pascal Collet (DG);
39  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
40  * 05-Feb-2003 : Revised implementation to use KeyedObjects2D (DG);
41  * 28-Aug-2003 : Moved from org.jfree.data --> org.jfree.data.statistics (DG);
42  * 06-Oct-2003 : Removed incorrect Javadoc text (DG);
43  * 18-Nov-2004 : Updated for changes in RangeInfo interface (DG);
44  * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0
45  * release (DG);
46  * 01-Feb-2005 : Changed minimumRangeValue and maximumRangeValue from Double
47  * to double (DG);
48  * 05-Feb-2005 : Implemented equals() method (DG);
49  *
50  */

51
52 package org.jfree.data.statistics;
53
54 import java.util.List JavaDoc;
55
56 import org.jfree.data.KeyedObjects2D;
57 import org.jfree.data.Range;
58 import org.jfree.data.RangeInfo;
59 import org.jfree.data.general.AbstractDataset;
60
61 /**
62  * A convenience class that provides a default implementation of the
63  * {@link StatisticalCategoryDataset} interface.
64  *
65  * @author Pascal Collet
66  */

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

85     public DefaultStatisticalCategoryDataset() {
86         this.data = new KeyedObjects2D();
87         this.minimumRangeValue = 0.0;
88         this.maximumRangeValue = 0.0;
89         this.rangeBounds = new Range(0.0, 0.0);
90     }
91
92     /**
93      * Returns the mean value for an item.
94      *
95      * @param row the row index (zero-based).
96      * @param column the column index (zero-based).
97      *
98      * @return The mean value.
99      */

100     public Number JavaDoc getMeanValue(int row, int column) {
101         Number JavaDoc result = null;
102         MeanAndStandardDeviation masd
103             = (MeanAndStandardDeviation) this.data.getObject(row, column);
104         if (masd != null) {
105             result = masd.getMean();
106         }
107         return result;
108     }
109
110     /**
111      * Returns the value for an item (for this dataset, the mean value is
112      * returned).
113      *
114      * @param row the row index.
115      * @param column the column index.
116      *
117      * @return The value.
118      */

119     public Number JavaDoc getValue(int row, int column) {
120         return getMeanValue(row, column);
121     }
122
123     /**
124      * Returns the value for an item (for this dataset, the mean value is
125      * returned).
126      *
127      * @param rowKey the row key.
128      * @param columnKey the columnKey.
129      *
130      * @return The value.
131      */

132     public Number JavaDoc getValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
133         return getMeanValue(rowKey, columnKey);
134     }
135
136     /**
137      * Returns the mean value for an item.
138      *
139      * @param rowKey the row key.
140      * @param columnKey the columnKey.
141      *
142      * @return The mean value.
143      */

144     public Number JavaDoc getMeanValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
145         Number JavaDoc result = null;
146         MeanAndStandardDeviation masd
147             = (MeanAndStandardDeviation) this.data.getObject(rowKey, columnKey);
148         if (masd != null) {
149             result = masd.getMean();
150         }
151         return result;
152     }
153
154     /**
155      * Returns the standard deviation value for an item.
156      *
157      * @param row the row index (zero-based).
158      * @param column the column index (zero-based).
159      *
160      * @return The standard deviation.
161      */

162     public Number JavaDoc getStdDevValue(int row, int column) {
163         Number JavaDoc result = null;
164         MeanAndStandardDeviation masd
165             = (MeanAndStandardDeviation) this.data.getObject(row, column);
166         if (masd != null) {
167             result = masd.getStandardDeviation();
168         }
169         return result;
170     }
171
172     /**
173      * Returns the standard deviation value for an item.
174      *
175      * @param rowKey the row key.
176      * @param columnKey the columnKey.
177      *
178      * @return The standard deviation.
179      */

180     public Number JavaDoc getStdDevValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
181         Number JavaDoc result = null;
182         MeanAndStandardDeviation masd
183             = (MeanAndStandardDeviation) this.data.getObject(rowKey, columnKey);
184         if (masd != null) {
185             result = masd.getStandardDeviation();
186         }
187         return result;
188     }
189
190     /**
191      * Returns the column index for a given key.
192      *
193      * @param key the column key.
194      *
195      * @return The column index.
196      */

197     public int getColumnIndex(Comparable JavaDoc key) {
198         return this.data.getColumnIndex(key);
199     }
200
201     /**
202      * Returns a column key.
203      *
204      * @param column the column index (zero-based).
205      *
206      * @return The column key.
207      */

208     public Comparable JavaDoc getColumnKey(int column) {
209         return this.data.getColumnKey(column);
210     }
211
212     /**
213      * Returns the column keys.
214      *
215      * @return The keys.
216      */

217     public List JavaDoc getColumnKeys() {
218         return this.data.getColumnKeys();
219     }
220
221     /**
222      * Returns the row index for a given key.
223      *
224      * @param key the row key.
225      *
226      * @return The row index.
227      */

228     public int getRowIndex(Comparable JavaDoc key) {
229         return this.data.getRowIndex(key);
230     }
231
232     /**
233      * Returns a row key.
234      *
235      * @param row the row index (zero-based).
236      *
237      * @return The row key.
238      */

239     public Comparable JavaDoc getRowKey(int row) {
240         return this.data.getRowKey(row);
241     }
242
243     /**
244      * Returns the row keys.
245      *
246      * @return The keys.
247      */

248     public List JavaDoc getRowKeys() {
249         return this.data.getRowKeys();
250     }
251
252     /**
253      * Returns the number of rows in the table.
254      *
255      * @return The row count.
256      */

257     public int getRowCount() {
258         return this.data.getRowCount();
259     }
260
261     /**
262      * Returns the number of columns in the table.
263      *
264      * @return The column count.
265      */

266     public int getColumnCount() {
267         return this.data.getColumnCount();
268     }
269
270     /**
271      * Adds a mean and standard deviation to the table.
272      *
273      * @param mean the mean.
274      * @param standardDeviation the standard deviation.
275      * @param rowKey the row key.
276      * @param columnKey the column key.
277      */

278     public void add(double mean, double standardDeviation,
279                     Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
280         add(new Double JavaDoc(mean), new Double JavaDoc(standardDeviation), rowKey, columnKey);
281     }
282
283     /**
284      * Adds a mean and standard deviation to the table.
285      *
286      * @param mean the mean.
287      * @param standardDeviation the standard deviation.
288      * @param rowKey the row key.
289      * @param columnKey the column key.
290      */

291     public void add(Number JavaDoc mean, Number JavaDoc standardDeviation,
292                     Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
293         MeanAndStandardDeviation item = new MeanAndStandardDeviation(
294             mean, standardDeviation
295         );
296         this.data.addObject(item, rowKey, columnKey);
297         double m = 0.0;
298         double sd = 0.0;
299         if (mean != null) {
300             m = mean.doubleValue();
301         }
302         if (standardDeviation != null) {
303             sd = standardDeviation.doubleValue();
304         }
305         
306         if ((m + sd) > this.maximumRangeValue) {
307             this.maximumRangeValue = m + sd;
308             this.rangeBounds = new Range(
309                 this.minimumRangeValue, this.maximumRangeValue
310             );
311         }
312         if ((m - sd) < this.minimumRangeValue) {
313             this.minimumRangeValue = m - sd;
314             this.rangeBounds = new Range(
315                 this.minimumRangeValue, this.maximumRangeValue
316             );
317         }
318         fireDatasetChanged();
319     }
320
321     /**
322      * Returns the minimum y-value in the dataset.
323      *
324      * @param includeInterval a flag that determines whether or not the
325      * y-interval is taken into account (ignored for
326      * this dataset).
327      *
328      * @return The minimum value.
329      */

330     public double getRangeLowerBound(boolean includeInterval) {
331         return this.minimumRangeValue;
332     }
333
334     /**
335      * Returns the maximum y-value in the dataset.
336      *
337      * @param includeInterval a flag that determines whether or not the
338      * y-interval is taken into account (ignored for
339      * this dataset).
340      *
341      * @return The maximum value.
342      */

343     public double getRangeUpperBound(boolean includeInterval) {
344         return this.maximumRangeValue;
345     }
346
347     /**
348      * Returns the range of the values in this dataset's range.
349      *
350      * @param includeInterval a flag that determines whether or not the
351      * y-interval is taken into account.
352      *
353      * @return The range.
354      */

355     public Range getRangeBounds(boolean includeInterval) {
356         return this.rangeBounds;
357     }
358
359     /**
360      * Tests this instance for equality with an arbitrary object.
361      *
362      * @param obj the object (<code>null</code> permitted).
363      *
364      * @return A boolean.
365      */

366     public boolean equals(Object JavaDoc obj) {
367         if (obj == this) {
368             return true;
369         }
370         if (!(obj instanceof DefaultStatisticalCategoryDataset)) {
371             return false;
372         }
373         DefaultStatisticalCategoryDataset that
374             = (DefaultStatisticalCategoryDataset) obj;
375         if (!this.data.equals(that.data)) {
376             return false;
377         }
378         return true;
379     }
380 }
381
Popular Tags