1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/model/DateRange.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,203 @@ 1.4 +/** 1.5 + * Copyright (c) 2012, Ben Fortuna 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1.12 + * o Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * o Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * o Neither the name of Ben Fortuna nor the names of any other contributors 1.20 + * may be used to endorse or promote products derived from this software 1.21 + * without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + */ 1.35 +package net.fortuna.ical4j.model; 1.36 + 1.37 +import java.io.Serializable; 1.38 +import java.util.Date; 1.39 + 1.40 +/** 1.41 + * @author fortuna 1.42 + * 1.43 + */ 1.44 +public class DateRange implements Serializable { 1.45 + 1.46 + private static final long serialVersionUID = -7303846680559287286L; 1.47 + 1.48 + /** 1.49 + * A flag indicating whether to include the start of the period in test functions. 1.50 + */ 1.51 + public static final int INCLUSIVE_START = 1; 1.52 + 1.53 + /** 1.54 + * A flag indicating whether to include the end of the period in test functions. 1.55 + */ 1.56 + public static final int INCLUSIVE_END = 2; 1.57 + 1.58 + private final Date rangeStart; 1.59 + 1.60 + private final Date rangeEnd; 1.61 + 1.62 + /** 1.63 + * @param start the start of the range 1.64 + * @param end the end of the range 1.65 + */ 1.66 + public DateRange(Date start, Date end) { 1.67 + if (start == null) { 1.68 + throw new IllegalArgumentException("Range start is null"); 1.69 + } 1.70 + if (end == null) { 1.71 + throw new IllegalArgumentException("Range end is null"); 1.72 + } 1.73 + if (end.before(start)) { 1.74 + throw new IllegalArgumentException("Range start must be before range end"); 1.75 + } 1.76 + this.rangeStart = start; 1.77 + this.rangeEnd = end; 1.78 + } 1.79 + 1.80 + /** 1.81 + * @return the rangeStart 1.82 + */ 1.83 + public Date getRangeStart() { 1.84 + return rangeStart; 1.85 + } 1.86 + 1.87 + /** 1.88 + * @return the rangeEnd 1.89 + */ 1.90 + public Date getRangeEnd() { 1.91 + return rangeEnd; 1.92 + } 1.93 + 1.94 + /** 1.95 + * Determines if the specified date occurs within this period (inclusive of 1.96 + * period start and end). 1.97 + * @param date a date to test for inclusion 1.98 + * @return true if the specified date occurs within the current period 1.99 + * 1.100 + */ 1.101 + public final boolean includes(final Date date) { 1.102 + return includes(date, INCLUSIVE_START | INCLUSIVE_END); 1.103 + } 1.104 + 1.105 + /** 1.106 + * Decides whether a date falls within this period. 1.107 + * @param date the date to be tested 1.108 + * @param inclusiveMask specifies whether period start and end are included 1.109 + * in the calculation 1.110 + * @return true if the date is in the period, false otherwise 1.111 + * @see Period#INCLUSIVE_START 1.112 + * @see Period#INCLUSIVE_END 1.113 + */ 1.114 + public final boolean includes(final Date date, final int inclusiveMask) { 1.115 + boolean includes = true; 1.116 + if ((inclusiveMask & INCLUSIVE_START) > 0) { 1.117 + includes = includes && !rangeStart.after(date); 1.118 + } 1.119 + else { 1.120 + includes = includes && rangeStart.before(date); 1.121 + } 1.122 + if ((inclusiveMask & INCLUSIVE_END) > 0) { 1.123 + includes = includes && !rangeEnd.before(date); 1.124 + } 1.125 + else { 1.126 + includes = includes && rangeEnd.after(date); 1.127 + } 1.128 + return includes; 1.129 + } 1.130 + 1.131 + /** 1.132 + * Decides whether this period is completed before the given period starts. 1.133 + * 1.134 + * @param range 1.135 + * a period that may or may not start after this period ends 1.136 + * @return true if the specified period starts after this periods ends, 1.137 + * otherwise false 1.138 + */ 1.139 + public final boolean before(final DateRange range) { 1.140 + return (rangeEnd.before(range.getRangeStart())); 1.141 + } 1.142 + 1.143 + /** 1.144 + * Decides whether this period starts after the given period ends. 1.145 + * 1.146 + * @param range 1.147 + * a period that may or may not end before this period starts 1.148 + * @return true if the specified period end before this periods starts, 1.149 + * otherwise false 1.150 + */ 1.151 + public final boolean after(final DateRange range) { 1.152 + return (rangeStart.after(range.getRangeEnd())); 1.153 + } 1.154 + 1.155 + /** 1.156 + * Decides whether this period intersects with another one. 1.157 + * 1.158 + * @param range 1.159 + * a possible intersecting period 1.160 + * @return true if the specified period intersects this one, false 1.161 + * otherwise. 1.162 + */ 1.163 + public final boolean intersects(final DateRange range) { 1.164 + boolean intersects = false; 1.165 + // Test for our start date in period 1.166 + // (Exclude if it is the end date of test range) 1.167 + if (range.includes(rangeStart) && !range.getRangeEnd().equals(rangeStart)) { 1.168 + intersects = true; 1.169 + } 1.170 + // Test for test range's start date in our range 1.171 + // (Exclude if it is the end date of our range) 1.172 + else if (includes(range.getRangeStart()) 1.173 + && !rangeEnd.equals(range.getRangeStart())) { 1.174 + intersects = true; 1.175 + } 1.176 + return intersects; 1.177 + } 1.178 + 1.179 + /** 1.180 + * Decides whether these periods are serial without a gap. 1.181 + * @param range a period to test for adjacency 1.182 + * @return true if one period immediately follows the other, false otherwise 1.183 + */ 1.184 + public final boolean adjacent(final DateRange range) { 1.185 + boolean adjacent = false; 1.186 + if (rangeStart.equals(range.getRangeEnd())) { 1.187 + adjacent = true; 1.188 + } else if (rangeEnd.equals(range.getRangeStart())) { 1.189 + adjacent = true; 1.190 + } 1.191 + return adjacent; 1.192 + } 1.193 + 1.194 + /** 1.195 + * Decides whether the given period is completely contained within this one. 1.196 + * 1.197 + * @param range 1.198 + * the period that may be contained by this one 1.199 + * @return true if this period covers all the dates of the specified period, 1.200 + * otherwise false 1.201 + */ 1.202 + public final boolean contains(final DateRange range) { 1.203 + // Test for period's start and end dates in our range 1.204 + return (includes(range.getRangeStart()) && includes(range.getRangeEnd())); 1.205 + } 1.206 +}