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: * KeyedObject.java
029: * ----------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: KeyedObject.java,v 1.5.2.1 2005/10/25 21:29:13 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 05-Feb-2003 : Version 1 (DG);
040: * 27-Jan-2003 : Implemented Cloneable and Serializable, and added an equals()
041: * method (DG);
042: *
043: */
044:
045: package org.jfree.data;
046:
047: import java.io.Serializable;
048:
049: import org.jfree.util.ObjectUtilities;
050: import org.jfree.util.PublicCloneable;
051:
052: /**
053: * A (key, object) pair.
054: */
055: public class KeyedObject implements Cloneable, PublicCloneable,
056: Serializable {
057:
058: /** For serialization. */
059: private static final long serialVersionUID = 2677930479256885863L;
060:
061: /** The key. */
062: private Comparable key;
063:
064: /** The object. */
065: private Object object;
066:
067: /**
068: * Creates a new (key, object) pair.
069: *
070: * @param key the key.
071: * @param object the object (<code>null</code> permitted).
072: */
073: public KeyedObject(Comparable key, Object object) {
074: this .key = key;
075: this .object = object;
076: }
077:
078: /**
079: * Returns the key.
080: *
081: * @return The key.
082: */
083: public Comparable getKey() {
084: return this .key;
085: }
086:
087: /**
088: * Returns the object.
089: *
090: * @return The object (possibly <code>null</code>).
091: */
092: public Object getObject() {
093: return this .object;
094: }
095:
096: /**
097: * Sets the object.
098: *
099: * @param object the object (<code>null</code> permitted).
100: */
101: public void setObject(Object object) {
102: this .object = object;
103: }
104:
105: /**
106: * Returns a clone of this object. It is assumed that the key is an
107: * immutable object, so it is not deep-cloned. The object is deep-cloned
108: * if it implements {@link PublicCloneable}, otherwise a shallow clone is
109: * made.
110: *
111: * @return A clone.
112: *
113: * @throws CloneNotSupportedException if there is a problem cloning.
114: */
115: public Object clone() throws CloneNotSupportedException {
116: KeyedObject clone = (KeyedObject) super .clone();
117: if (this .object instanceof PublicCloneable) {
118: PublicCloneable pc = (PublicCloneable) this .object;
119: clone.object = pc.clone();
120: }
121: return clone;
122: }
123:
124: /**
125: * Tests if this object is equal to another.
126: *
127: * @param obj the other object.
128: *
129: * @return A boolean.
130: */
131: public boolean equals(Object obj) {
132:
133: if (obj == this ) {
134: return true;
135: }
136:
137: if (!(obj instanceof KeyedObject)) {
138: return false;
139: }
140: KeyedObject that = (KeyedObject) obj;
141: if (!ObjectUtilities.equal(this .key, that.key)) {
142: return false;
143: }
144:
145: if (!ObjectUtilities.equal(this .object, that.object)) {
146: return false;
147: }
148:
149: return true;
150: }
151:
152: }
|