001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * -------------------- 028 * TimePeriodValue.java 029 * -------------------- 030 * (C) Copyright 2003-2006, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * $Id: TimePeriodValue.java,v 1.4.2.2 2006/10/03 15:16:33 mungady Exp $ 036 * 037 * Changes 038 * ------- 039 * 22-Apr-2003 : Version 1 (DG); 040 * 03-Oct-2006 : Added null argument check to constructor (DG); 041 * 042 */ 043 044 package org.jfree.data.time; 045 046 import java.io.Serializable; 047 048 /** 049 * Represents a time period and an associated value. 050 */ 051 public class TimePeriodValue implements Cloneable, Serializable { 052 053 /** For serialization. */ 054 private static final long serialVersionUID = 3390443360845711275L; 055 056 /** The time period. */ 057 private TimePeriod period; 058 059 /** The value associated with the time period. */ 060 private Number value; 061 062 /** 063 * Constructs a new data item. 064 * 065 * @param period the time period (<code>null</code> not permitted). 066 * @param value the value associated with the time period. 067 * 068 * @throws IllegalArgumentException if <code>period</code> is 069 * <code>null</code>. 070 */ 071 public TimePeriodValue(TimePeriod period, Number value) { 072 if (period == null) { 073 throw new IllegalArgumentException("Null 'period' argument."); 074 } 075 this.period = period; 076 this.value = value; 077 } 078 079 /** 080 * Constructs a new data item. 081 * 082 * @param period the time period (<code>null</code> not permitted). 083 * @param value the value associated with the time period. 084 * 085 * @throws IllegalArgumentException if <code>period</code> is 086 * <code>null</code>. 087 */ 088 public TimePeriodValue(TimePeriod period, double value) { 089 this(period, new Double(value)); 090 } 091 092 /** 093 * Returns the time period. 094 * 095 * @return The time period (never <code>null</code>). 096 */ 097 public TimePeriod getPeriod() { 098 return this.period; 099 } 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 }