001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ----------------
028 * KeyedObject.java
029 * ----------------
030 * (C) Copyright 2003-2008, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 05-Feb-2003 : Version 1 (DG);
038 * 27-Jan-2003 : Implemented Cloneable and Serializable, and added an equals()
039 *               method (DG);
040 *
041 */
042
043package org.jfree.data;
044
045import java.io.Serializable;
046
047import org.jfree.util.ObjectUtilities;
048import org.jfree.util.PublicCloneable;
049
050/**
051 * A (key, object) pair.
052 */
053public class KeyedObject implements Cloneable, PublicCloneable, Serializable {
054
055    /** For serialization. */
056    private static final long serialVersionUID = 2677930479256885863L;
057
058    /** The key. */
059    private Comparable key;
060
061    /** The object. */
062    private Object object;
063
064    /**
065     * Creates a new (key, object) pair.
066     *
067     * @param key  the key.
068     * @param object  the object (<code>null</code> permitted).
069     */
070    public KeyedObject(Comparable key, Object object) {
071        this.key = key;
072        this.object = object;
073    }
074
075    /**
076     * Returns the key.
077     *
078     * @return The key.
079     */
080    public Comparable getKey() {
081        return this.key;
082    }
083
084    /**
085     * Returns the object.
086     *
087     * @return The object (possibly <code>null</code>).
088     */
089    public Object getObject() {
090        return this.object;
091    }
092
093    /**
094     * Sets the object.
095     *
096     * @param object  the object (<code>null</code> permitted).
097     */
098    public void setObject(Object object) {
099        this.object = object;
100    }
101
102    /**
103     * Returns a clone of this object.  It is assumed that the key is an
104     * immutable object, so it is not deep-cloned.  The object is deep-cloned
105     * if it implements {@link PublicCloneable}, otherwise a shallow clone is
106     * made.
107     *
108     * @return A clone.
109     *
110     * @throws CloneNotSupportedException if there is a problem cloning.
111     */
112    @Override
113    public Object clone() throws CloneNotSupportedException {
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    @Override
130    public boolean equals(Object obj) {
131
132        if (obj == this) {
133            return true;
134        }
135
136        if (!(obj instanceof KeyedObject)) {
137            return false;
138        }
139        KeyedObject that = (KeyedObject) obj;
140        if (!ObjectUtilities.equal(this.key, that.key)) {
141            return false;
142        }
143
144        if (!ObjectUtilities.equal(this.object, that.object)) {
145            return false;
146        }
147
148        return true;
149    }
150
151}