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
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * ------------------------
  27. * DefaultValueDataset.java
  28. * ------------------------
  29. * (C) Copyright 2003-2005, by Object Refinery Limited.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id $
  35. *
  36. * Changes
  37. * -------
  38. * 27-Mar-2003 : Version 1 (DG);
  39. * 18-Aug-2003 : Implemented Cloneable (DG);
  40. * 03-Mar-2005 : Implemented PublicCloneable (DG);
  41. *
  42. */
  43. package org.jfree.data.general;
  44. import java.io.Serializable;
  45. import org.jfree.util.ObjectUtilities;
  46. import org.jfree.util.PublicCloneable;
  47. /**
  48. * A dataset that stores a single value. This class provides a default
  49. * implementation of the {@link ValueDataset} interface.
  50. */
  51. public class DefaultValueDataset extends AbstractDataset
  52. implements ValueDataset,
  53. Cloneable, PublicCloneable,
  54. Serializable {
  55. /** The value. */
  56. private Number value;
  57. /**
  58. * Constructs a new dataset, initially empty.
  59. */
  60. public DefaultValueDataset() {
  61. this(null);
  62. }
  63. /**
  64. * Creates a new dataset.
  65. *
  66. * @param value the value.
  67. */
  68. public DefaultValueDataset(double value) {
  69. this(new Double(value));
  70. }
  71. /**
  72. * Creates a new dataset.
  73. *
  74. * @param value the initial value.
  75. */
  76. public DefaultValueDataset(Number value) {
  77. this.value = value;
  78. }
  79. /**
  80. * Returns the value.
  81. *
  82. * @return The value (possibly <code>null</code>).
  83. */
  84. public Number getValue() {
  85. return this.value;
  86. }
  87. /**
  88. * Sets the value and sends a {@link DatasetChangeEvent} to all registered
  89. * listeners.
  90. *
  91. * @param value the new value (<code>null</code> permitted).
  92. */
  93. public void setValue(Number value) {
  94. this.value = value;
  95. notifyListeners(new DatasetChangeEvent(this, this));
  96. }
  97. /**
  98. * Tests this dataset for equality with an arbitrary object.
  99. *
  100. * @param obj the object.
  101. *
  102. * @return A boolean.
  103. */
  104. public boolean equals(Object obj) {
  105. if (obj == null) {
  106. return false;
  107. }
  108. if (obj == this) {
  109. return true;
  110. }
  111. if (obj instanceof ValueDataset) {
  112. ValueDataset vd = (ValueDataset) obj;
  113. return ObjectUtilities.equal(this.value, vd.getValue());
  114. }
  115. return false;
  116. }
  117. /**
  118. * Returns a hash code.
  119. *
  120. * @return A hash code.
  121. */
  122. public int hashCode() {
  123. return (this.value != null ? this.value.hashCode() : 0);
  124. }
  125. }