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

changeset 0
fb9019fb1bf7
child 3
73bdfa70b04e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/net/fortuna/ical4j/model/DateList.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,409 @@
     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.text.ParseException;
    1.39 +import java.util.ArrayList;
    1.40 +import java.util.Collection;
    1.41 +import java.util.Collections;
    1.42 +import java.util.Iterator;
    1.43 +import java.util.List;
    1.44 +import java.util.ListIterator;
    1.45 +import java.util.StringTokenizer;
    1.46 +
    1.47 +import net.fortuna.ical4j.model.parameter.Value;
    1.48 +
    1.49 +import org.apache.commons.lang.builder.EqualsBuilder;
    1.50 +import org.apache.commons.lang.builder.HashCodeBuilder;
    1.51 +
    1.52 +/**
    1.53 + * $Id$ [23-Apr-2004]
    1.54 + *
    1.55 + * Defines a list of iCalendar dates. If no value type is specified a list
    1.56 + * defaults to DATE-TIME instances.
    1.57 + * @author Ben Fortuna
    1.58 + */
    1.59 +public class DateList implements List, Serializable {
    1.60 +
    1.61 +	private static final long serialVersionUID = -3700862452550012357L;
    1.62 +
    1.63 +	private final Value type;
    1.64 +    
    1.65 +    private final List dates;
    1.66 +
    1.67 +    private TimeZone timeZone;
    1.68 +    
    1.69 +    private boolean utc;
    1.70 +
    1.71 +    /**
    1.72 +     * Default constructor.
    1.73 +     */
    1.74 +    public DateList() {
    1.75 +    	this(false);
    1.76 +    }
    1.77 +
    1.78 +    public DateList(final boolean unmodifiable) {
    1.79 +    
    1.80 +        this.type = Value.DATE_TIME;
    1.81 +        if (unmodifiable) {
    1.82 +        	dates = Collections.EMPTY_LIST;
    1.83 +        }
    1.84 +        else {
    1.85 +            dates = new ArrayList();
    1.86 +        }
    1.87 +    }
    1.88 +
    1.89 +    /**
    1.90 +     * @param aType the type of dates contained by the instance
    1.91 +     */
    1.92 +    public DateList(final Value aType) {
    1.93 +        this(aType, null);
    1.94 +    }
    1.95 +    
    1.96 +    /**
    1.97 +     * Default constructor.
    1.98 +     * 
    1.99 +     * @param aType
   1.100 +     *            specifies the type of dates (either date or date-time)
   1.101 +     * @param timezone the timezone to apply to dates contained by the instance
   1.102 +     */
   1.103 +    public DateList(final Value aType, final TimeZone timezone) {
   1.104 +        if (aType == null) {
   1.105 +            this.type = Value.DATE_TIME;
   1.106 +        } else {
   1.107 +            this.type = aType;
   1.108 +        }
   1.109 +        this.timeZone = timezone;
   1.110 +        dates = new ArrayList();
   1.111 +    }
   1.112 +
   1.113 +    /**
   1.114 +     * @param aValue a string representation of a date list
   1.115 +     * @param aType the date types contained in the instance
   1.116 +     * @throws ParseException where the specified string is not a valid date list
   1.117 +     */
   1.118 +    public DateList(final String aValue, final Value aType) throws ParseException {
   1.119 +        this(aValue, aType, null);
   1.120 +    }
   1.121 +    
   1.122 +    /**
   1.123 +     * Parses the specified string representation to create a list of dates.
   1.124 +     * 
   1.125 +     * @param aValue
   1.126 +     *            a string representation of a list of dates
   1.127 +     * @param aType
   1.128 +     *            specifies the type of dates (either date or date-time)
   1.129 +     * @param timezone the timezone to apply to contained dates
   1.130 +     * @throws ParseException
   1.131 +     *             if an invalid date representation exists in the date list
   1.132 +     *             string
   1.133 +     */
   1.134 +    public DateList(final String aValue, final Value aType, final TimeZone timezone)
   1.135 +            throws ParseException {
   1.136 +    	
   1.137 +        this(aType, timezone);
   1.138 +        final StringTokenizer t = new StringTokenizer(aValue, ",");
   1.139 +        while (t.hasMoreTokens()) {
   1.140 +            if (Value.DATE.equals(type)) {
   1.141 +                add((Object) new Date(t.nextToken()));
   1.142 +            }
   1.143 +            else {
   1.144 +                add((Object) new DateTime(t.nextToken(), timezone));
   1.145 +            }
   1.146 +        }
   1.147 +    }
   1.148 +    
   1.149 +    /**
   1.150 +     * Constructs a new date list of the specified type containing
   1.151 +     * the dates in the specified list.
   1.152 +     * @param list a list of dates to include in the new list
   1.153 +     * @param type the type of the new list
   1.154 +     */
   1.155 +    public DateList(final DateList list, final Value type) {
   1.156 +        if (!Value.DATE.equals(type) && !Value.DATE_TIME.equals(type)) {
   1.157 +            throw new IllegalArgumentException(
   1.158 +                    "Type must be either DATE or DATE-TIME");
   1.159 +        }
   1.160 +        
   1.161 +        this.type = type;
   1.162 +        dates = new ArrayList();
   1.163 +        
   1.164 +        if (Value.DATE.equals(type)) {
   1.165 +            for (final Iterator i = list.iterator(); i.hasNext();) {
   1.166 +                add(new Date((Date) i.next()));
   1.167 +            }
   1.168 +        }
   1.169 +        else {
   1.170 +            for (final Iterator i = list.iterator(); i.hasNext();) {
   1.171 +                add(new DateTime((Date) i.next()));
   1.172 +            }
   1.173 +        }
   1.174 +    }
   1.175 +
   1.176 +    /**
   1.177 +     * {@inheritDoc}
   1.178 +     */
   1.179 +    public final String toString() {
   1.180 +        final StringBuffer b = new StringBuffer();
   1.181 +        for (final Iterator i = iterator(); i.hasNext();) {
   1.182 +            /*
   1.183 +             * if (type != null && Value.DATE.equals(type)) {
   1.184 +             * b.append(DateFormat.getInstance().format((Date) i.next())); }
   1.185 +             * else { b.append(DateTimeFormat.getInstance().format((Date)
   1.186 +             * i.next(), isUtc())); }
   1.187 +             */
   1.188 +            b.append(i.next());
   1.189 +            if (i.hasNext()) {
   1.190 +                b.append(',');
   1.191 +            }
   1.192 +        }
   1.193 +        return b.toString();
   1.194 +    }
   1.195 +
   1.196 +    /**
   1.197 +     * Add a date to the list. The date will be updated to reflect the
   1.198 +     * timezone of this list.
   1.199 +     * @param date the date to add
   1.200 +     * @return true
   1.201 +     * @see List#add(java.lang.Object)
   1.202 +     */
   1.203 +    public final boolean add(final Date date) {
   1.204 +        if (date instanceof DateTime) {
   1.205 +            if (isUtc()) {
   1.206 +                ((DateTime) date).setUtc(true);
   1.207 +            }
   1.208 +            else {
   1.209 +                ((DateTime) date).setTimeZone(getTimeZone());
   1.210 +            }
   1.211 +        }
   1.212 +        else if (!Value.DATE.equals(getType())) {
   1.213 +            final DateTime dateTime = new DateTime(date);
   1.214 +            dateTime.setTimeZone(getTimeZone());
   1.215 +            return add((Object) dateTime);
   1.216 +        }
   1.217 +        return add((Object) date);
   1.218 +    }
   1.219 +    
   1.220 +    /**
   1.221 +     * Overrides superclass to throw an <code>IllegalArgumentException</code>
   1.222 +     * Where argument is not a <code>net.fortuna.ical4j.model.Date</code>.
   1.223 +     * @param date the date to add
   1.224 +     * @return true if the object was added, otherwise false
   1.225 +     * @see List#add(E)
   1.226 +     */
   1.227 +    public final boolean add(final Object date) {
   1.228 +        if (!(date instanceof Date)) {
   1.229 +            throw new IllegalArgumentException("Argument not a " + Date.class.getName());
   1.230 +        }
   1.231 +        return dates.add(date);
   1.232 +    }
   1.233 +
   1.234 +    /**
   1.235 +     * Remove a date from the list.
   1.236 +     * 
   1.237 +     * @param date
   1.238 +     *            the date to remove
   1.239 +     * @return true if the list contained the specified date
   1.240 +     * @see List#remove(java.lang.Object)
   1.241 +     */
   1.242 +    public final boolean remove(final Date date) {
   1.243 +        return remove((Object) date);
   1.244 +    }
   1.245 +
   1.246 +    /**
   1.247 +     * Returns the VALUE parameter specifying the type of dates (ie. date or
   1.248 +     * date-time) stored in this date list.
   1.249 +     * 
   1.250 +     * @return Returns a Value parameter.
   1.251 +     */
   1.252 +    public final Value getType() {
   1.253 +        return type;
   1.254 +    }
   1.255 +
   1.256 +    /**
   1.257 +     * Indicates whether this list is in local or UTC format. This property will
   1.258 +     * have no affect if the type of the list is not DATE-TIME.
   1.259 +     * 
   1.260 +     * @return Returns true if in UTC format, otherwise false.
   1.261 +     */
   1.262 +    public final boolean isUtc() {
   1.263 +        return utc;
   1.264 +    }
   1.265 +
   1.266 +    /**
   1.267 +     * Sets whether this list is in UTC or local time format.
   1.268 +     * 
   1.269 +     * @param utc
   1.270 +     *            The utc to set.
   1.271 +     */
   1.272 +    public final void setUtc(final boolean utc) {
   1.273 +        if (!Value.DATE.equals(type)) {
   1.274 +            for (final Iterator i = iterator(); i.hasNext();) {
   1.275 +                ((DateTime) i.next()).setUtc(utc);
   1.276 +            }
   1.277 +        }
   1.278 +        this.timeZone = null;
   1.279 +        this.utc = utc;
   1.280 +    }
   1.281 +    
   1.282 +    /**
   1.283 +     * Applies the specified timezone to all dates in the list.
   1.284 +     * All dates added to this list will also have this timezone
   1.285 +     * applied.
   1.286 +     * @param timeZone a timezone to apply to contained dates
   1.287 +     */
   1.288 +    public final void setTimeZone(final TimeZone timeZone) {
   1.289 +        if (!Value.DATE.equals(type)) {
   1.290 +            for (final Iterator i = iterator(); i.hasNext();) {
   1.291 +                ((DateTime) i.next()).setTimeZone(timeZone);
   1.292 +            }
   1.293 +        }
   1.294 +        this.timeZone = timeZone;
   1.295 +        this.utc = false;
   1.296 +    }
   1.297 +
   1.298 +    /**
   1.299 +     * @return Returns the timeZone.
   1.300 +     */
   1.301 +    public final TimeZone getTimeZone() {
   1.302 +        return timeZone;
   1.303 +    }
   1.304 +
   1.305 +	public final void add(int arg0, Object arg1) {
   1.306 +		dates.add(arg0, arg1);
   1.307 +	}
   1.308 +
   1.309 +	public final boolean addAll(Collection arg0) {
   1.310 +		return dates.addAll(arg0);
   1.311 +	}
   1.312 +
   1.313 +	public final boolean addAll(int arg0, Collection arg1) {
   1.314 +		return dates.addAll(arg0, arg1);
   1.315 +	}
   1.316 +
   1.317 +	public final void clear() {
   1.318 +		dates.clear();
   1.319 +	}
   1.320 +
   1.321 +	public final boolean contains(Object o) {
   1.322 +		return dates.contains(o);
   1.323 +	}
   1.324 +
   1.325 +	public final boolean containsAll(Collection arg0) {
   1.326 +		return dates.containsAll(arg0);
   1.327 +	}
   1.328 +
   1.329 +	public final Object get(int index) {
   1.330 +		return dates.get(index);
   1.331 +	}
   1.332 +
   1.333 +	public final int indexOf(Object o) {
   1.334 +		return dates.indexOf(o);
   1.335 +	}
   1.336 +
   1.337 +	public final boolean isEmpty() {
   1.338 +		return dates.isEmpty();
   1.339 +	}
   1.340 +
   1.341 +	public final Iterator iterator() {
   1.342 +		return dates.iterator();
   1.343 +	}
   1.344 +
   1.345 +	public final int lastIndexOf(Object o) {
   1.346 +		return dates.lastIndexOf(o);
   1.347 +	}
   1.348 +
   1.349 +	public final ListIterator listIterator() {
   1.350 +		return dates.listIterator();
   1.351 +	}
   1.352 +
   1.353 +	public final ListIterator listIterator(int index) {
   1.354 +		return dates.listIterator(index);
   1.355 +	}
   1.356 +
   1.357 +	public final Object remove(int index) {
   1.358 +		return dates.remove(index);
   1.359 +	}
   1.360 +
   1.361 +	public final boolean remove(Object o) {
   1.362 +		return dates.remove(o);
   1.363 +	}
   1.364 +
   1.365 +	public final boolean removeAll(Collection arg0) {
   1.366 +		return dates.removeAll(arg0);
   1.367 +	}
   1.368 +
   1.369 +	public final boolean retainAll(Collection arg0) {
   1.370 +		return dates.retainAll(arg0);
   1.371 +	}
   1.372 +
   1.373 +	public final Object set(int arg0, Object arg1) {
   1.374 +		return dates.set(arg0, arg1);
   1.375 +	}
   1.376 +
   1.377 +	public final int size() {
   1.378 +		return dates.size();
   1.379 +	}
   1.380 +
   1.381 +	public final List subList(int fromIndex, int toIndex) {
   1.382 +		return dates.subList(fromIndex, toIndex);
   1.383 +	}
   1.384 +
   1.385 +	public final Object[] toArray() {
   1.386 +		return dates.toArray();
   1.387 +	}
   1.388 +
   1.389 +	public final Object[] toArray(Object[] arg0) {
   1.390 +		return dates.toArray(arg0);
   1.391 +	}
   1.392 +	
   1.393 +	public final boolean equals(Object obj) {
   1.394 +		if (!getClass().isAssignableFrom(obj.getClass())) {
   1.395 +			return false;
   1.396 +		}
   1.397 +		final DateList rhs = (DateList) obj;
   1.398 +		return new EqualsBuilder().append(dates, rhs.dates)
   1.399 +			.append(type, rhs.type)
   1.400 +			.append(timeZone, rhs.timeZone)
   1.401 +			.append(utc, utc)
   1.402 +			.isEquals();
   1.403 +	}
   1.404 +	
   1.405 +	public final int hashCode() {
   1.406 +		return new HashCodeBuilder().append(dates)
   1.407 +			.append(type)
   1.408 +			.append(timeZone)
   1.409 +			.append(utc)
   1.410 +			.toHashCode();
   1.411 +	}
   1.412 +}

mercurial