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. * DefaultKeyedValueDataset.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 $
  33. *
  34. * Changes
  35. * -------
  36. * 27-Mar-2003 : Version 1 (DG);
  37. * 18-Aug-2003 : Implemented Cloneable (DG);
  38. *
  39. */
  40. package org.jfree.data.general;
  41. import java.io.Serializable;
  42. import org.jfree.data.DefaultKeyedValue;
  43. import org.jfree.data.KeyedValue;
  44. import org.jfree.util.ObjectUtilities;
  45. /**
  46. * A default implementation of the {@link KeyedValueDataset} interface.
  47. */
  48. public class DefaultKeyedValueDataset extends AbstractDataset
  49. implements KeyedValueDataset, Serializable {
  50. /** Storage for the data. */
  51. private KeyedValue data;
  52. /**
  53. * Constructs a new dataset, initially empty.
  54. */
  55. public DefaultKeyedValueDataset() {
  56. this(null);
  57. }
  58. /**
  59. * Creates a new dataset with the specified initial value.
  60. *
  61. * @param key the key.
  62. * @param value the value (<code>null</code> permitted).
  63. */
  64. public DefaultKeyedValueDataset(Comparable key, Number value) {
  65. this(new DefaultKeyedValue(key, value));
  66. }
  67. /**
  68. * Creates a new dataset that uses the data from a {@link KeyedValue} instance.
  69. *
  70. * @param data the data (<code>null</code> permitted).
  71. */
  72. public DefaultKeyedValueDataset(KeyedValue data) {
  73. this.data = data;
  74. }
  75. /**
  76. * Returns the key associated with the value, or <code>null</code> if the dataset has no
  77. * data item.
  78. *
  79. * @return The key.
  80. */
  81. public Comparable getKey() {
  82. Comparable result = null;
  83. if (this.data != null) {
  84. result = this.data.getKey();
  85. }
  86. return result;
  87. }
  88. /**
  89. * Returns the value.
  90. *
  91. * @return The value (possibly <code>null</code>).
  92. */
  93. public Number getValue() {
  94. Number result = null;
  95. if (this.data != null) {
  96. result = this.data.getValue();
  97. }
  98. return result;
  99. }
  100. /**
  101. * Updates the value.
  102. *
  103. * @param value the new value (<code>null</code> permitted).
  104. */
  105. public void updateValue(Number value) {
  106. if (this.data == null) {
  107. throw new RuntimeException("updateValue: can't update null.");
  108. }
  109. setValue(this.data.getKey(), value);
  110. }
  111. /**
  112. * Sets the value for the dataset and sends a {@link DatasetChangeEvent} to all registered
  113. * listeners.
  114. *
  115. * @param key the key.
  116. * @param value the value (<code>null</code> permitted).
  117. */
  118. public void setValue(Comparable key, Number value) {
  119. this.data = new DefaultKeyedValue(key, value);
  120. notifyListeners(new DatasetChangeEvent(this, this));
  121. }
  122. /**
  123. * Tests this dataset for equality with an arbitrary object.
  124. *
  125. * @param obj the object.
  126. *
  127. * @return A boolean.
  128. */
  129. public boolean equals(Object obj) {
  130. if (obj == null) {
  131. return false;
  132. }
  133. if (obj == this) {
  134. return true;
  135. }
  136. if (!(obj instanceof KeyedValueDataset)) {
  137. return false;
  138. }
  139. KeyedValueDataset kvd = (KeyedValueDataset) obj;
  140. if (this.data == null) {
  141. if (kvd.getKey() != null || kvd.getValue() != null) {
  142. return false;
  143. }
  144. return true;
  145. }
  146. if (!ObjectUtilities.equal(this.data.getKey(), kvd.getKey())) {
  147. return false;
  148. }
  149. if (!ObjectUtilities.equal(this.data.getValue(), kvd.getValue())) {
  150. return false;
  151. }
  152. return true;
  153. }
  154. /**
  155. * Returns a hash code.
  156. *
  157. * @return A hash code.
  158. */
  159. public int hashCode() {
  160. return (this.data != null ? this.data.hashCode() : 0);
  161. }
  162. /**
  163. * Creates a clone of the dataset.
  164. *
  165. * @return A clone.
  166. *
  167. * @throws CloneNotSupportedException This class will not throw this exception, but subclasses
  168. * (if any) might.
  169. */
  170. public Object clone() throws CloneNotSupportedException {
  171. DefaultKeyedValueDataset clone = (DefaultKeyedValueDataset) super.clone();
  172. return clone;
  173. }
  174. }