1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ----------------------
  25. * DefaultKeyedValue.java
  26. * ----------------------
  27. * (C) Copyright 2002-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: DefaultKeyedValue.java,v 1.4 2005/01/12 14:56:55 mungady Exp $
  33. *
  34. * Changes:
  35. * --------
  36. * 31-Oct-2002 : Version 1 (DG);
  37. * 13-Mar-2003 : Added equals() method, and implemented Serializable (DG);
  38. * 18-Aug-2003 : Implemented Cloneable (DG);
  39. * 18-Aug-2004 : Moved from org.jfree.data --> org.jfree.data.base (DG);
  40. * 15-Sep-2004 : Added PublicCloneable interface (DG);
  41. *
  42. */
  43. package org.jfree.data;
  44. import java.io.Serializable;
  45. import org.jfree.util.PublicCloneable;
  46. /**
  47. * A (key, value) pair. This class provides a default implementation
  48. * of the {@link KeyedValue} interface.
  49. */
  50. public class DefaultKeyedValue implements KeyedValue, Cloneable, PublicCloneable, Serializable {
  51. /** The key. */
  52. private Comparable key;
  53. /** The value. */
  54. private Number value;
  55. /**
  56. * Creates a new (key, value) item.
  57. *
  58. * @param key the key (should be immutable).
  59. * @param value the value (<code>null</code> permitted).
  60. */
  61. public DefaultKeyedValue(Comparable key, Number value) {
  62. this.key = key;
  63. this.value = value;
  64. }
  65. /**
  66. * Returns the key.
  67. *
  68. * @return The key.
  69. */
  70. public Comparable getKey() {
  71. return this.key;
  72. }
  73. /**
  74. * Returns the value.
  75. *
  76. * @return The value (possibly <code>null</code>).
  77. */
  78. public Number getValue() {
  79. return this.value;
  80. }
  81. /**
  82. * Sets the value.
  83. *
  84. * @param value the value (<code>null</code> permitted).
  85. */
  86. public synchronized void setValue(Number value) {
  87. this.value = value;
  88. }
  89. /**
  90. * Tests this key-value pair for equality with an arbitrary object.
  91. *
  92. * @param obj the object (<code>null</code> permitted).
  93. *
  94. * @return A boolean.
  95. */
  96. public boolean equals(Object obj) {
  97. if (obj == this) {
  98. return true;
  99. }
  100. if (!(obj instanceof DefaultKeyedValue)) {
  101. return false;
  102. }
  103. // TODO: modify this so that we check for equality with any KeyedValue
  104. // rather than specifically a DefaultKeyedValue
  105. DefaultKeyedValue that = (DefaultKeyedValue) obj;
  106. // TODO: the following checks for null should be handled in a utility method
  107. if (this.key != null ? !this.key.equals(that.key) : that.key != null) {
  108. return false;
  109. }
  110. if (this.value != null ? !this.value.equals(that.value) : that.value != null) {
  111. return false;
  112. }
  113. return true;
  114. }
  115. /**
  116. * Returns a hash code.
  117. *
  118. * @return A hash code.
  119. */
  120. public int hashCode() {
  121. int result;
  122. result = (this.key != null ? this.key.hashCode() : 0);
  123. result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
  124. return result;
  125. }
  126. /**
  127. * Returns a clone. It is assumed that both the key and value are immutable objects,
  128. * so only the references are cloned, not the objects themselves.
  129. *
  130. * @return A clone.
  131. *
  132. * @throws CloneNotSupportedException Not thrown by this class, but subclasses (if any) might.
  133. */
  134. public Object clone() throws CloneNotSupportedException {
  135. DefaultKeyedValue clone = (DefaultKeyedValue) super.clone();
  136. return clone;
  137. }
  138. }