001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006     *
007     * Project Info:  http://www.jfree.org/jfreechart/index.html
008     *
009     * This library is free software; you can redistribute it and/or modify it 
010     * under the terms of the GNU Lesser General Public License as published by 
011     * the Free Software Foundation; either version 2.1 of the License, or 
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but 
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022     * USA.  
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     *
027     * --------------------------------------
028     * DefaultStatisticalCategoryDataset.java
029     * --------------------------------------
030     * (C) Copyright 2002-2005, by Pascal Collet and Contributors.
031     *
032     * Original Author:  Pascal Collet;
033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
034     *
035     * $Id: DefaultStatisticalCategoryDataset.java,v 1.8.2.1 2005/10/25 21:34:46 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 21-Aug-2002 : Version 1, contributed by Pascal Collet (DG);
040     * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041     * 05-Feb-2003 : Revised implementation to use KeyedObjects2D (DG);
042     * 28-Aug-2003 : Moved from org.jfree.data --> org.jfree.data.statistics (DG);
043     * 06-Oct-2003 : Removed incorrect Javadoc text (DG);
044     * 18-Nov-2004 : Updated for changes in RangeInfo interface (DG);
045     * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0 
046     *               release (DG);
047     * 01-Feb-2005 : Changed minimumRangeValue and maximumRangeValue from Double
048     *               to double (DG);
049     * 05-Feb-2005 : Implemented equals() method (DG);
050     *
051     */
052    
053    package org.jfree.data.statistics;
054    
055    import java.util.List;
056    
057    import org.jfree.data.KeyedObjects2D;
058    import org.jfree.data.Range;
059    import org.jfree.data.RangeInfo;
060    import org.jfree.data.general.AbstractDataset;
061    
062    /**
063     * A convenience class that provides a default implementation of the
064     * {@link StatisticalCategoryDataset} interface.
065     *
066     * @author Pascal Collet
067     */
068    public class DefaultStatisticalCategoryDataset extends AbstractDataset
069        implements StatisticalCategoryDataset, RangeInfo {
070    
071        /** Storage for the data. */
072        private KeyedObjects2D data;
073    
074        /** The minimum range value. */
075        private double minimumRangeValue;
076    
077        /** The maximum range value. */
078        private double maximumRangeValue;
079    
080        /** The range of values. */
081        private Range rangeBounds;
082    
083        /**
084         * Creates a new dataset.
085         */
086        public DefaultStatisticalCategoryDataset() {
087            this.data = new KeyedObjects2D();
088            this.minimumRangeValue = 0.0;
089            this.maximumRangeValue = 0.0;
090            this.rangeBounds = new Range(0.0, 0.0);
091        }
092    
093        /**
094         * Returns the mean value for an item.
095         *
096         * @param row  the row index (zero-based).
097         * @param column  the column index (zero-based).
098         *
099         * @return The mean value.
100         */
101        public Number getMeanValue(int row, int column) {
102            Number result = null;
103            MeanAndStandardDeviation masd 
104                = (MeanAndStandardDeviation) this.data.getObject(row, column);
105            if (masd != null) {
106                result = masd.getMean();
107            }
108            return result;
109        }
110    
111        /**
112         * Returns the value for an item (for this dataset, the mean value is
113         * returned).
114         *
115         * @param row  the row index.
116         * @param column  the column index.
117         *
118         * @return The value.
119         */
120        public Number getValue(int row, int column) {
121            return getMeanValue(row, column);
122        }
123    
124        /**
125         * Returns the value for an item (for this dataset, the mean value is
126         * returned).
127         *
128         * @param rowKey  the row key.
129         * @param columnKey  the columnKey.
130         *
131         * @return The value.
132         */
133        public Number getValue(Comparable rowKey, Comparable columnKey) {
134            return getMeanValue(rowKey, columnKey);
135        }
136    
137        /**
138         * Returns the mean value for an item.
139         *
140         * @param rowKey  the row key.
141         * @param columnKey  the columnKey.
142         *
143         * @return The mean value.
144         */
145        public Number getMeanValue(Comparable rowKey, Comparable columnKey) {
146            Number result = null;
147            MeanAndStandardDeviation masd
148                = (MeanAndStandardDeviation) this.data.getObject(rowKey, columnKey);
149            if (masd != null) {
150                result = masd.getMean();
151            }
152            return result;
153        }
154    
155        /**
156         * Returns the standard deviation value for an item.
157         *
158         * @param row  the row index (zero-based).
159         * @param column  the column index (zero-based).
160         *
161         * @return The standard deviation.
162         */
163        public Number getStdDevValue(int row, int column) {
164            Number result = null;
165            MeanAndStandardDeviation masd 
166                = (MeanAndStandardDeviation) this.data.getObject(row, column);
167            if (masd != null) {
168                result = masd.getStandardDeviation();
169            }
170            return result;
171        }
172    
173        /**
174         * Returns the standard deviation value for an item.
175         *
176         * @param rowKey  the row key.
177         * @param columnKey  the columnKey.
178         *
179         * @return The standard deviation.
180         */
181        public Number getStdDevValue(Comparable rowKey, Comparable columnKey) {
182            Number result = null;
183            MeanAndStandardDeviation masd
184                = (MeanAndStandardDeviation) this.data.getObject(rowKey, columnKey);
185            if (masd != null) {
186                result = masd.getStandardDeviation();
187            }
188            return result;
189        }
190    
191        /**
192         * Returns the column index for a given key.
193         *
194         * @param key  the column key.
195         *
196         * @return The column index.
197         */
198        public int getColumnIndex(Comparable key) {
199            return this.data.getColumnIndex(key);
200        }
201    
202        /**
203         * Returns a column key.
204         *
205         * @param column  the column index (zero-based).
206         *
207         * @return The column key.
208         */
209        public Comparable getColumnKey(int column) {
210            return this.data.getColumnKey(column);
211        }
212    
213        /**
214         * Returns the column keys.
215         *
216         * @return The keys.
217         */
218        public List getColumnKeys() {
219            return this.data.getColumnKeys();
220        }
221    
222        /**
223         * Returns the row index for a given key.
224         *
225         * @param key  the row key.
226         *
227         * @return The row index.
228         */
229        public int getRowIndex(Comparable key) {
230            return this.data.getRowIndex(key);
231        }
232    
233        /**
234         * Returns a row key.
235         *
236         * @param row  the row index (zero-based).
237         *
238         * @return The row key.
239         */
240        public Comparable getRowKey(int row) {
241            return this.data.getRowKey(row);
242        }
243    
244        /**
245         * Returns the row keys.
246         *
247         * @return The keys.
248         */
249        public List getRowKeys() {
250            return this.data.getRowKeys();
251        }
252    
253        /**
254         * Returns the number of rows in the table.
255         *
256         * @return The row count.
257         */
258        public int getRowCount() {
259            return this.data.getRowCount();
260        }
261    
262        /**
263         * Returns the number of columns in the table.
264         *
265         * @return The column count.
266         */
267        public int getColumnCount() {
268            return this.data.getColumnCount();
269        }
270    
271        /**
272         * Adds a mean and standard deviation to the table.
273         *
274         * @param mean  the mean.
275         * @param standardDeviation  the standard deviation.
276         * @param rowKey  the row key.
277         * @param columnKey  the column key.
278         */
279        public void add(double mean, double standardDeviation,
280                        Comparable rowKey, Comparable columnKey) {
281            add(new Double(mean), new Double(standardDeviation), rowKey, columnKey);
282        }
283    
284        /**
285         * Adds a mean and standard deviation to the table.
286         *
287         * @param mean  the mean.
288         * @param standardDeviation  the standard deviation.
289         * @param rowKey  the row key.
290         * @param columnKey  the column key.
291         */
292        public void add(Number mean, Number standardDeviation,
293                        Comparable rowKey, Comparable columnKey) {
294            MeanAndStandardDeviation item = new MeanAndStandardDeviation(
295                mean, standardDeviation
296            );
297            this.data.addObject(item, rowKey, columnKey);
298            double m = 0.0;
299            double sd = 0.0;
300            if (mean != null) {
301                m = mean.doubleValue();
302            }
303            if (standardDeviation != null) {
304                sd = standardDeviation.doubleValue();   
305            }
306            
307            if ((m + sd) > this.maximumRangeValue) {
308                this.maximumRangeValue = m + sd;
309                this.rangeBounds = new Range(
310                    this.minimumRangeValue, this.maximumRangeValue
311                );
312            }
313            if ((m - sd) < this.minimumRangeValue) {
314                this.minimumRangeValue = m - sd;
315                this.rangeBounds = new Range(
316                    this.minimumRangeValue, this.maximumRangeValue
317                );
318            }
319            fireDatasetChanged();
320        }
321    
322        /**
323         * Returns the minimum y-value in the dataset.
324         *
325         * @param includeInterval  a flag that determines whether or not the
326         *                         y-interval is taken into account (ignored for
327         *                         this dataset).
328         * 
329         * @return The minimum value.
330         */
331        public double getRangeLowerBound(boolean includeInterval) {
332            return this.minimumRangeValue;        
333        }
334    
335        /**
336         * Returns the maximum y-value in the dataset.
337         *
338         * @param includeInterval  a flag that determines whether or not the
339         *                         y-interval is taken into account (ignored for
340         *                         this dataset).
341         * 
342         * @return The maximum value.
343         */
344        public double getRangeUpperBound(boolean includeInterval) {
345            return this.maximumRangeValue;        
346        }
347    
348        /**
349         * Returns the range of the values in this dataset's range.
350         *
351         * @param includeInterval  a flag that determines whether or not the
352         *                         y-interval is taken into account.
353         * 
354         * @return The range.
355         */
356        public Range getRangeBounds(boolean includeInterval) {
357            return this.rangeBounds;
358        }
359    
360        /**
361         * Tests this instance for equality with an arbitrary object.
362         * 
363         * @param obj  the object (<code>null</code> permitted).
364         * 
365         * @return A boolean.
366         */
367        public boolean equals(Object obj) {
368            if (obj == this) {
369                return true;   
370            }
371            if (!(obj instanceof DefaultStatisticalCategoryDataset)) {
372                return false;   
373            }
374            DefaultStatisticalCategoryDataset that 
375                = (DefaultStatisticalCategoryDataset) obj;
376            if (!this.data.equals(that.data)) {
377                return false;   
378            }
379            return true;
380        }
381    }