KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultKeyedValue.java
28  * ----------------------
29  * (C) Copyright 2002-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: DefaultKeyedValue.java,v 1.6 2005/05/19 10:34:07 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 31-Oct-2002 : Version 1 (DG);
39  * 13-Mar-2003 : Added equals() method, and implemented Serializable (DG);
40  * 18-Aug-2003 : Implemented Cloneable (DG);
41  * 18-Aug-2004 : Moved from org.jfree.data --> org.jfree.data.base (DG);
42  * 15-Sep-2004 : Added PublicCloneable interface (DG);
43  *
44  */

45
46 package org.jfree.data;
47
48 import java.io.Serializable JavaDoc;
49
50 import org.jfree.util.PublicCloneable;
51
52 /**
53  * A (key, value) pair. This class provides a default implementation
54  * of the {@link KeyedValue} interface.
55  */

56 public class DefaultKeyedValue implements KeyedValue,
57                                           Cloneable JavaDoc, PublicCloneable,
58                                           Serializable JavaDoc {
59
60     /** For serialization. */
61     private static final long serialVersionUID = -7388924517460437712L;
62     
63     /** The key. */
64     private Comparable JavaDoc key;
65
66     /** The value. */
67     private Number JavaDoc value;
68
69     /**
70      * Creates a new (key, value) item.
71      *
72      * @param key the key (should be immutable).
73      * @param value the value (<code>null</code> permitted).
74      */

75     public DefaultKeyedValue(Comparable JavaDoc key, Number JavaDoc value) {
76         this.key = key;
77         this.value = value;
78     }
79
80     /**
81      * Returns the key.
82      *
83      * @return The key.
84      */

85     public Comparable JavaDoc getKey() {
86         return this.key;
87     }
88
89     /**
90      * Returns the value.
91      *
92      * @return The value (possibly <code>null</code>).
93      */

94     public Number JavaDoc getValue() {
95         return this.value;
96     }
97
98     /**
99      * Sets the value.
100      *
101      * @param value the value (<code>null</code> permitted).
102      */

103     public synchronized void setValue(Number JavaDoc value) {
104         this.value = value;
105     }
106
107     /**
108      * Tests this key-value pair for equality with an arbitrary object.
109      *
110      * @param obj the object (<code>null</code> permitted).
111      *
112      * @return A boolean.
113      */

114     public boolean equals(Object JavaDoc obj) {
115         if (obj == this) {
116             return true;
117         }
118         if (!(obj instanceof DefaultKeyedValue)) {
119             return false;
120         }
121         // TODO: modify this so that we check for equality with any KeyedValue
122
// rather than specifically a DefaultKeyedValue
123
DefaultKeyedValue that = (DefaultKeyedValue) obj;
124         
125         // TODO: the following checks for null should be handled in a utility
126
// method
127
if (this.key != null ? !this.key.equals(that.key) : that.key != null) {
128             return false;
129         }
130         if (this.value != null
131                 ? !this.value.equals(that.value) : that.value != null) {
132             return false;
133         }
134         return true;
135     }
136
137     /**
138      * Returns a hash code.
139      *
140      * @return A hash code.
141      */

142     public int hashCode() {
143         int result;
144         result = (this.key != null ? this.key.hashCode() : 0);
145         result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
146         return result;
147     }
148
149     /**
150      * Returns a clone. It is assumed that both the key and value are
151      * immutable objects, so only the references are cloned, not the objects
152      * themselves.
153      *
154      * @return A clone.
155      *
156      * @throws CloneNotSupportedException Not thrown by this class, but
157      * subclasses (if any) might.
158      */

159     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
160         DefaultKeyedValue clone = (DefaultKeyedValue) super.clone();
161         return clone;
162     }
163
164 }
165
Popular Tags