    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2007, 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-2007, 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
    043    package org.jfree.data;
    044
    045    import java.io.Serializable;
    046
    047    import org.jfree.util.ObjectUtilities;
    048    import org.jfree.util.PublicCloneable;
    049
    050    /**
    051     * A (key, object) pair.
    052     */
    053    public 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        public Object clone() throws CloneNotSupportedException {
    113            KeyedObject clone = (KeyedObject) super.clone();
    114            if (this.object instanceof PublicCloneable) {
    115                PublicCloneable pc = (PublicCloneable) this.object;
    116                clone.object = pc.clone();
    117            }
    118            return clone;
    119        }
    120
    121        /**
    122         * Tests if this object is equal to another.
    123         *
    124         * @param obj  the other object.
    125         *
    126         * @return A boolean.
    127         */
    128        public boolean equals(Object obj) {
    129
    130            if (obj == this) {
    131                return true;
    132            }
    133
    134            if (!(obj instanceof KeyedObject)) {
    135                return false;
    136            }
    137            KeyedObject that = (KeyedObject) obj;
    138            if (!ObjectUtilities.equal(this.key, that.key)) {
    139                return false;
    140            }
    141
    142            if (!ObjectUtilities.equal(this.object, that.object)) {
    143                return false;
    144            }
    145
    146            return true;
    147        }
    148
    149    }