    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, Serializable {
    056
    057        /** For serialization. */
    058        private static final long serialVersionUID = 2677930479256885863L;
    059
    060        /** The key. */
    061        private Comparable key;
    062
    063        /** The object. */
    064        private Object object;
    065
    066        /**
    067         * Creates a new (key, object) pair.
    068         *
    069         * @param key  the key.
    070         * @param object  the object (<code>null</code> permitted).
    071         */
    072        public KeyedObject(Comparable key, Object object) {
    073            this.key = key;
    074            this.object = object;
    075        }
    076
    077        /**
    078         * Returns the key.
    079         *
    080         * @return The key.
    081         */
    082        public Comparable getKey() {
    083            return this.key;
    084        }
    085
    086        /**
    087         * Returns the object.
    088         *
    089         * @return The object (possibly <code>null</code>).
    090         */
    091        public Object getObject() {
    092            return this.object;
    093        }
    094
    095        /**
    096         * Sets the object.
    097         *
    098         * @param object  the object (<code>null</code> permitted).
    099         */
    100        public void setObject(Object object) {
    101            this.object = object;
    102        }
    103
    104        /**
    105         * Returns a clone of this object.  It is assumed that the key is an
    106         * immutable object, so it is not deep-cloned.  The object is deep-cloned
    107         * if it implements {@link PublicCloneable}, otherwise a shallow clone is
    108         * made.
    109         *
    110         * @return A clone.
    111         *
    112         * @throws CloneNotSupportedException if there is a problem cloning.
    113         */
    114        public Object clone() throws CloneNotSupportedException {
    115            KeyedObject clone = (KeyedObject) super.clone();
    116            if (this.object instanceof PublicCloneable) {
    117                PublicCloneable pc = (PublicCloneable) this.object;
    118                clone.object = pc.clone();
    119            }
    120            return clone;
    121        }
    122
    123        /**
    124         * Tests if this object is equal to another.
    125         *
    126         * @param obj  the other object.
    127         *
    128         * @return A boolean.
    129         */
    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    }