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. * TimePeriodValue.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: TimePeriodValue.java,v 1.2 2005/01/14 17:29:49 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 22-Apr-2003 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.data.time;
  40. import java.io.Serializable;
  41. /**
  42. * Represents a time period and an associated value.
  43. */
  44. public class TimePeriodValue implements Cloneable, Serializable {
  45. /** The time period. */
  46. private TimePeriod period;
  47. /** The value associated with the time period. */
  48. private Number value;
  49. /**
  50. * Constructs a new data item.
  51. *
  52. * @param period the time period.
  53. * @param value the value associated with the time period.
  54. */
  55. public TimePeriodValue(TimePeriod period, Number value) {
  56. this.period = period;
  57. this.value = value;
  58. }
  59. /**
  60. * Constructs a new data pair.
  61. *
  62. * @param period the time period.
  63. * @param value the value associated with the time period.
  64. */
  65. public TimePeriodValue(TimePeriod period, double value) {
  66. this(period, new Double(value));
  67. }
  68. /**
  69. * Returns the time period.
  70. *
  71. * @return The time period.
  72. */
  73. public TimePeriod getPeriod() {
  74. return this.period;
  75. }
  76. /**
  77. * Returns the value.
  78. *
  79. * @return The value (possibly <code>null</code>).
  80. */
  81. public Number getValue() {
  82. return this.value;
  83. }
  84. /**
  85. * Sets the value for this data item.
  86. *
  87. * @param value the new value (<code>null</code> permitted).
  88. */
  89. public void setValue(Number value) {
  90. this.value = value;
  91. }
  92. /**
  93. * Tests this object for equality with the target object.
  94. *
  95. * @param obj the object (<code>null</code> permitted).
  96. *
  97. * @return A boolean.
  98. */
  99. public boolean equals(Object obj) {
  100. if (this == obj) {
  101. return true;
  102. }
  103. if (!(obj instanceof TimePeriodValue)) {
  104. return false;
  105. }
  106. TimePeriodValue timePeriodValue = (TimePeriodValue) obj;
  107. if (this.period != null ? !this.period.equals(timePeriodValue.period)
  108. : timePeriodValue.period != null) {
  109. return false;
  110. }
  111. if (this.value != null ? !this.value.equals(timePeriodValue.value)
  112. : timePeriodValue.value != null) {
  113. return false;
  114. }
  115. return true;
  116. }
  117. /**
  118. * Returns a hash code value for the object.
  119. *
  120. * @return The hashcode
  121. */
  122. public int hashCode() {
  123. int result;
  124. result = (this.period != null ? this.period.hashCode() : 0);
  125. result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
  126. return result;
  127. }
  128. /**
  129. * Clones the object.
  130. * <P>
  131. * Note: no need to clone the period or value since they are immutable
  132. * classes.
  133. *
  134. * @return A clone.
  135. */
  136. public Object clone() {
  137. Object clone = null;
  138. try {
  139. clone = super.clone();
  140. }
  141. catch (CloneNotSupportedException e) { // won't get here...
  142. System.err.println("TimePeriodValue.clone(): operation not supported.");
  143. }
  144. return clone;
  145. }
  146. }