KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > DefaultKeyedValueDataset


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -----------------------------
23  * DefaultKeyedValueDataset.java
24  * -----------------------------
25  * (C) Copyright 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id $
31  *
32  * Changes
33  * -------
34  * 27-Mar-2003 : Version 1 (DG);
35  * 18-Aug-2003 : Implemented Cloneable (DG);
36  *
37  */

38
39 package org.jfree.data;
40
41 import java.io.Serializable JavaDoc;
42
43 import org.jfree.util.ObjectUtils;
44
45 /**
46  * A default implementation of the {@link KeyedValueDataset} interface.
47  *
48  * @author David Gilbert
49  */

50 public class DefaultKeyedValueDataset extends AbstractDataset
51                                       implements KeyedValueDataset, Serializable JavaDoc {
52
53     /** Storage for the data. */
54     private KeyedValue data;
55
56     /**
57      * Constructs a new dataset, initially empty.
58      */

59     public DefaultKeyedValueDataset() {
60
61         this(null);
62
63     }
64
65     /**
66      * Creates a new dataset with the specified initial value.
67      *
68      * @param key the key.
69      * @param value the value.
70      */

71     public DefaultKeyedValueDataset(Comparable JavaDoc key, Number JavaDoc value) {
72         this(new DefaultKeyedValue(key, value));
73     }
74
75     /**
76      * Creates a new dataset that uses the data from a {@link KeyedValue} instance.
77      *
78      * @param data the data.
79      */

80     public DefaultKeyedValueDataset(KeyedValue data) {
81
82         this.data = data;
83
84     }
85
86     /**
87      * Returns the key associated with the value.
88      *
89      * @return the key.
90      */

91     public Comparable JavaDoc getKey() {
92         return this.data.getKey();
93     }
94
95     /**
96      * Returns the value.
97      *
98      * @return the value.
99      */

100     public Number JavaDoc getValue() {
101         return this.data.getValue();
102     }
103
104     /**
105      * Updates the value.
106      *
107      * @param value the new value (<code>null</code> permitted).
108      */

109     public void updateValue(Number JavaDoc value) {
110         if (this.data == null) {
111             throw new RuntimeException JavaDoc("updateValue: can't update null.");
112         }
113         setValue(this.data.getKey(), value);
114     }
115
116     /**
117      * Sets the value for the dataset. After the change is made, a {@link DatasetChangeEvent} is
118      * sent to all registered listeners.
119      *
120      * @param key the key.
121      * @param value the value.
122      */

123     public void setValue(Comparable JavaDoc key, Number JavaDoc value) {
124         this.data = new DefaultKeyedValue(key, value);
125         notifyListeners(new DatasetChangeEvent(this, this));
126     }
127
128     /**
129      * Tests this dataset for equality with an arbitrary object.
130      *
131      * @param obj the object.
132      *
133      * @return A boolean.
134      */

135     public boolean equals(Object JavaDoc obj) {
136
137         if (obj == null) {
138             return false;
139         }
140
141         if (obj == this) {
142             return true;
143         }
144
145         if (obj instanceof KeyedValueDataset) {
146             KeyedValueDataset kvd = (KeyedValueDataset) obj;
147             boolean b0 = ObjectUtils.equal(this.data.getKey(), kvd.getKey());
148             boolean b1 = ObjectUtils.equal(this.data.getValue(), kvd.getValue());
149             return b0 && b1;
150         }
151
152         return false;
153     }
154
155     /**
156      * Creates a clone of the dataset.
157      *
158      * @return A clone.
159      *
160      * @throws CloneNotSupportedException This class will not throw this exception, but subclasses
161      * (if any) might.
162      */

163     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
164         DefaultKeyedValueDataset clone = (DefaultKeyedValueDataset) super.clone();
165         return clone;
166     }
167     
168 }
169
Popular Tags