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     * DefaultCategoryDataset.java
029     * ---------------------------
030     * (C) Copyright 2002-2005, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * $Id: DefaultCategoryDataset.java,v 1.5.2.2 2005/10/25 21:29:58 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 21-Jan-2003 : Added standard header, and renamed DefaultCategoryDataset (DG);
040     * 13-Mar-2003 : Inserted DefaultKeyedValues2DDataset into class hierarchy (DG);
041     * 06-Oct-2003 : Added incrementValue() method (DG);
042     * 05-Apr-2004 : Added clear() method (DG);
043     * 18-Aug-2004 : Moved from org.jfree.data --> org.jfree.data.category (DG);
044     *
045     */
046    
047    package org.jfree.data.category;
048    
049    import java.io.Serializable;
050    import java.util.List;
051    
052    import org.jfree.data.DefaultKeyedValues2D;
053    import org.jfree.data.UnknownKeyException;
054    import org.jfree.data.general.AbstractDataset;
055    import org.jfree.data.general.DatasetChangeEvent;
056    
057    /**
058     * A default implementation of the {@link CategoryDataset} interface.
059     */
060    public class DefaultCategoryDataset extends AbstractDataset
061                                        implements CategoryDataset, Serializable {
062    
063        /** For serialization. */
064        private static final long serialVersionUID = -8168173757291644622L;
065        
066        /** A storage structure for the data. */
067        private DefaultKeyedValues2D data;
068    
069        /**
070         * Creates a new (empty) dataset.
071         */
072        public DefaultCategoryDataset() {
073            this.data = new DefaultKeyedValues2D();
074        }
075    
076        /**
077         * Returns the number of rows in the table.
078         *
079         * @return The row count.
080         */
081        public int getRowCount() {
082            return this.data.getRowCount();
083        }
084    
085        /**
086         * Returns the number of columns in the table.
087         *
088         * @return The column count.
089         */
090        public int getColumnCount() {
091            return this.data.getColumnCount();
092        }
093    
094        /**
095         * Returns a value from the table.
096         *
097         * @param row  the row index (zero-based).
098         * @param column  the column index (zero-based).
099         *
100         * @return The value (possibly <code>null</code>).
101         */
102        public Number getValue(int row, int column) {
103            return this.data.getValue(row, column);
104        }
105    
106        /**
107         * Returns a row key.
108         *
109         * @param row  the row index (zero-based).
110         *
111         * @return The row key.
112         */
113        public Comparable getRowKey(int row) {
114            return this.data.getRowKey(row);
115        }
116    
117        /**
118         * Returns the row index for a given key.
119         *
120         * @param key  the row key.
121         *
122         * @return The row index.
123         */
124        public int getRowIndex(Comparable key) {
125            return this.data.getRowIndex(key);
126        }
127    
128        /**
129         * Returns the row keys.
130         *
131         * @return The keys.
132         */
133        public List getRowKeys() {
134            return this.data.getRowKeys();
135        }
136    
137        /**
138         * Returns a column key.
139         *
140         * @param column  the column index (zero-based).
141         *
142         * @return The column key.
143         */
144        public Comparable getColumnKey(int column) {
145            return this.data.getColumnKey(column);
146        }
147    
148        /**
149         * Returns the column index for a given key.
150         *
151         * @param key  the column key.
152         *
153         * @return The column index.
154         */
155        public int getColumnIndex(Comparable key) {
156            return this.data.getColumnIndex(key);
157        }
158    
159        /**
160         * Returns the column keys.
161         *
162         * @return The keys.
163         */
164        public List getColumnKeys() {
165            return this.data.getColumnKeys();
166        }
167    
168        /**
169         * Returns the value for a pair of keys.
170         *
171         * @param rowKey  the row key (<code>null</code> not permitted).
172         * @param columnKey  the column key (<code>null</code> not permitted).
173         *
174         * @return The value (possibly <code>null</code>).
175         * 
176         * @throws UnknownKeyException if either key is not defined in the dataset.
177         */
178        public Number getValue(Comparable rowKey, Comparable columnKey) {
179            return this.data.getValue(rowKey, columnKey);
180        }
181    
182        /**
183         * Adds a value to the table.  Performs the same function as setValue().
184         *
185         * @param value  the value.
186         * @param rowKey  the row key.
187         * @param columnKey  the column key.
188         */
189        public void addValue(Number value, Comparable rowKey, 
190                             Comparable columnKey) {
191            this.data.addValue(value, rowKey, columnKey);
192            fireDatasetChanged();
193        }
194    
195        /**
196         * Adds a value to the table.
197         *
198         * @param value  the value.
199         * @param rowKey  the row key.
200         * @param columnKey  the column key.
201         */
202        public void addValue(double value, Comparable rowKey, 
203                             Comparable columnKey) {
204            addValue(new Double(value), rowKey, columnKey);
205        }
206    
207        /**
208         * Adds or updates a value in the table and sends a 
209         * {@link DatasetChangeEvent} to all registered listeners.
210         *
211         * @param value  the value (<code>null</code> permitted).
212         * @param rowKey  the row key (<code>null</code> not permitted).
213         * @param columnKey  the column key (<code>null</code> not permitted).
214         */
215        public void setValue(Number value, Comparable rowKey, 
216                             Comparable columnKey) {
217            this.data.setValue(value, rowKey, columnKey);
218            fireDatasetChanged();
219        }
220    
221        /**
222         * Adds or updates a value in the table and sends a 
223         * {@link DatasetChangeEvent} to all registered listeners.
224         *
225         * @param value  the value.
226         * @param rowKey  the row key (<code>null</code> not permitted).
227         * @param columnKey  the column key (<code>null</code> not permitted).
228         */
229        public void setValue(double value, Comparable rowKey, 
230                             Comparable columnKey) {
231            setValue(new Double(value), rowKey, columnKey);
232        }
233    
234        /**
235         * Adds the specified value to an existing value in the dataset (if the 
236         * existing value is <code>null</code>, it is treated as if it were 0.0).
237         * 
238         * @param value  the value.
239         * @param rowKey  the row key (<code>null</code> not permitted).
240         * @param columnKey  the column key (<code>null</code> not permitted).
241         * 
242         * @throws UnknownKeyException if either key is not defined in the dataset.
243         */
244        public void incrementValue(double value, 
245                                   Comparable rowKey, 
246                                   Comparable columnKey) {
247            double existing = 0.0;
248            Number n = getValue(rowKey, columnKey);
249            if (n != null) {
250                existing = n.doubleValue();
251            }
252            setValue(existing + value, rowKey, columnKey);
253        }
254        
255        /**
256         * Removes a value from the dataset.
257         *
258         * @param rowKey  the row key.
259         * @param columnKey  the column key.
260         */
261        public void removeValue(Comparable rowKey, Comparable columnKey) {
262            this.data.removeValue(rowKey, columnKey);
263            fireDatasetChanged();
264        }
265    
266        /**
267         * Removes a row from the dataset.
268         *
269         * @param rowIndex  the row index.
270         */
271        public void removeRow(int rowIndex) {
272            this.data.removeRow(rowIndex);
273            fireDatasetChanged();
274        }
275    
276        /**
277         * Removes a row from the dataset.
278         *
279         * @param rowKey  the row key.
280         */
281        public void removeRow(Comparable rowKey) {
282            this.data.removeRow(rowKey);
283            fireDatasetChanged();
284        }
285    
286        /**
287         * Removes a column from the dataset.
288         *
289         * @param columnIndex  the column index.
290         */
291        public void removeColumn(int columnIndex) {
292            this.data.removeColumn(columnIndex);
293            fireDatasetChanged();
294        }
295    
296        /**
297         * Removes a column from the dataset.
298         *
299         * @param columnKey  the column key.
300         */
301        public void removeColumn(Comparable columnKey) {
302            this.data.removeColumn(columnKey);
303            fireDatasetChanged();
304        }
305    
306        /**
307         * Clears all data from the dataset and sends a {@link DatasetChangeEvent} 
308         * to all registered listeners.
309         */
310        public void clear() {
311            this.data.clear();
312            fireDatasetChanged();
313        }
314        
315        /**
316         * Tests if this object is equal to another.
317         *
318         * @param obj  the other object.
319         *
320         * @return A boolean.
321         */
322        public boolean equals(Object obj) {
323    
324            if (obj == this) {
325                return true;
326            }
327    
328            if (!(obj instanceof CategoryDataset)) {
329                return false;
330            }
331    
332            CategoryDataset that = (CategoryDataset) obj;
333            if (!getRowKeys().equals(that.getRowKeys())) {
334                return false;
335            }
336    
337            if (!getColumnKeys().equals(that.getColumnKeys())) {
338                return false;
339            }
340    
341            int rowCount = getRowCount();
342            int colCount = getColumnCount();
343            for (int r = 0; r < rowCount; r++) {
344                for (int c = 0; c < colCount; c++) {
345                    Number v1 = getValue(r, c);
346                    Number v2 = that.getValue(r, c);
347                    if (v1 == null) {
348                        if (v2 != null) {
349                            return false;
350                        }
351                    }
352                    else if (!v1.equals(v2)) {
353                        return false;
354                    }
355                }
356            }
357            return true;
358        }
359    
360        /**
361         * Returns a hash code for the dataset.
362         * 
363         * @return A hash code.
364         */
365        public int hashCode() {
366            return this.data.hashCode();
367        }
368        
369    }