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.ArrayList;
michael@0: import java.util.Collection;
michael@0: import java.util.Collections;
michael@0: import java.util.Iterator;
michael@0: import java.util.List;
michael@0: import java.util.ListIterator;
michael@0: import java.util.StringTokenizer;
michael@0:
michael@0: import net.fortuna.ical4j.model.parameter.Value;
michael@0:
michael@4: import org.apache.commons.lang3.builder.EqualsBuilder;
michael@4: import org.apache.commons.lang3.builder.HashCodeBuilder;
michael@0:
michael@0: /**
michael@0: * $Id$ [23-Apr-2004]
michael@0: *
michael@0: * Defines a list of iCalendar dates. If no value type is specified a list
michael@0: * defaults to DATE-TIME instances.
michael@0: * @author Ben Fortuna
michael@0: */
michael@0: public class DateList implements List, Serializable {
michael@0:
michael@0: private static final long serialVersionUID = -3700862452550012357L;
michael@0:
michael@0: private final Value type;
michael@0:
michael@0: private final List dates;
michael@0:
michael@0: private TimeZone timeZone;
michael@0:
michael@0: private boolean utc;
michael@0:
michael@0: /**
michael@0: * Default constructor.
michael@0: */
michael@0: public DateList() {
michael@0: this(false);
michael@0: }
michael@0:
michael@0: public DateList(final boolean unmodifiable) {
michael@0:
michael@0: this.type = Value.DATE_TIME;
michael@0: if (unmodifiable) {
michael@0: dates = Collections.EMPTY_LIST;
michael@0: }
michael@0: else {
michael@0: dates = new ArrayList();
michael@0: }
michael@0: }
michael@0:
michael@0: /**
michael@0: * @param aType the type of dates contained by the instance
michael@0: */
michael@0: public DateList(final Value aType) {
michael@0: this(aType, null);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Default constructor.
michael@0: *
michael@0: * @param aType
michael@0: * specifies the type of dates (either date or date-time)
michael@0: * @param timezone the timezone to apply to dates contained by the instance
michael@0: */
michael@0: public DateList(final Value aType, final TimeZone timezone) {
michael@0: if (aType == null) {
michael@0: this.type = Value.DATE_TIME;
michael@0: } else {
michael@0: this.type = aType;
michael@0: }
michael@0: this.timeZone = timezone;
michael@0: dates = new ArrayList();
michael@0: }
michael@0:
michael@0: /**
michael@0: * @param aValue a string representation of a date list
michael@0: * @param aType the date types contained in the instance
michael@0: * @throws ParseException where the specified string is not a valid date list
michael@0: */
michael@0: public DateList(final String aValue, final Value aType) throws ParseException {
michael@0: this(aValue, aType, null);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Parses the specified string representation to create a list of dates.
michael@0: *
michael@0: * @param aValue
michael@0: * a string representation of a list of dates
michael@0: * @param aType
michael@0: * specifies the type of dates (either date or date-time)
michael@0: * @param timezone the timezone to apply to contained dates
michael@0: * @throws ParseException
michael@0: * if an invalid date representation exists in the date list
michael@0: * string
michael@0: */
michael@0: public DateList(final String aValue, final Value aType, final TimeZone timezone)
michael@0: throws ParseException {
michael@0:
michael@0: this(aType, timezone);
michael@0: final StringTokenizer t = new StringTokenizer(aValue, ",");
michael@0: while (t.hasMoreTokens()) {
michael@0: if (Value.DATE.equals(type)) {
michael@0: add((Object) new Date(t.nextToken()));
michael@0: }
michael@0: else {
michael@0: add((Object) new DateTime(t.nextToken(), timezone));
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: /**
michael@0: * Constructs a new date list of the specified type containing
michael@0: * the dates in the specified list.
michael@0: * @param list a list of dates to include in the new list
michael@0: * @param type the type of the new list
michael@0: */
michael@0: public DateList(final DateList list, final Value type) {
michael@0: if (!Value.DATE.equals(type) && !Value.DATE_TIME.equals(type)) {
michael@0: throw new IllegalArgumentException(
michael@0: "Type must be either DATE or DATE-TIME");
michael@0: }
michael@0:
michael@0: this.type = type;
michael@0: dates = new ArrayList();
michael@0:
michael@0: if (Value.DATE.equals(type)) {
michael@0: for (final Iterator i = list.iterator(); i.hasNext();) {
michael@0: add(new Date((Date) i.next()));
michael@0: }
michael@0: }
michael@0: else {
michael@0: for (final Iterator i = list.iterator(); i.hasNext();) {
michael@0: add(new DateTime((Date) i.next()));
michael@0: }
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: /*
michael@0: * if (type != null && Value.DATE.equals(type)) {
michael@0: * b.append(DateFormat.getInstance().format((Date) i.next())); }
michael@0: * else { b.append(DateTimeFormat.getInstance().format((Date)
michael@0: * i.next(), isUtc())); }
michael@0: */
michael@0: b.append(i.next());
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 date to the list. The date will be updated to reflect the
michael@0: * timezone of this list.
michael@0: * @param date the date to add
michael@0: * @return true
michael@3: * @see java.util.List#add(Object)
michael@0: */
michael@0: public final boolean add(final Date date) {
michael@0: if (date instanceof DateTime) {
michael@0: if (isUtc()) {
michael@0: ((DateTime) date).setUtc(true);
michael@0: }
michael@0: else {
michael@0: ((DateTime) date).setTimeZone(getTimeZone());
michael@0: }
michael@0: }
michael@0: else if (!Value.DATE.equals(getType())) {
michael@0: final DateTime dateTime = new DateTime(date);
michael@0: dateTime.setTimeZone(getTimeZone());
michael@0: return add((Object) dateTime);
michael@0: }
michael@0: return add((Object) date);
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.Date
.
michael@0: * @param date the date to add
michael@0: * @return true if the object was added, otherwise false
michael@3: * @see java.util.List#add(Object)
michael@0: */
michael@0: public final boolean add(final Object date) {
michael@0: if (!(date instanceof Date)) {
michael@0: throw new IllegalArgumentException("Argument not a " + Date.class.getName());
michael@0: }
michael@0: return dates.add(date);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Remove a date from the list.
michael@0: *
michael@0: * @param date
michael@0: * the date to remove
michael@0: * @return true if the list contained the specified date
michael@0: * @see List#remove(java.lang.Object)
michael@0: */
michael@0: public final boolean remove(final Date date) {
michael@0: return remove((Object) date);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the VALUE parameter specifying the type of dates (ie. date or
michael@0: * date-time) stored in this date list.
michael@0: *
michael@0: * @return Returns a Value parameter.
michael@0: */
michael@0: public final Value getType() {
michael@0: return type;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Indicates whether this list is in local or UTC format. This property will
michael@0: * have no affect if the type of the list is not DATE-TIME.
michael@0: *
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: *
michael@0: * @param utc
michael@0: * The utc to set.
michael@0: */
michael@0: public final void setUtc(final boolean utc) {
michael@0: if (!Value.DATE.equals(type)) {
michael@0: for (final Iterator i = iterator(); i.hasNext();) {
michael@0: ((DateTime) i.next()).setUtc(utc);
michael@0: }
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 a timezone to apply to contained dates
michael@0: */
michael@0: public final void setTimeZone(final TimeZone timeZone) {
michael@0: if (!Value.DATE.equals(type)) {
michael@0: for (final Iterator i = iterator(); i.hasNext();) {
michael@0: ((DateTime) i.next()).setTimeZone(timeZone);
michael@0: }
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: public final void add(int arg0, Object arg1) {
michael@0: dates.add(arg0, arg1);
michael@0: }
michael@0:
michael@0: public final boolean addAll(Collection arg0) {
michael@0: return dates.addAll(arg0);
michael@0: }
michael@0:
michael@0: public final boolean addAll(int arg0, Collection arg1) {
michael@0: return dates.addAll(arg0, arg1);
michael@0: }
michael@0:
michael@0: public final void clear() {
michael@0: dates.clear();
michael@0: }
michael@0:
michael@0: public final boolean contains(Object o) {
michael@0: return dates.contains(o);
michael@0: }
michael@0:
michael@0: public final boolean containsAll(Collection arg0) {
michael@0: return dates.containsAll(arg0);
michael@0: }
michael@0:
michael@0: public final Object get(int index) {
michael@0: return dates.get(index);
michael@0: }
michael@0:
michael@0: public final int indexOf(Object o) {
michael@0: return dates.indexOf(o);
michael@0: }
michael@0:
michael@0: public final boolean isEmpty() {
michael@0: return dates.isEmpty();
michael@0: }
michael@0:
michael@0: public final Iterator iterator() {
michael@0: return dates.iterator();
michael@0: }
michael@0:
michael@0: public final int lastIndexOf(Object o) {
michael@0: return dates.lastIndexOf(o);
michael@0: }
michael@0:
michael@0: public final ListIterator listIterator() {
michael@0: return dates.listIterator();
michael@0: }
michael@0:
michael@0: public final ListIterator listIterator(int index) {
michael@0: return dates.listIterator(index);
michael@0: }
michael@0:
michael@0: public final Object remove(int index) {
michael@0: return dates.remove(index);
michael@0: }
michael@0:
michael@0: public final boolean remove(Object o) {
michael@0: return dates.remove(o);
michael@0: }
michael@0:
michael@0: public final boolean removeAll(Collection arg0) {
michael@0: return dates.removeAll(arg0);
michael@0: }
michael@0:
michael@0: public final boolean retainAll(Collection arg0) {
michael@0: return dates.retainAll(arg0);
michael@0: }
michael@0:
michael@0: public final Object set(int arg0, Object arg1) {
michael@0: return dates.set(arg0, arg1);
michael@0: }
michael@0:
michael@0: public final int size() {
michael@0: return dates.size();
michael@0: }
michael@0:
michael@0: public final List subList(int fromIndex, int toIndex) {
michael@0: return dates.subList(fromIndex, toIndex);
michael@0: }
michael@0:
michael@0: public final Object[] toArray() {
michael@0: return dates.toArray();
michael@0: }
michael@0:
michael@0: public final Object[] toArray(Object[] arg0) {
michael@0: return dates.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 DateList rhs = (DateList) obj;
michael@0: return new EqualsBuilder().append(dates, rhs.dates)
michael@0: .append(type, rhs.type)
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(dates)
michael@0: .append(type)
michael@0: .append(timeZone)
michael@0: .append(utc)
michael@0: .toHashCode();
michael@0: }
michael@0: }