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