src/net/fortuna/ical4j/model/DateRange.java

Tue, 10 Feb 2015 19:58:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 19:58:00 +0100
changeset 4
45d57ecba757
permissions
-rw-r--r--

Upgrade the upgraded ical4j component to use org.apache.commons.lang3.

     1 /**
     2  * Copyright (c) 2012, Ben Fortuna
     3  * All rights reserved.
     4  *
     5  * Redistribution and use in source and binary forms, with or without
     6  * modification, are permitted provided that the following conditions
     7  * are met:
     8  *
     9  *  o Redistributions of source code must retain the above copyright
    10  * notice, this list of conditions and the following disclaimer.
    11  *
    12  *  o Redistributions in binary form must reproduce the above copyright
    13  * notice, this list of conditions and the following disclaimer in the
    14  * documentation and/or other materials provided with the distribution.
    15  *
    16  *  o Neither the name of Ben Fortuna nor the names of any other contributors
    17  * may be used to endorse or promote products derived from this software
    18  * without specific prior written permission.
    19  *
    20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31  */
    32 package net.fortuna.ical4j.model;
    34 import java.io.Serializable;
    35 import java.util.Date;
    37 /**
    38  * @author fortuna
    39  *
    40  */
    41 public class DateRange implements Serializable {
    43     private static final long serialVersionUID = -7303846680559287286L;
    45     /**
    46      * A flag indicating whether to include the start of the period in test functions.
    47      */
    48     public static final int INCLUSIVE_START = 1;
    50     /**
    51      * A flag indicating whether to include the end of the period in test functions.
    52      */
    53     public static final int INCLUSIVE_END = 2;
    55     private final Date rangeStart;
    57     private final Date rangeEnd;
    59     /**
    60      * @param start the start of the range
    61      * @param end the end of the range
    62      */
    63     public DateRange(Date start, Date end) {
    64         if (start == null) {
    65             throw new IllegalArgumentException("Range start is null");
    66         }
    67         if (end == null) {
    68             throw new IllegalArgumentException("Range end is null");
    69         }
    70         if (end.before(start)) {
    71             throw new IllegalArgumentException("Range start must be before range end");
    72         }
    73         this.rangeStart = start;
    74         this.rangeEnd = end;
    75     }
    77     /**
    78      * @return the rangeStart
    79      */
    80     public Date getRangeStart() {
    81         return rangeStart;
    82     }
    84     /**
    85      * @return the rangeEnd
    86      */
    87     public Date getRangeEnd() {
    88         return rangeEnd;
    89     }
    91     /**
    92      * Determines if the specified date occurs within this period (inclusive of
    93      * period start and end).
    94      * @param date a date to test for inclusion
    95      * @return true if the specified date occurs within the current period
    96      * 
    97      */
    98     public final boolean includes(final Date date) {
    99         return includes(date, INCLUSIVE_START | INCLUSIVE_END);
   100     }
   102     /**
   103      * Decides whether a date falls within this period.
   104      * @param date the date to be tested
   105      * @param inclusiveMask specifies whether period start and end are included
   106      * in the calculation
   107      * @return true if the date is in the period, false otherwise
   108      * @see Period#INCLUSIVE_START
   109      * @see Period#INCLUSIVE_END
   110      */
   111     public final boolean includes(final Date date, final int inclusiveMask) {
   112         boolean includes = true;
   113         if ((inclusiveMask & INCLUSIVE_START) > 0) {
   114             includes = includes && !rangeStart.after(date);
   115         }
   116         else {
   117             includes = includes && rangeStart.before(date);
   118         }
   119         if ((inclusiveMask & INCLUSIVE_END) > 0) {
   120             includes = includes && !rangeEnd.before(date);
   121         }
   122         else {
   123             includes = includes && rangeEnd.after(date);
   124         }
   125         return includes;
   126     }
   128     /**
   129      * Decides whether this period is completed before the given period starts.
   130      * 
   131      * @param range
   132      *            a period that may or may not start after this period ends
   133      * @return true if the specified period starts after this periods ends,
   134      *         otherwise false
   135      */
   136     public final boolean before(final DateRange range) {
   137         return (rangeEnd.before(range.getRangeStart()));
   138     }
   140     /**
   141      * Decides whether this period starts after the given period ends.
   142      * 
   143      * @param range
   144      *            a period that may or may not end before this period starts
   145      * @return true if the specified period end before this periods starts,
   146      *         otherwise false
   147      */
   148     public final boolean after(final DateRange range) {
   149         return (rangeStart.after(range.getRangeEnd()));
   150     }
   152     /**
   153      * Decides whether this period intersects with another one.
   154      * 
   155      * @param range
   156      *            a possible intersecting period
   157      * @return true if the specified period intersects this one, false
   158      *         otherwise.
   159      */
   160     public final boolean intersects(final DateRange range) {
   161         boolean intersects = false;
   162         // Test for our start date in period
   163         // (Exclude if it is the end date of test range)
   164         if (range.includes(rangeStart) && !range.getRangeEnd().equals(rangeStart)) {
   165             intersects = true;
   166         }
   167         // Test for test range's start date in our range
   168         // (Exclude if it is the end date of our range)
   169         else if (includes(range.getRangeStart())
   170                 && !rangeEnd.equals(range.getRangeStart())) {
   171             intersects = true;
   172         }
   173         return intersects;
   174     }
   176     /**
   177      * Decides whether these periods are serial without a gap.
   178      * @param range a period to test for adjacency
   179      * @return true if one period immediately follows the other, false otherwise
   180      */
   181     public final boolean adjacent(final DateRange range) {
   182         boolean adjacent = false;
   183         if (rangeStart.equals(range.getRangeEnd())) {
   184             adjacent = true;
   185         } else if (rangeEnd.equals(range.getRangeStart())) {
   186             adjacent = true;
   187         }
   188         return adjacent;
   189     }
   191     /**
   192      * Decides whether the given period is completely contained within this one.
   193      * 
   194      * @param range
   195      *            the period that may be contained by this one
   196      * @return true if this period covers all the dates of the specified period,
   197      *         otherwise false
   198      */
   199     public final boolean contains(final DateRange range) {
   200         // Test for period's start and end dates in our range
   201         return (includes(range.getRangeStart()) && includes(range.getRangeEnd()));
   202     }
   203 }

mercurial