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