Source for org.jfree.data.time.TimePeriodValue

   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
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * --------------------
  28:  * TimePeriodValue.java
  29:  * --------------------
  30:  * (C) Copyright 2003-2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: TimePeriodValue.java,v 1.4.2.1 2005/10/25 21:35:24 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 22-Apr-2003 : Version 1 (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.data.time;
  44: 
  45: import java.io.Serializable;
  46: 
  47: /**
  48:  * Represents a time period and an associated value.
  49:  */
  50: public class TimePeriodValue implements Cloneable, Serializable {
  51: 
  52:     /** For serialization. */
  53:     private static final long serialVersionUID = 3390443360845711275L;
  54:     
  55:     /** The time period. */
  56:     private TimePeriod period;
  57: 
  58:     /** The value associated with the time period. */
  59:     private Number value;
  60: 
  61:     /**
  62:      * Constructs a new data item.
  63:      *
  64:      * @param period  the time period.
  65:      * @param value  the value associated with the time period.
  66:      */
  67:     public TimePeriodValue(TimePeriod period, Number value) {
  68:         this.period = period;
  69:         this.value = value;
  70:     }
  71: 
  72:     /**
  73:      * Constructs a new data pair.
  74:      *
  75:      * @param period  the time period.
  76:      * @param value  the value associated with the time period.
  77:      */
  78:     public TimePeriodValue(TimePeriod period, double value) {
  79:         this(period, new Double(value));
  80:     }
  81: 
  82:     /**
  83:      * Returns the time period.
  84:      *
  85:      * @return The time period.
  86:      */
  87:     public TimePeriod getPeriod() {
  88:         return this.period;
  89:     }
  90: 
  91:     /**
  92:      * Returns the value.
  93:      *
  94:      * @return The value (possibly <code>null</code>).
  95:      */
  96:     public Number getValue() {
  97:         return this.value;
  98:     }
  99: 
 100:     /**
 101:      * Sets the value for this data item.
 102:      *
 103:      * @param value  the new value (<code>null</code> permitted).
 104:      */
 105:     public void setValue(Number value) {
 106:         this.value = value;
 107:     }
 108: 
 109:     /**
 110:      * Tests this object for equality with the target object.
 111:      *
 112:      * @param obj  the object (<code>null</code> permitted).
 113:      *
 114:      * @return A boolean.
 115:      */
 116:     public boolean equals(Object obj) {
 117:         if (this == obj) {
 118:             return true;
 119:         }
 120:         if (!(obj instanceof TimePeriodValue)) {
 121:             return false;
 122:         }
 123: 
 124:         TimePeriodValue timePeriodValue = (TimePeriodValue) obj;
 125: 
 126:         if (this.period != null ? !this.period.equals(timePeriodValue.period) 
 127:                 : timePeriodValue.period != null) {
 128:             return false;
 129:         }
 130:         if (this.value != null ? !this.value.equals(timePeriodValue.value) 
 131:                 : timePeriodValue.value != null) {
 132:             return false;
 133:         }
 134: 
 135:         return true;
 136:     }
 137: 
 138:     /**
 139:      * Returns a hash code value for the object.
 140:      *
 141:      * @return The hashcode
 142:      */
 143:     public int hashCode() {
 144:         int result;
 145:         result = (this.period != null ? this.period.hashCode() : 0);
 146:         result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
 147:         return result;
 148:     }
 149: 
 150:     /**
 151:      * Clones the object.
 152:      * <P>
 153:      * Note: no need to clone the period or value since they are immutable 
 154:      * classes.
 155:      *
 156:      * @return A clone.
 157:      */
 158:     public Object clone() {
 159:         Object clone = null;
 160:         try {
 161:             clone = super.clone();
 162:         }
 163:         catch (CloneNotSupportedException e) { // won't get here...
 164:             System.err.println("Operation not supported.");
 165:         }
 166:         return clone;
 167: 
 168:     }
 169: 
 170: }