1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/model/Period.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,371 @@ 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.text.ParseException; 1.38 +import java.util.Date; 1.39 + 1.40 +import org.apache.commons.lang.builder.EqualsBuilder; 1.41 +import org.apache.commons.lang.builder.HashCodeBuilder; 1.42 + 1.43 +/** 1.44 + * $Id$ [Apr 14, 2004] 1.45 + * 1.46 + * Defines a period of time. A period may be specified as either a start date 1.47 + * and end date, or a start date and duration. NOTE: End dates and durations are 1.48 + * implicitly derived when not explicitly specified. This means that you cannot 1.49 + * rely on the returned values from the getters to deduce whether a period has 1.50 + * an explicit end date or duration. 1.51 + * 1.52 + * @author Ben Fortuna 1.53 + */ 1.54 +public class Period extends DateRange implements Comparable { 1.55 + 1.56 + private static final long serialVersionUID = 7321090422911676490L; 1.57 + 1.58 + private Dur duration; 1.59 + 1.60 + /** 1.61 + * Constructor. 1.62 + * 1.63 + * @param aValue 1.64 + * a string representation of a period 1.65 + * @throws ParseException 1.66 + * where the specified string is not a valid representation 1.67 + */ 1.68 + public Period(final String aValue) throws ParseException { 1.69 + super(parseStartDate(aValue), parseEndDate(aValue, true)); 1.70 + 1.71 + // period may end in either a date-time or a duration.. 1.72 + try { 1.73 + parseEndDate(aValue, false); 1.74 + } 1.75 + catch (ParseException pe) { 1.76 + // duration = DurationFormat.getInstance().parse(aValue); 1.77 + duration = parseDuration(aValue); 1.78 + } 1.79 + normalise(); 1.80 + } 1.81 + 1.82 + /** 1.83 + * Constructs a new period with the specied start and end date. 1.84 + * 1.85 + * @param start 1.86 + * the start date of the period 1.87 + * @param end 1.88 + * the end date of the period 1.89 + */ 1.90 + public Period(final DateTime start, final DateTime end) { 1.91 + super(start, end); 1.92 + normalise(); 1.93 + } 1.94 + 1.95 + /** 1.96 + * Constructs a new period with the specified start date and duration. 1.97 + * 1.98 + * @param start 1.99 + * the start date of the period 1.100 + * @param duration 1.101 + * the duration of the period 1.102 + */ 1.103 + public Period(final DateTime start, final Dur duration) { 1.104 + super(start, new DateTime(duration.getTime(start))); 1.105 + this.duration = duration; 1.106 + normalise(); 1.107 + } 1.108 + 1.109 + private static DateTime parseStartDate(String value) throws ParseException { 1.110 + return new DateTime(value.substring(0, value.indexOf('/'))); 1.111 + } 1.112 + 1.113 + private static DateTime parseEndDate(String value, boolean resolve) throws ParseException { 1.114 + DateTime end = null; 1.115 + try { 1.116 + end = new DateTime(value.substring(value.indexOf('/') + 1)); 1.117 + } 1.118 + catch (ParseException e) { 1.119 + if (resolve) { 1.120 + final Dur duration = parseDuration(value); 1.121 + end = new DateTime(duration.getTime(parseStartDate(value))); 1.122 + } 1.123 + else { 1.124 + throw e; 1.125 + } 1.126 + } 1.127 + return end; 1.128 + } 1.129 + 1.130 + private static Dur parseDuration(String value) { 1.131 + return new Dur(value.substring(value.indexOf('/') + 1)); 1.132 + } 1.133 + 1.134 + private void normalise() { 1.135 + // ensure the end timezone is the same as the start.. 1.136 + if (getStart().isUtc()) { 1.137 + getEnd().setUtc(true); 1.138 + } 1.139 + else { 1.140 + getEnd().setTimeZone(getStart().getTimeZone()); 1.141 + } 1.142 + } 1.143 + 1.144 + /** 1.145 + * Returns the duration of this period. If an explicit duration is not 1.146 + * specified, the duration is derived from the end date. 1.147 + * 1.148 + * @return the duration of this period in milliseconds. 1.149 + */ 1.150 + public final Dur getDuration() { 1.151 + if (duration == null) { 1.152 + return new Dur(getStart(), getEnd()); 1.153 + } 1.154 + return duration; 1.155 + } 1.156 + 1.157 + /** 1.158 + * Returns the end date of this period. If an explicit end date is not 1.159 + * specified, the end date is derived from the duration. 1.160 + * 1.161 + * @return the end date of this period. 1.162 + */ 1.163 + public final DateTime getEnd() { 1.164 + return (DateTime) getRangeEnd(); 1.165 + } 1.166 + 1.167 + /** 1.168 + * @return Returns the start. 1.169 + */ 1.170 + public final DateTime getStart() { 1.171 + return (DateTime) getRangeStart(); 1.172 + } 1.173 + 1.174 + /** 1.175 + * @param date a date to test for inclusion 1.176 + * @param inclusive indicates if the start and end of the period are included in the test 1.177 + * @return true if the specified date occurs within the current period 1.178 + * @deprecated use {@link Period#includes(Date, int)} instead. 1.179 + */ 1.180 + public final boolean includes(final Date date, final boolean inclusive) { 1.181 + if (inclusive) { 1.182 + return includes(date, INCLUSIVE_START | INCLUSIVE_END); 1.183 + } 1.184 + else { 1.185 + return includes(date, 0); 1.186 + } 1.187 + } 1.188 + 1.189 + /** 1.190 + * Creates a period that encompasses both this period and another one. If 1.191 + * the other period is null, return a copy of this period. NOTE: Resulting 1.192 + * periods are specified by explicitly setting a start date and end date 1.193 + * (i.e. durations are implied). 1.194 + * 1.195 + * @param period 1.196 + * the period to add to this one 1.197 + * @return a period 1.198 + */ 1.199 + public final Period add(final Period period) { 1.200 + DateTime newPeriodStart = null; 1.201 + DateTime newPeriodEnd = null; 1.202 + 1.203 + if (period == null) { 1.204 + newPeriodStart = getStart(); 1.205 + newPeriodEnd = getEnd(); 1.206 + } 1.207 + else { 1.208 + if (getStart().before(period.getStart())) { 1.209 + newPeriodStart = getStart(); 1.210 + } 1.211 + else { 1.212 + newPeriodStart = period.getStart(); 1.213 + } 1.214 + if (getEnd().after(period.getEnd())) { 1.215 + newPeriodEnd = getEnd(); 1.216 + } 1.217 + else { 1.218 + newPeriodEnd = period.getEnd(); 1.219 + } 1.220 + } 1.221 + 1.222 + return new Period(newPeriodStart, newPeriodEnd); 1.223 + } 1.224 + 1.225 + /** 1.226 + * Creates a set of periods resulting from the subtraction of the specified 1.227 + * period from this one. If the specified period is completely contained 1.228 + * in this period, the resulting list will contain two periods. Otherwise 1.229 + * it will contain one. If the specified period does not interest this period 1.230 + * a list containing this period is returned. If this period is completely 1.231 + * contained within the specified period an empty period list is returned. 1.232 + * @param period a period to subtract from this one 1.233 + * @return a list containing zero, one or two periods. 1.234 + */ 1.235 + public final PeriodList subtract(final Period period) { 1.236 + final PeriodList result = new PeriodList(); 1.237 + 1.238 + if (period.contains(this)) { 1.239 + return result; 1.240 + } 1.241 + else if (!period.intersects(this)) { 1.242 + result.add(this); 1.243 + return result; 1.244 + } 1.245 + 1.246 + DateTime newPeriodStart; 1.247 + DateTime newPeriodEnd; 1.248 + if (!period.getStart().after(getStart())) { 1.249 + newPeriodStart = period.getEnd(); 1.250 + newPeriodEnd = getEnd(); 1.251 + } 1.252 + else if (!period.getEnd().before(getEnd())) { 1.253 + newPeriodStart = getStart(); 1.254 + newPeriodEnd = period.getStart(); 1.255 + } 1.256 + else { 1.257 + // subtraction consumed by this period.. 1.258 + // initialise and add head period.. 1.259 + newPeriodStart = getStart(); 1.260 + newPeriodEnd = period.getStart(); 1.261 + result.add(new Period(newPeriodStart, newPeriodEnd)); 1.262 + // initialise tail period.. 1.263 + newPeriodStart = period.getEnd(); 1.264 + newPeriodEnd = getEnd(); 1.265 + } 1.266 + result.add(new Period(newPeriodStart, newPeriodEnd)); 1.267 + return result; 1.268 + } 1.269 + 1.270 + /** 1.271 + * An empty period is one that consumes no time. 1.272 + * @return true if this period consumes no time, otherwise false 1.273 + */ 1.274 + public final boolean isEmpty() { 1.275 + return getStart().equals(getEnd()); 1.276 + } 1.277 + 1.278 + /** 1.279 + * Updates the start and (possible) end times of this period to reflect 1.280 + * the specified UTC timezone status. 1.281 + * @param utc indicates whether the period is in UTC time 1.282 + */ 1.283 + public void setUtc(final boolean utc) { 1.284 + getStart().setUtc(utc); 1.285 + getEnd().setUtc(utc); 1.286 + } 1.287 + 1.288 + /** 1.289 + * Updates the start and (possible) end times of this period to reflect 1.290 + * the specified timezone status. 1.291 + * @param timezone a timezone for the period 1.292 + */ 1.293 + public final void setTimeZone(final TimeZone timezone) { 1.294 + getStart().setUtc(false); 1.295 + getStart().setTimeZone(timezone); 1.296 + getEnd().setUtc(false); 1.297 + getEnd().setTimeZone(timezone); 1.298 + } 1.299 + 1.300 + /** 1.301 + * {@inheritDoc} 1.302 + */ 1.303 + public final String toString() { 1.304 + final StringBuffer b = new StringBuffer(); 1.305 + b.append(getStart()); 1.306 + b.append('/'); 1.307 + if (duration == null) { 1.308 + b.append(getEnd()); 1.309 + } 1.310 + else { 1.311 + // b.append(DurationFormat.getInstance().format(duration)); 1.312 + b.append(duration); 1.313 + } 1.314 + return b.toString(); 1.315 + } 1.316 + 1.317 + /** 1.318 + * {@inheritDoc} 1.319 + */ 1.320 + public final int compareTo(final Object arg0) { 1.321 + return compareTo((Period) arg0); 1.322 + } 1.323 + 1.324 + /** 1.325 + * Compares the specified period with this period. 1.326 + * 1.327 + * @param arg0 a period to compare with this one 1.328 + * @return a postive value if this period is greater, negative if the other is 1.329 + * greater, or zero if they are equal 1.330 + */ 1.331 + public final int compareTo(final Period arg0) { 1.332 + // Throws documented exception if type is wrong or parameter is null 1.333 + if (arg0 == null) { 1.334 + throw new ClassCastException("Cannot compare this object to null"); 1.335 + } 1.336 + final int startCompare = getStart().compareTo(arg0.getStart()); 1.337 + if (startCompare != 0) { 1.338 + return startCompare; 1.339 + } 1.340 + // start dates are equal, compare end dates.. 1.341 + else if (duration == null) { 1.342 + final int endCompare = getEnd().compareTo(arg0.getEnd()); 1.343 + if (endCompare != 0) { 1.344 + return endCompare; 1.345 + } 1.346 + } 1.347 + // ..or durations 1.348 + return getDuration().compareTo(arg0.getDuration()); 1.349 + } 1.350 + 1.351 + /** 1.352 + * {@inheritDoc} 1.353 + */ 1.354 + public final boolean equals(final Object o) { 1.355 + if (this == o) { 1.356 + return true; 1.357 + } 1.358 + if (!(o instanceof Period)) { 1.359 + return false; 1.360 + } 1.361 + 1.362 + final Period period = (Period) o; 1.363 + return new EqualsBuilder().append(getStart(), period.getStart()) 1.364 + .append(getEnd(), period.getEnd()).isEquals(); 1.365 + } 1.366 + 1.367 + /** 1.368 + * {@inheritDoc} 1.369 + */ 1.370 + public final int hashCode() { 1.371 + return new HashCodeBuilder().append(getStart()) 1.372 + .append((duration == null) ? (Object) getEnd() : duration).toHashCode(); 1.373 + } 1.374 +}