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-2006, 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-2006, 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.2 2006/10/03 15:16:33 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 22-Apr-2003 : Version 1 (DG);
  40:  * 03-Oct-2006 : Added null argument check to constructor (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.data.time;
  45: 
  46: import java.io.Serializable;
  47: 
  48: /**
  49:  * Represents a time period and an associated value.
  50:  */
  51: public class TimePeriodValue implements Cloneable, Serializable {
  52: 
  53:     /** For serialization. */
  54:     private static final long serialVersionUID = 3390443360845711275L;
  55:     
  56:     /** The time period. */
  57:     private TimePeriod period;
  58: 
  59:     /** The value associated with the time period. */
  60:     private Number value;
  61: 
  62:     /**
  63:      * Constructs a new data item.
  64:      *
  65:      * @param period  the time period (<code>null</code> not permitted).
  66:      * @param value  the value associated with the time period.
  67:      * 
  68:      * @throws IllegalArgumentException if <code>period</code> is 
  69:      *     <code>null</code>.
  70:      */
  71:     public TimePeriodValue(TimePeriod period, Number value) {
  72:         if (period == null) {
  73:             throw new IllegalArgumentException("Null 'period' argument.");
  74:         }
  75:         this.period = period;
  76:         this.value = value;
  77:     }
  78: 
  79:     /**
  80:      * Constructs a new data item.
  81:      *
  82:      * @param period  the time period (<code>null</code> not permitted).
  83:      * @param value  the value associated with the time period.
  84:      * 
  85:      * @throws IllegalArgumentException if <code>period</code> is 
  86:      *     <code>null</code>.
  87:      */
  88:     public TimePeriodValue(TimePeriod period, double value) {
  89:         this(period, new Double(value));
  90:     }
  91: 
  92:     /**
  93:      * Returns the time period.
  94:      *
  95:      * @return The time period (never <code>null</code>).
  96:      */
  97:     public TimePeriod getPeriod() {
  98:         return this.period;
  99:     }
 100: 
 101:     /**
 102:      * Returns the value.
 103:      *
 104:      * @return The value (possibly <code>null</code>).
 105:      * 
 106:      * @see #setValue(Number)
 107:      */
 108:     public Number getValue() {
 109:         return this.value;
 110:     }
 111: 
 112:     /**
 113:      * Sets the value for this data item.
 114:      *
 115:      * @param value  the new value (<code>null</code> permitted).
 116:      * 
 117:      * @see #getValue()
 118:      */
 119:     public void setValue(Number value) {
 120:         this.value = value;
 121:     }
 122: 
 123:     /**
 124:      * Tests this object for equality with the target object.
 125:      *
 126:      * @param obj  the object (<code>null</code> permitted).
 127:      *
 128:      * @return A boolean.
 129:      */
 130:     public boolean equals(Object obj) {
 131:         if (this == obj) {
 132:             return true;
 133:         }
 134:         if (!(obj instanceof TimePeriodValue)) {
 135:             return false;
 136:         }
 137: 
 138:         TimePeriodValue timePeriodValue = (TimePeriodValue) obj;
 139: 
 140:         if (this.period != null ? !this.period.equals(timePeriodValue.period) 
 141:                 : timePeriodValue.period != null) {
 142:             return false;
 143:         }
 144:         if (this.value != null ? !this.value.equals(timePeriodValue.value) 
 145:                 : timePeriodValue.value != null) {
 146:             return false;
 147:         }
 148: 
 149:         return true;
 150:     }
 151: 
 152:     /**
 153:      * Returns a hash code value for the object.
 154:      *
 155:      * @return The hashcode
 156:      */
 157:     public int hashCode() {
 158:         int result;
 159:         result = (this.period != null ? this.period.hashCode() : 0);
 160:         result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
 161:         return result;
 162:     }
 163: 
 164:     /**
 165:      * Clones the object.
 166:      * <P>
 167:      * Note: no need to clone the period or value since they are immutable 
 168:      * classes.
 169:      *
 170:      * @return A clone.
 171:      */
 172:     public Object clone() {
 173:         Object clone = null;
 174:         try {
 175:             clone = super.clone();
 176:         }
 177:         catch (CloneNotSupportedException e) { // won't get here...
 178:             e.printStackTrace();
 179:         }
 180:         return clone;
 181:     }
 182: 
 183: }