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