KickJava   Java API By Example, From Geeks To Geeks.

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


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  * KeyedObject.java
28  * ----------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: KeyedObject.java,v 1.5 2005/05/19 10:34:07 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 05-Feb-2003 : Version 1 (DG);
39  * 27-Jan-2003 : Implemented Cloneable and Serializable, and added an equals()
40  * method (DG);
41  *
42  */

43
44 package org.jfree.data;
45
46 import java.io.Serializable JavaDoc;
47
48 import org.jfree.util.ObjectUtilities;
49 import org.jfree.util.PublicCloneable;
50
51 /**
52  * A (key, object) pair.
53  */

54 public class KeyedObject implements Cloneable JavaDoc, PublicCloneable, Serializable JavaDoc {
55
56     /** For serialization. */
57     private static final long serialVersionUID = 2677930479256885863L;
58     
59     /** The key. */
60     private Comparable JavaDoc key;
61
62     /** The object. */
63     private Object JavaDoc object;
64
65     /**
66      * Creates a new (key, object) pair.
67      *
68      * @param key the key.
69      * @param object the object (<code>null</code> permitted).
70      */

71     public KeyedObject(Comparable JavaDoc key, Object JavaDoc object) {
72         this.key = key;
73         this.object = object;
74     }
75
76     /**
77      * Returns the key.
78      *
79      * @return The key.
80      */

81     public Comparable JavaDoc getKey() {
82         return this.key;
83     }
84
85     /**
86      * Returns the object.
87      *
88      * @return The object (possibly <code>null</code>).
89      */

90     public Object JavaDoc getObject() {
91         return this.object;
92     }
93
94     /**
95      * Sets the object.
96      *
97      * @param object the object (<code>null</code> permitted).
98      */

99     public void setObject(Object JavaDoc object) {
100         this.object = object;
101     }
102     
103     /**
104      * Returns a clone of this object. It is assumed that the key is an
105      * immutable object, so it is not deep-cloned. The object is deep-cloned
106      * if it implements {@link PublicCloneable}, otherwise a shallow clone is
107      * made.
108      *
109      * @return A clone.
110      *
111      * @throws CloneNotSupportedException if there is a problem cloning.
112      */

113     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
114         KeyedObject clone = (KeyedObject) super.clone();
115         if (this.object instanceof PublicCloneable) {
116             PublicCloneable pc = (PublicCloneable) this.object;
117             clone.object = pc.clone();
118         }
119         return clone;
120     }
121     
122     /**
123      * Tests if this object is equal to another.
124      *
125      * @param obj the other object.
126      *
127      * @return A boolean.
128      */

129     public boolean equals(Object JavaDoc obj) {
130
131         if (obj == this) {
132             return true;
133         }
134
135         if (!(obj instanceof KeyedObject)) {
136             return false;
137         }
138         KeyedObject that = (KeyedObject) obj;
139         if (!ObjectUtilities.equal(this.key, that.key)) {
140             return false;
141         }
142
143         if (!ObjectUtilities.equal(this.object, that.object)) {
144             return false;
145         }
146
147         return true;
148     }
149     
150 }
151
Popular Tags