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-2008, 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-2008, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 22-Apr-2003 : Version 1 (DG);
  38:  * 03-Oct-2006 : Added null argument check to constructor (DG);
  39:  * 07-Apr-2008 : Added a toString() override for debugging (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 (<code>null</code> not permitted).
  65:      * @param value  the value associated with the time period.
  66:      * 
  67:      * @throws IllegalArgumentException if <code>period</code> is 
  68:      *     <code>null</code>.
  69:      */
  70:     public TimePeriodValue(TimePeriod period, Number value) {
  71:         if (period == null) {
  72:             throw new IllegalArgumentException("Null 'period' argument.");
  73:         }
  74:         this.period = period;
  75:         this.value = value;
  76:     }
  77: 
  78:     /**
  79:      * Constructs a new data item.
  80:      *
  81:      * @param period  the time period (<code>null</code> not permitted).
  82:      * @param value  the value associated with the time period.
  83:      * 
  84:      * @throws IllegalArgumentException if <code>period</code> is 
  85:      *     <code>null</code>.
  86:      */
  87:     public TimePeriodValue(TimePeriod period, double value) {
  88:         this(period, new Double(value));
  89:     }
  90: 
  91:     /**
  92:      * Returns the time period.
  93:      *
  94:      * @return The time period (never <code>null</code>).
  95:      */
  96:     public TimePeriod getPeriod() {
  97:         return this.period;
  98:     }
  99: 
 100:     /**
 101:      * Returns the value.
 102:      *
 103:      * @return The value (possibly <code>null</code>).
 104:      * 
 105:      * @see #setValue(Number)
 106:      */
 107:     public Number getValue() {
 108:         return this.value;
 109:     }
 110: 
 111:     /**
 112:      * Sets the value for this data item.
 113:      *
 114:      * @param value  the new value (<code>null</code> permitted).
 115:      * 
 116:      * @see #getValue()
 117:      */
 118:     public void setValue(Number value) {
 119:         this.value = value;
 120:     }
 121: 
 122:     /**
 123:      * Tests this object for equality with the target object.
 124:      *
 125:      * @param obj  the object (<code>null</code> permitted).
 126:      *
 127:      * @return A boolean.
 128:      */
 129:     public boolean equals(Object obj) {
 130:         if (this == obj) {
 131:             return true;
 132:         }
 133:         if (!(obj instanceof TimePeriodValue)) {
 134:             return false;
 135:         }
 136: 
 137:         TimePeriodValue timePeriodValue = (TimePeriodValue) obj;
 138: 
 139:         if (this.period != null ? !this.period.equals(timePeriodValue.period) 
 140:                 : timePeriodValue.period != null) {
 141:             return false;
 142:         }
 143:         if (this.value != null ? !this.value.equals(timePeriodValue.value) 
 144:                 : timePeriodValue.value != null) {
 145:             return false;
 146:         }
 147: 
 148:         return true;
 149:     }
 150: 
 151:     /**
 152:      * Returns a hash code value for the object.
 153:      *
 154:      * @return The hashcode
 155:      */
 156:     public int hashCode() {
 157:         int result;
 158:         result = (this.period != null ? this.period.hashCode() : 0);
 159:         result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
 160:         return result;
 161:     }
 162: 
 163:     /**
 164:      * Clones the object.
 165:      * <P>
 166:      * Note: no need to clone the period or value since they are immutable 
 167:      * classes.
 168:      *
 169:      * @return A clone.
 170:      */
 171:     public Object clone() {
 172:         Object clone = null;
 173:         try {
 174:             clone = super.clone();
 175:         }
 176:         catch (CloneNotSupportedException e) { // won't get here...
 177:             e.printStackTrace();
 178:         }
 179:         return clone;
 180:     }
 181:     
 182:     /**
 183:      * Returns a string representing this instance, primarily for use in 
 184:      * debugging.
 185:      * 
 186:      * @return A string.
 187:      */
 188:     public String toString() {
 189:         return "TimePeriodValue[" + getPeriod() + "," + getValue() + "]";
 190:     }
 191: 
 192: }