michael@0: /** michael@0: * Copyright (c) 2012, Ben Fortuna michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * o Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * o Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * o Neither the name of Ben Fortuna nor the names of any other contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: package net.fortuna.ical4j.model; michael@0: michael@0: import java.io.Serializable; michael@0: import java.util.Date; michael@0: michael@0: /** michael@0: * @author fortuna michael@0: * michael@0: */ michael@0: public class DateRange implements Serializable { michael@0: michael@0: private static final long serialVersionUID = -7303846680559287286L; michael@0: michael@0: /** michael@0: * A flag indicating whether to include the start of the period in test functions. michael@0: */ michael@0: public static final int INCLUSIVE_START = 1; michael@0: michael@0: /** michael@0: * A flag indicating whether to include the end of the period in test functions. michael@0: */ michael@0: public static final int INCLUSIVE_END = 2; michael@0: michael@0: private final Date rangeStart; michael@0: michael@0: private final Date rangeEnd; michael@0: michael@0: /** michael@0: * @param start the start of the range michael@0: * @param end the end of the range michael@0: */ michael@0: public DateRange(Date start, Date end) { michael@0: if (start == null) { michael@0: throw new IllegalArgumentException("Range start is null"); michael@0: } michael@0: if (end == null) { michael@0: throw new IllegalArgumentException("Range end is null"); michael@0: } michael@0: if (end.before(start)) { michael@0: throw new IllegalArgumentException("Range start must be before range end"); michael@0: } michael@0: this.rangeStart = start; michael@0: this.rangeEnd = end; michael@0: } michael@0: michael@0: /** michael@0: * @return the rangeStart michael@0: */ michael@0: public Date getRangeStart() { michael@0: return rangeStart; michael@0: } michael@0: michael@0: /** michael@0: * @return the rangeEnd michael@0: */ michael@0: public Date getRangeEnd() { michael@0: return rangeEnd; michael@0: } michael@0: michael@0: /** michael@0: * Determines if the specified date occurs within this period (inclusive of michael@0: * period start and end). michael@0: * @param date a date to test for inclusion michael@0: * @return true if the specified date occurs within the current period michael@0: * michael@0: */ michael@0: public final boolean includes(final Date date) { michael@0: return includes(date, INCLUSIVE_START | INCLUSIVE_END); michael@0: } michael@0: michael@0: /** michael@0: * Decides whether a date falls within this period. michael@0: * @param date the date to be tested michael@0: * @param inclusiveMask specifies whether period start and end are included michael@0: * in the calculation michael@0: * @return true if the date is in the period, false otherwise michael@0: * @see Period#INCLUSIVE_START michael@0: * @see Period#INCLUSIVE_END michael@0: */ michael@0: public final boolean includes(final Date date, final int inclusiveMask) { michael@0: boolean includes = true; michael@0: if ((inclusiveMask & INCLUSIVE_START) > 0) { michael@0: includes = includes && !rangeStart.after(date); michael@0: } michael@0: else { michael@0: includes = includes && rangeStart.before(date); michael@0: } michael@0: if ((inclusiveMask & INCLUSIVE_END) > 0) { michael@0: includes = includes && !rangeEnd.before(date); michael@0: } michael@0: else { michael@0: includes = includes && rangeEnd.after(date); michael@0: } michael@0: return includes; michael@0: } michael@0: michael@0: /** michael@0: * Decides whether this period is completed before the given period starts. michael@0: * michael@0: * @param range michael@0: * a period that may or may not start after this period ends michael@0: * @return true if the specified period starts after this periods ends, michael@0: * otherwise false michael@0: */ michael@0: public final boolean before(final DateRange range) { michael@0: return (rangeEnd.before(range.getRangeStart())); michael@0: } michael@0: michael@0: /** michael@0: * Decides whether this period starts after the given period ends. michael@0: * michael@0: * @param range michael@0: * a period that may or may not end before this period starts michael@0: * @return true if the specified period end before this periods starts, michael@0: * otherwise false michael@0: */ michael@0: public final boolean after(final DateRange range) { michael@0: return (rangeStart.after(range.getRangeEnd())); michael@0: } michael@0: michael@0: /** michael@0: * Decides whether this period intersects with another one. michael@0: * michael@0: * @param range michael@0: * a possible intersecting period michael@0: * @return true if the specified period intersects this one, false michael@0: * otherwise. michael@0: */ michael@0: public final boolean intersects(final DateRange range) { michael@0: boolean intersects = false; michael@0: // Test for our start date in period michael@0: // (Exclude if it is the end date of test range) michael@0: if (range.includes(rangeStart) && !range.getRangeEnd().equals(rangeStart)) { michael@0: intersects = true; michael@0: } michael@0: // Test for test range's start date in our range michael@0: // (Exclude if it is the end date of our range) michael@0: else if (includes(range.getRangeStart()) michael@0: && !rangeEnd.equals(range.getRangeStart())) { michael@0: intersects = true; michael@0: } michael@0: return intersects; michael@0: } michael@0: michael@0: /** michael@0: * Decides whether these periods are serial without a gap. michael@0: * @param range a period to test for adjacency michael@0: * @return true if one period immediately follows the other, false otherwise michael@0: */ michael@0: public final boolean adjacent(final DateRange range) { michael@0: boolean adjacent = false; michael@0: if (rangeStart.equals(range.getRangeEnd())) { michael@0: adjacent = true; michael@0: } else if (rangeEnd.equals(range.getRangeStart())) { michael@0: adjacent = true; michael@0: } michael@0: return adjacent; michael@0: } michael@0: michael@0: /** michael@0: * Decides whether the given period is completely contained within this one. michael@0: * michael@0: * @param range michael@0: * the period that may be contained by this one michael@0: * @return true if this period covers all the dates of the specified period, michael@0: * otherwise false michael@0: */ michael@0: public final boolean contains(final DateRange range) { michael@0: // Test for period's start and end dates in our range michael@0: return (includes(range.getRangeStart()) && includes(range.getRangeEnd())); michael@0: } michael@0: }