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