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. * KeyedObject.java
  26. * ----------------
  27. * (C) Copyright 2003-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: KeyedObject.java,v 1.3 2005/01/11 16:41:39 mungady Exp $
  33. *
  34. * Changes:
  35. * --------
  36. * 05-Feb-2003 : Version 1 (DG);
  37. * 27-Jan-2003 : Implemented Cloneable and Serializable, and added an equals() method (DG);
  38. *
  39. */
  40. package org.jfree.data;
  41. import java.io.Serializable;
  42. import org.jfree.util.ObjectUtilities;
  43. import org.jfree.util.PublicCloneable;
  44. /**
  45. * A (key, object) pair.
  46. */
  47. public class KeyedObject implements Cloneable, PublicCloneable, Serializable {
  48. /** The key. */
  49. private Comparable key;
  50. /** The object. */
  51. private Object object;
  52. /**
  53. * Creates a new (key, object) pair.
  54. *
  55. * @param key the key.
  56. * @param object the object (<code>null</code> permitted).
  57. */
  58. public KeyedObject(Comparable key, Object object) {
  59. this.key = key;
  60. this.object = object;
  61. }
  62. /**
  63. * Returns the key.
  64. *
  65. * @return The key.
  66. */
  67. public Comparable getKey() {
  68. return this.key;
  69. }
  70. /**
  71. * Returns the object.
  72. *
  73. * @return The object (possibly <code>null</code>).
  74. */
  75. public Object getObject() {
  76. return this.object;
  77. }
  78. /**
  79. * Sets the object.
  80. *
  81. * @param object the object (<code>null</code> permitted).
  82. */
  83. public void setObject(Object object) {
  84. this.object = object;
  85. }
  86. /**
  87. * Returns a clone of this object. It is assumed that the key is an immutable object,
  88. * so it is not deep-cloned. The object is deep-cloned if it implements
  89. * {@link PublicCloneable}, otherwise a shallow clone is made.
  90. *
  91. * @return A clone.
  92. *
  93. * @throws CloneNotSupportedException if there is a problem cloning.
  94. */
  95. public Object clone() throws CloneNotSupportedException {
  96. KeyedObject clone = (KeyedObject) super.clone();
  97. if (this.object instanceof PublicCloneable) {
  98. PublicCloneable pc = (PublicCloneable) this.object;
  99. clone.object = pc.clone();
  100. }
  101. return clone;
  102. }
  103. /**
  104. * Tests if this object is equal to another.
  105. *
  106. * @param obj the other object.
  107. *
  108. * @return A boolean.
  109. */
  110. public boolean equals(Object obj) {
  111. if (obj == this) {
  112. return true;
  113. }
  114. if (!(obj instanceof KeyedObject)) {
  115. return false;
  116. }
  117. KeyedObject that = (KeyedObject) obj;
  118. if (!ObjectUtilities.equal(this.key, that.key)) {
  119. return false;
  120. }
  121. if (!ObjectUtilities.equal(this.object, that.object)) {
  122. return false;
  123. }
  124. return true;
  125. }
  126. }