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

Tue, 10 Feb 2015 18:12:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 18:12:00 +0100
changeset 0
fb9019fb1bf7
permissions
-rw-r--r--

Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.

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

mercurial