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.text.ParseException; michael@0: import java.util.Collection; michael@0: import java.util.Collections; michael@0: import java.util.Iterator; michael@0: import java.util.Set; michael@0: import java.util.StringTokenizer; michael@0: import java.util.TreeSet; michael@0: michael@0: import org.apache.commons.lang.builder.EqualsBuilder; michael@0: import org.apache.commons.lang.builder.HashCodeBuilder; michael@0: michael@0: /** michael@0: * $Id$ [23-Apr-2004] michael@0: * michael@0: * Defines a list of iCalendar periods. NOTE: By implementing the michael@0: * java.util.SortedSet interface period lists will always be michael@0: * sorted according to natural ordering. michael@0: * michael@0: * @author Ben Fortuna michael@0: */ michael@0: public class PeriodList implements Set, Serializable { michael@0: michael@0: private static final long serialVersionUID = -2317587285790834492L; michael@0: michael@0: private final Set periods; michael@0: michael@0: private TimeZone timezone; michael@0: michael@0: private boolean utc; michael@0: michael@0: private final boolean unmodifiable; michael@0: michael@0: /** michael@0: * Default constructor. michael@0: */ michael@0: public PeriodList() { michael@0: this(true); michael@0: } michael@0: michael@0: /** michael@0: * @param utc indicates whether the period list is in UTC time michael@0: */ michael@0: public PeriodList(boolean utc) { michael@0: this(utc, false); michael@0: } michael@0: michael@0: /** michael@0: * @param utc indicates whether the period list is in UTC time michael@0: */ michael@0: public PeriodList(boolean utc, final boolean unmodifiable) { michael@0: this.utc = utc; michael@0: this.unmodifiable = unmodifiable; michael@0: if (unmodifiable) { michael@0: periods = Collections.EMPTY_SET; michael@0: } michael@0: else { michael@0: periods = new TreeSet(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Parses the specified string representation to create a list of periods. michael@0: * michael@0: * @param aValue michael@0: * a string representation of a list of periods michael@0: * @throws ParseException michael@0: * thrown when an invalid string representation of a period list michael@0: * is specified michael@0: */ michael@0: public PeriodList(final String aValue) throws ParseException { michael@0: this(); michael@0: final StringTokenizer t = new StringTokenizer(aValue, ","); michael@0: while (t.hasMoreTokens()) { michael@0: add((Object) new Period(t.nextToken())); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final String toString() { michael@0: final StringBuffer b = new StringBuffer(); michael@0: for (final Iterator i = iterator(); i.hasNext();) { michael@0: b.append(i.next().toString()); michael@0: if (i.hasNext()) { michael@0: b.append(','); michael@0: } michael@0: } michael@0: return b.toString(); michael@0: } michael@0: michael@0: /** michael@0: * Add a period to the list. michael@0: * michael@0: * @param period michael@0: * the period to add michael@0: * @return true michael@0: * @see java.util.List#add(java.lang.Object) michael@0: */ michael@0: public final boolean add(final Period period) { michael@0: if (isUtc()) { michael@0: period.setUtc(true); michael@0: } michael@0: else { michael@0: period.setTimeZone(timezone); michael@0: } michael@0: return add((Object) period); michael@0: } michael@0: michael@0: /** michael@0: * Overrides superclass to throw an IllegalArgumentException michael@0: * where argument is not a net.fortuna.ical4j.model.Period. michael@0: * @param period a period to add to the list michael@0: * @return true if the period was added, otherwise false michael@3: * @see java.util.List#add(Object) michael@0: */ michael@0: public final boolean add(final Object period) { michael@0: if (!(period instanceof Period)) { michael@0: throw new IllegalArgumentException("Argument not a " michael@0: + Period.class.getName()); michael@0: } michael@0: return periods.add(period); michael@0: } michael@0: michael@0: /** michael@0: * Remove a period from the list. michael@0: * michael@0: * @param period michael@0: * the period to remove michael@0: * @return true if the list contained the specified period michael@0: * @see java.util.List#remove(java.lang.Object) michael@0: */ michael@0: public final boolean remove(final Period period) { michael@0: return remove((Object) period); michael@0: } michael@0: michael@0: /** michael@0: * Returns a normalised version of this period list. Normalisation includes michael@0: * combining overlapping periods, removing periods contained by other michael@0: * periods, combining adjacent periods, and removing periods that consume michael@0: * no time. NOTE: If the period list is michael@0: * already normalised then this period list is returned. michael@0: * michael@0: * @return a period list michael@0: */ michael@0: public final PeriodList normalise() { michael@0: Period prevPeriod = null; michael@0: Period period = null; michael@0: final PeriodList newList = new PeriodList(isUtc()); michael@0: if (timezone != null) { michael@0: newList.setTimeZone(timezone); michael@0: } michael@0: boolean normalised = false; michael@0: for (final Iterator i = iterator(); i.hasNext();) { michael@0: period = (Period) i.next(); michael@0: if (period.isEmpty()) { michael@0: period = prevPeriod; michael@0: normalised = true; michael@0: } michael@0: else if (prevPeriod != null) { michael@0: // ignore periods contained by other periods.. michael@0: if (prevPeriod.contains(period)) { michael@0: period = prevPeriod; michael@0: normalised = true; michael@0: } michael@0: // combine intersecting periods.. michael@0: else if (prevPeriod.intersects(period)) { michael@0: period = prevPeriod.add(period); michael@0: normalised = true; michael@0: } michael@0: // combine adjacent periods.. michael@0: else if (prevPeriod.adjacent(period)) { michael@0: period = prevPeriod.add(period); michael@0: normalised = true; michael@0: } michael@0: else { michael@0: // if current period is recognised as distinct michael@0: // from previous period, add the previous period michael@0: // to the list.. michael@0: newList.add(prevPeriod); michael@0: } michael@0: } michael@0: prevPeriod = period; michael@0: } michael@0: // remember to add the last period to the list.. michael@0: if (prevPeriod != null) { michael@0: newList.add(prevPeriod); michael@0: } michael@0: // only return new list if normalisation michael@0: // has ocurred.. michael@0: if (normalised) { michael@0: return newList; michael@0: } michael@0: else { michael@0: return this; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * A convenience method that combines all the periods in the specified list to michael@0: * this list. The result returned is a new PeriodList instance, except where michael@0: * no periods are specified in the arguments. In such cases this instance is returned. michael@0: * michael@0: * Normalisation is also performed automatically after all periods have been added. michael@0: * michael@0: * @param periods a list of periods to add michael@0: * @return a period list instance michael@0: */ michael@0: public final PeriodList add(final PeriodList periods) { michael@0: if (periods != null) { michael@0: final PeriodList newList = new PeriodList(); michael@0: newList.addAll(this); michael@0: for (final Iterator i = periods.iterator(); i.hasNext();) { michael@0: newList.add((Period) i.next()); michael@0: } michael@0: return newList.normalise(); michael@0: } michael@0: return this; michael@0: } michael@0: michael@0: /** michael@0: * Subtracts the intersection of this list with the specified list of michael@0: * periods from this list and returns the results as a new period list. If michael@0: * no intersection is identified this list is returned. michael@0: * michael@0: * @param subtractions michael@0: * a list of periods to subtract from this list michael@0: * @return a period list michael@0: */ michael@0: public final PeriodList subtract(final PeriodList subtractions) { michael@0: if (subtractions == null || subtractions.isEmpty()) { michael@0: return this; michael@0: } michael@0: michael@0: PeriodList result = this; michael@0: PeriodList tmpResult = new PeriodList(); michael@0: michael@0: for (final Iterator i = subtractions.iterator(); i.hasNext();) { michael@0: final Period subtraction = (Period) i.next(); michael@0: for (final Iterator j = result.iterator(); j.hasNext();) { michael@0: final Period period = (Period) j.next(); michael@0: tmpResult.addAll(period.subtract(subtraction)); michael@0: } michael@0: result = tmpResult; michael@0: tmpResult = new PeriodList(); michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: public final boolean isUnmodifiable() { michael@0: return unmodifiable; michael@0: } michael@0: michael@0: /** michael@0: * Indicates whether this list is in local or UTC format. michael@0: * @return Returns true if in UTC format, otherwise false. michael@0: */ michael@0: public final boolean isUtc() { michael@0: return utc; michael@0: } michael@0: michael@0: /** michael@0: * Sets whether this list is in UTC or local time format. michael@0: * @param utc The utc to set. michael@0: */ michael@0: public final void setUtc(final boolean utc) { michael@0: for (final Iterator i = iterator(); i.hasNext();) { michael@0: final Period period = (Period) i.next(); michael@0: period.setUtc(utc); michael@0: } michael@0: this.timezone = null; michael@0: this.utc = utc; michael@0: } michael@0: michael@0: /** michael@0: * Applies the specified timezone to all dates in the list. michael@0: * All dates added to this list will also have this timezone michael@0: * applied. michael@0: * @param timeZone the timezone for the period list michael@0: */ michael@0: public final void setTimeZone(final TimeZone timeZone) { michael@0: for (final Iterator i = iterator(); i.hasNext();) { michael@0: final Period period = (Period) i.next(); michael@0: period.setTimeZone(timeZone); michael@0: } michael@0: this.timezone = timeZone; michael@0: this.utc = false; michael@0: } michael@0: michael@0: /** michael@0: * @return Returns the timeZone. michael@0: */ michael@0: public final TimeZone getTimeZone() { michael@0: return timezone; michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final boolean addAll(Collection arg0) { michael@0: for (Iterator i = arg0.iterator(); i.hasNext();) { michael@0: add(i.next()); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final void clear() { michael@0: periods.clear(); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final boolean contains(Object o) { michael@0: return periods.contains(o); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final boolean containsAll(Collection arg0) { michael@0: return periods.containsAll(arg0); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final boolean isEmpty() { michael@0: return periods.isEmpty(); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final Iterator iterator() { michael@0: return periods.iterator(); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final boolean remove(Object o) { michael@0: return periods.remove(o); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final boolean removeAll(Collection arg0) { michael@0: return periods.removeAll(arg0); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final boolean retainAll(Collection arg0) { michael@0: return periods.retainAll(arg0); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final int size() { michael@0: return periods.size(); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final Object[] toArray() { michael@0: return periods.toArray(); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final Object[] toArray(Object[] arg0) { michael@0: return periods.toArray(arg0); michael@0: } michael@0: michael@0: public final boolean equals(Object obj) { michael@0: if (!getClass().isAssignableFrom(obj.getClass())) { michael@0: return false; michael@0: } michael@0: final PeriodList rhs = (PeriodList) obj; michael@0: return new EqualsBuilder().append(periods, rhs.periods) michael@0: .append(timezone, rhs.timezone) michael@0: .append(utc, utc) michael@0: .isEquals(); michael@0: } michael@0: michael@0: public final int hashCode() { michael@0: return new HashCodeBuilder().append(periods) michael@0: .append(timezone) michael@0: .append(utc) michael@0: .toHashCode(); michael@0: } michael@0: }