Source for org.jfree.data.time.DateRange

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, 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:  * DateRange.java
  29:  * --------------
  30:  * (C) Copyright 2002-2007, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   Bill Kelemen;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 22-Apr-2002 : Version 1 based on code by Bill Kelemen (DG);
  38:  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  39:  * 23-Sep-2003 : Minor Javadoc update (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.data.time;
  44: 
  45: import java.io.Serializable;
  46: import java.text.DateFormat;
  47: import java.util.Date;
  48: 
  49: import org.jfree.data.Range;
  50: 
  51: /**
  52:  * A range specified in terms of two <code>java.util.Date</code> objects.  
  53:  * Instances of this class are immutable.
  54:  */
  55: public class DateRange extends Range implements Serializable {
  56: 
  57:     /** For serialization. */
  58:     private static final long serialVersionUID = -4705682568375418157L;
  59:     
  60:     /** The lower bound for the range. */
  61:     private Date lowerDate;
  62: 
  63:     /** The upper bound for the range. */
  64:     private Date upperDate;
  65: 
  66:     /**
  67:      * Default constructor.
  68:      */
  69:     public DateRange() {
  70:         this(new Date(0), new Date(1));
  71:     }
  72: 
  73:     /**
  74:      * Constructs a new range.
  75:      *
  76:      * @param lower  the lower bound (<code>null</code> not permitted).
  77:      * @param upper  the upper bound (<code>null</code> not permitted).
  78:      */
  79:     public DateRange(Date lower, Date upper) {
  80: 
  81:         super(lower.getTime(), upper.getTime());
  82:         this.lowerDate = lower;
  83:         this.upperDate = upper;
  84: 
  85:     }
  86: 
  87:     /**
  88:      * Constructs a new range using two values that will be interpreted as 
  89:      * "milliseconds since midnight GMT, 1-Jan-1970".
  90:      *
  91:      * @param lower  the lower (oldest) date.
  92:      * @param upper  the upper (most recent) date.
  93:      */
  94:     public DateRange(double lower, double upper) {
  95:         super(lower, upper);
  96:         this.lowerDate = new Date((long) lower);
  97:         this.upperDate = new Date((long) upper);
  98:     }
  99: 
 100:     /**
 101:      * Constructs a new range that is based on another {@link Range}.  The 
 102:      * other range does not have to be a {@link DateRange}.  If it is not, the 
 103:      * upper and lower bounds are evaluated as milliseconds since midnight 
 104:      * GMT, 1-Jan-1970.
 105:      *
 106:      * @param other  the other range (<code>null</code> not permitted).
 107:      */
 108:     public DateRange(Range other) {
 109:         this(other.getLowerBound(), other.getUpperBound());
 110:     }
 111: 
 112:     /**
 113:      * Returns the lower (earlier) date for the range.
 114:      *
 115:      * @return The lower date for the range.
 116:      */
 117:     public Date getLowerDate() {
 118:         return this.lowerDate;
 119:     }
 120: 
 121:     /**
 122:      * Returns the upper (later) date for the range.
 123:      *
 124:      * @return The upper date for the range.
 125:      */
 126:     public Date getUpperDate() {
 127:         return this.upperDate;
 128:     }
 129:     
 130:     /**
 131:      * Returns a string representing the date range (useful for debugging).
 132:      * 
 133:      * @return A string representing the date range.
 134:      */
 135:     public String toString() {
 136:         DateFormat df = DateFormat.getDateTimeInstance();
 137:         return "[" + df.format(this.lowerDate) + " --> " 
 138:             + df.format(this.upperDate) + "]";
 139:     }
 140: 
 141: }