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 * DefaultKeyedValue.java 029 * ---------------------- 030 * (C) Copyright 2002-2016, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes: 036 * -------- 037 * 31-Oct-2002 : Version 1 (DG); 038 * 13-Mar-2003 : Added equals() method, and implemented Serializable (DG); 039 * 18-Aug-2003 : Implemented Cloneable (DG); 040 * 18-Aug-2004 : Moved from org.jfree.data --> org.jfree.data.base (DG); 041 * 15-Sep-2004 : Added PublicCloneable interface (DG); 042 * ------------- JFREECHART 1.0.x --------------------------------------------- 043 * 11-Jun-2007 : Added toString() method to help with debugging (DG); 044 * 15-Feb-2008 : Prevent null key (DG); 045 * 07-Apr-2008 : Removed to-do item (DG); 046 * 03-Jul-2013 : Use ParamChecks (DG); 047 * 048 */ 049 050package org.jfree.data; 051 052import java.io.Serializable; 053import org.jfree.chart.util.Args; 054import org.jfree.chart.util.PublicCloneable; 055 056/** 057 * A (key, value) pair. This class provides a default implementation 058 * of the {@link KeyedValue} interface. 059 */ 060public class DefaultKeyedValue implements KeyedValue, Cloneable, 061 PublicCloneable, Serializable { 062 063 /** For serialization. */ 064 private static final long serialVersionUID = -7388924517460437712L; 065 066 /** The key. */ 067 private Comparable key; 068 069 /** The value. */ 070 private Number value; 071 072 /** 073 * Creates a new (key, value) item. 074 * 075 * @param key the key (should be immutable, {@code null} not 076 * permitted). 077 * @param value the value ({@code null} permitted). 078 */ 079 public DefaultKeyedValue(Comparable key, Number value) { 080 Args.nullNotPermitted(key, "key"); 081 this.key = key; 082 this.value = value; 083 } 084 085 /** 086 * Returns the key. 087 * 088 * @return The key (never {@code null}). 089 */ 090 @Override 091 public Comparable getKey() { 092 return this.key; 093 } 094 095 /** 096 * Returns the value. 097 * 098 * @return The value (possibly {@code null}). 099 */ 100 @Override 101 public Number getValue() { 102 return this.value; 103 } 104 105 /** 106 * Sets the value. 107 * 108 * @param value the value ({@code null} permitted). 109 */ 110 public synchronized void setValue(Number value) { 111 this.value = value; 112 } 113 114 /** 115 * Tests this key-value pair for equality with an arbitrary object. 116 * 117 * @param obj the object ({@code null} permitted). 118 * 119 * @return A boolean. 120 */ 121 @Override 122 public boolean equals(Object obj) { 123 if (obj == this) { 124 return true; 125 } 126 if (!(obj instanceof DefaultKeyedValue)) { 127 return false; 128 } 129 DefaultKeyedValue that = (DefaultKeyedValue) obj; 130 131 if (!this.key.equals(that.key)) { 132 return false; 133 } 134 if (this.value != null 135 ? !this.value.equals(that.value) : that.value != null) { 136 return false; 137 } 138 return true; 139 } 140 141 /** 142 * Returns a hash code. 143 * 144 * @return A hash code. 145 */ 146 @Override 147 public int hashCode() { 148 int result; 149 result = (this.key != null ? this.key.hashCode() : 0); 150 result = 29 * result + (this.value != null ? this.value.hashCode() : 0); 151 return result; 152 } 153 154 /** 155 * Returns a clone. It is assumed that both the key and value are 156 * immutable objects, so only the references are cloned, not the objects 157 * themselves. 158 * 159 * @return A clone. 160 * 161 * @throws CloneNotSupportedException Not thrown by this class, but 162 * subclasses (if any) might. 163 */ 164 @Override 165 public Object clone() throws CloneNotSupportedException { 166 return (DefaultKeyedValue) super.clone(); 167 } 168 169 /** 170 * Returns a string representing this instance, primarily useful for 171 * debugging. 172 * 173 * @return A string. 174 */ 175 @Override 176 public String toString() { 177 return "(" + this.key.toString() + ", " + this.value.toString() + ")"; 178 } 179 180}