DateSpan.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:6k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DateSpan.java,v 1.3 2005/10/10 18:02:46 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.calendar;
  22. import java.util.Date;
  23. /**
  24.  * An immutable representation of a time range.  The time range is
  25.  * internally represented as two longs. The methods that take and return
  26.  * <code>Date</code>s create the <code>Date</code>s as needed, so that
  27.  * if you modify returned <code>Date</code>s you will <b>not</b> effect
  28.  * the <code>DateSpan</code>.  The end points are inclusive.
  29.  *
  30.  * @version  $Revision: 1.3 $
  31.  */
  32. public class DateSpan {
  33.     private long _start;
  34.     private long _end;
  35.     /**
  36.      * Creates a <code>DateSpan</code> between the two end points.
  37.      *
  38.      * @param start Beginning date
  39.      * @param end Ending date
  40.      * @throws IllegalArgumentException if <code>start</code> is after
  41.      *         <code>end</code>
  42.      */
  43.     public DateSpan(long start, long end) {
  44.         _start = start;
  45.         _end = end;
  46.         if (_start > _end) {
  47.             throw new IllegalArgumentException(
  48.                              "Start date must be before end date");
  49.         }
  50.     }
  51.     /**
  52.      * Creates a <code>DateSpan</code> between the two end points.  This
  53.      * is a conveniance constructor that is equivalent to
  54.      * <code>new Date(start.getTime(), end.getTime());</code>.
  55.      *
  56.      * @param start Beginning date
  57.      * @param end Ending date
  58.      */
  59.     public DateSpan(Date start, Date end) {
  60.         this(start.getTime(), end.getTime());
  61.     }
  62.     /**
  63.      * Returns the start of the date span.
  64.      *
  65.      * @return start of the  span.
  66.      */
  67.     public long getStart() {
  68.         return _start;
  69.     }
  70.     /**
  71.      * Returns the end of the date span.
  72.      *
  73.      * @return end of the span.
  74.      */
  75.     public long getEnd() {
  76.         return _end;
  77.     }
  78.     /**
  79.      * Returns the start of the date span as a <code>Date</code>.
  80.      *
  81.      * @return start of the  span.
  82.      */
  83.     public Date getStartAsDate() {
  84.         return new Date(getStart());
  85.     }
  86.     /**
  87.      * Returns the end of the date span as a <code>Date</code>.
  88.      *
  89.      * @return end of the span.
  90.      */
  91.     public Date getEndAsDate() {
  92.         return new Date(getEnd());
  93.     }
  94.     /**
  95.      * Returns true if this <code>DateSpan</code> contains the specified
  96.      * <code>DateSpan</code>.
  97.      *
  98.      * @param span Date to check
  99.      * @return true if this DateSpan contains <code>span</code>.
  100.      */
  101.     public boolean contains(DateSpan span) {
  102.         return (contains(span.getStart()) && contains(span.getEnd()));
  103.     }
  104.     /**
  105.      * Returns whether or not this <code>DateSpan</code> contains the specified
  106.      * time.
  107.      *
  108.      * @param time time check
  109.      * @return true if this DateSpan contains <code>time</code>.
  110.      */
  111.     public boolean contains(long time) {
  112.         return (time >= getStart() && time <= getEnd());
  113.     }
  114.     /**
  115.      * Returns whether or not this <code>DateSpan</code> contains the
  116.      * specified date span.
  117.      *
  118.      * @param start Start of time span
  119.      * @param end End of time
  120.      * @return true if this <code>DateSpan</code> contains the specified
  121.      *         date span.
  122.      */
  123.     public boolean contains(long start, long end) {
  124.         return (start >= getStart() && end <= getEnd());
  125.     }
  126.     /**
  127.      * Returns true if the this <code>DateSpan</code> intersects with the
  128.      * specified time.
  129.      *
  130.      * @param start Start time
  131.      * @param end End time
  132.      * @return true if this <code>DateSpan</code> intersects with the specified
  133.      * time.
  134.      */
  135.     public boolean intersects(long start, long end) {
  136.         return (start <= getEnd() && end >= getStart());
  137.     }
  138.     /**
  139.      * Returns true if the this <code>DateSpan</code> intersects with the
  140.      * specified <code>DateSpan</code>.
  141.      *
  142.      * @param span DateSpan to compare to
  143.      * @return true if this <code>DateSpan</code> intersects with the specified
  144.      * time.
  145.      */
  146.     public boolean intersects(DateSpan span) {
  147.         return intersects(span.getStart(), span.getEnd());
  148.     }
  149.     /**
  150.      * Returns a new <code>DateSpan</code> that is the union of this
  151.      * <code>DateSpan</code> and <code>span</code>.
  152.      *
  153.      * @param span DateSpan to add
  154.      * @return union of this DateSpan and <code>span</code>
  155.      */
  156.     public DateSpan add(DateSpan span) {
  157.         return add(span.getStart(), span.getEnd());
  158.     }
  159.     /**
  160.      * Returns a new <code>DateSpan</code> that is the union of this
  161.      * <code>DateSpan</code> and the passed in span.
  162.      *
  163.      * @param start Start of region to add
  164.      * @param end End of region to end
  165.      * @return union of this DateSpan and <code>start</code>, <code>end</code>
  166.      */
  167.     public DateSpan add(long start, long end) {
  168.         return new DateSpan(Math.min(start, getStart()),
  169.                             Math.max(end, getEnd()));
  170.     }
  171.     /**
  172.      * {@inheritDoc}
  173.      */
  174.     @Override
  175.     public boolean equals(Object o) {
  176.         if (o == this) {
  177.             return true;
  178.         }
  179.         if (o instanceof DateSpan) {
  180.             DateSpan ds = (DateSpan)o;
  181.             return (_start == ds.getStart() && _end == ds.getEnd());
  182.         }
  183.         return false;
  184.     }
  185.     /**
  186.      * {@inheritDoc}
  187.      */
  188.     @Override
  189.     public int hashCode() {
  190.         int result = 17;
  191.         result = 37 * result + (int)(_start ^ (_start >>> 32));
  192.         result = 37 * result + (int)(_end ^ (_end >>> 32));
  193.         return result;
  194.     }
  195.     /**
  196.      * {@inheritDoc}
  197.      */
  198.     @Override
  199.     public String toString() {
  200.         return "DateSpan [" + getStartAsDate() + "-" + getEndAsDate() + "]";
  201.     }
  202. }