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

Tue, 10 Feb 2015 19:38:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 19:38:00 +0100
changeset 3
73bdfa70b04e
parent 0
fb9019fb1bf7
child 4
45d57ecba757
permissions
-rw-r--r--

Upgrade embedded ical4j from ancient whatever to upstream version 1.0.6.

michael@0 1 /**
michael@0 2 * Copyright (c) 2012, Ben Fortuna
michael@0 3 * All rights reserved.
michael@0 4 *
michael@0 5 * Redistribution and use in source and binary forms, with or without
michael@0 6 * modification, are permitted provided that the following conditions
michael@0 7 * are met:
michael@0 8 *
michael@0 9 * o Redistributions of source code must retain the above copyright
michael@0 10 * notice, this list of conditions and the following disclaimer.
michael@0 11 *
michael@0 12 * o Redistributions in binary form must reproduce the above copyright
michael@0 13 * notice, this list of conditions and the following disclaimer in the
michael@0 14 * documentation and/or other materials provided with the distribution.
michael@0 15 *
michael@0 16 * o Neither the name of Ben Fortuna nor the names of any other contributors
michael@0 17 * may be used to endorse or promote products derived from this software
michael@0 18 * without specific prior written permission.
michael@0 19 *
michael@0 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
michael@0 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
michael@0 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
michael@0 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
michael@0 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31 */
michael@0 32 package net.fortuna.ical4j.model;
michael@0 33
michael@0 34 import java.io.Serializable;
michael@0 35 import java.text.ParseException;
michael@0 36 import java.util.ArrayList;
michael@0 37 import java.util.Collection;
michael@0 38 import java.util.Collections;
michael@0 39 import java.util.Iterator;
michael@0 40 import java.util.List;
michael@0 41 import java.util.ListIterator;
michael@0 42 import java.util.StringTokenizer;
michael@0 43
michael@0 44 import net.fortuna.ical4j.model.parameter.Value;
michael@0 45
michael@0 46 import org.apache.commons.lang.builder.EqualsBuilder;
michael@0 47 import org.apache.commons.lang.builder.HashCodeBuilder;
michael@0 48
michael@0 49 /**
michael@0 50 * $Id$ [23-Apr-2004]
michael@0 51 *
michael@0 52 * Defines a list of iCalendar dates. If no value type is specified a list
michael@0 53 * defaults to DATE-TIME instances.
michael@0 54 * @author Ben Fortuna
michael@0 55 */
michael@0 56 public class DateList implements List, Serializable {
michael@0 57
michael@0 58 private static final long serialVersionUID = -3700862452550012357L;
michael@0 59
michael@0 60 private final Value type;
michael@0 61
michael@0 62 private final List dates;
michael@0 63
michael@0 64 private TimeZone timeZone;
michael@0 65
michael@0 66 private boolean utc;
michael@0 67
michael@0 68 /**
michael@0 69 * Default constructor.
michael@0 70 */
michael@0 71 public DateList() {
michael@0 72 this(false);
michael@0 73 }
michael@0 74
michael@0 75 public DateList(final boolean unmodifiable) {
michael@0 76
michael@0 77 this.type = Value.DATE_TIME;
michael@0 78 if (unmodifiable) {
michael@0 79 dates = Collections.EMPTY_LIST;
michael@0 80 }
michael@0 81 else {
michael@0 82 dates = new ArrayList();
michael@0 83 }
michael@0 84 }
michael@0 85
michael@0 86 /**
michael@0 87 * @param aType the type of dates contained by the instance
michael@0 88 */
michael@0 89 public DateList(final Value aType) {
michael@0 90 this(aType, null);
michael@0 91 }
michael@0 92
michael@0 93 /**
michael@0 94 * Default constructor.
michael@0 95 *
michael@0 96 * @param aType
michael@0 97 * specifies the type of dates (either date or date-time)
michael@0 98 * @param timezone the timezone to apply to dates contained by the instance
michael@0 99 */
michael@0 100 public DateList(final Value aType, final TimeZone timezone) {
michael@0 101 if (aType == null) {
michael@0 102 this.type = Value.DATE_TIME;
michael@0 103 } else {
michael@0 104 this.type = aType;
michael@0 105 }
michael@0 106 this.timeZone = timezone;
michael@0 107 dates = new ArrayList();
michael@0 108 }
michael@0 109
michael@0 110 /**
michael@0 111 * @param aValue a string representation of a date list
michael@0 112 * @param aType the date types contained in the instance
michael@0 113 * @throws ParseException where the specified string is not a valid date list
michael@0 114 */
michael@0 115 public DateList(final String aValue, final Value aType) throws ParseException {
michael@0 116 this(aValue, aType, null);
michael@0 117 }
michael@0 118
michael@0 119 /**
michael@0 120 * Parses the specified string representation to create a list of dates.
michael@0 121 *
michael@0 122 * @param aValue
michael@0 123 * a string representation of a list of dates
michael@0 124 * @param aType
michael@0 125 * specifies the type of dates (either date or date-time)
michael@0 126 * @param timezone the timezone to apply to contained dates
michael@0 127 * @throws ParseException
michael@0 128 * if an invalid date representation exists in the date list
michael@0 129 * string
michael@0 130 */
michael@0 131 public DateList(final String aValue, final Value aType, final TimeZone timezone)
michael@0 132 throws ParseException {
michael@0 133
michael@0 134 this(aType, timezone);
michael@0 135 final StringTokenizer t = new StringTokenizer(aValue, ",");
michael@0 136 while (t.hasMoreTokens()) {
michael@0 137 if (Value.DATE.equals(type)) {
michael@0 138 add((Object) new Date(t.nextToken()));
michael@0 139 }
michael@0 140 else {
michael@0 141 add((Object) new DateTime(t.nextToken(), timezone));
michael@0 142 }
michael@0 143 }
michael@0 144 }
michael@0 145
michael@0 146 /**
michael@0 147 * Constructs a new date list of the specified type containing
michael@0 148 * the dates in the specified list.
michael@0 149 * @param list a list of dates to include in the new list
michael@0 150 * @param type the type of the new list
michael@0 151 */
michael@0 152 public DateList(final DateList list, final Value type) {
michael@0 153 if (!Value.DATE.equals(type) && !Value.DATE_TIME.equals(type)) {
michael@0 154 throw new IllegalArgumentException(
michael@0 155 "Type must be either DATE or DATE-TIME");
michael@0 156 }
michael@0 157
michael@0 158 this.type = type;
michael@0 159 dates = new ArrayList();
michael@0 160
michael@0 161 if (Value.DATE.equals(type)) {
michael@0 162 for (final Iterator i = list.iterator(); i.hasNext();) {
michael@0 163 add(new Date((Date) i.next()));
michael@0 164 }
michael@0 165 }
michael@0 166 else {
michael@0 167 for (final Iterator i = list.iterator(); i.hasNext();) {
michael@0 168 add(new DateTime((Date) i.next()));
michael@0 169 }
michael@0 170 }
michael@0 171 }
michael@0 172
michael@0 173 /**
michael@0 174 * {@inheritDoc}
michael@0 175 */
michael@0 176 public final String toString() {
michael@0 177 final StringBuffer b = new StringBuffer();
michael@0 178 for (final Iterator i = iterator(); i.hasNext();) {
michael@0 179 /*
michael@0 180 * if (type != null && Value.DATE.equals(type)) {
michael@0 181 * b.append(DateFormat.getInstance().format((Date) i.next())); }
michael@0 182 * else { b.append(DateTimeFormat.getInstance().format((Date)
michael@0 183 * i.next(), isUtc())); }
michael@0 184 */
michael@0 185 b.append(i.next());
michael@0 186 if (i.hasNext()) {
michael@0 187 b.append(',');
michael@0 188 }
michael@0 189 }
michael@0 190 return b.toString();
michael@0 191 }
michael@0 192
michael@0 193 /**
michael@0 194 * Add a date to the list. The date will be updated to reflect the
michael@0 195 * timezone of this list.
michael@0 196 * @param date the date to add
michael@0 197 * @return true
michael@3 198 * @see java.util.List#add(Object)
michael@0 199 */
michael@0 200 public final boolean add(final Date date) {
michael@0 201 if (date instanceof DateTime) {
michael@0 202 if (isUtc()) {
michael@0 203 ((DateTime) date).setUtc(true);
michael@0 204 }
michael@0 205 else {
michael@0 206 ((DateTime) date).setTimeZone(getTimeZone());
michael@0 207 }
michael@0 208 }
michael@0 209 else if (!Value.DATE.equals(getType())) {
michael@0 210 final DateTime dateTime = new DateTime(date);
michael@0 211 dateTime.setTimeZone(getTimeZone());
michael@0 212 return add((Object) dateTime);
michael@0 213 }
michael@0 214 return add((Object) date);
michael@0 215 }
michael@0 216
michael@0 217 /**
michael@0 218 * Overrides superclass to throw an <code>IllegalArgumentException</code>
michael@0 219 * Where argument is not a <code>net.fortuna.ical4j.model.Date</code>.
michael@0 220 * @param date the date to add
michael@0 221 * @return true if the object was added, otherwise false
michael@3 222 * @see java.util.List#add(Object)
michael@0 223 */
michael@0 224 public final boolean add(final Object date) {
michael@0 225 if (!(date instanceof Date)) {
michael@0 226 throw new IllegalArgumentException("Argument not a " + Date.class.getName());
michael@0 227 }
michael@0 228 return dates.add(date);
michael@0 229 }
michael@0 230
michael@0 231 /**
michael@0 232 * Remove a date from the list.
michael@0 233 *
michael@0 234 * @param date
michael@0 235 * the date to remove
michael@0 236 * @return true if the list contained the specified date
michael@0 237 * @see List#remove(java.lang.Object)
michael@0 238 */
michael@0 239 public final boolean remove(final Date date) {
michael@0 240 return remove((Object) date);
michael@0 241 }
michael@0 242
michael@0 243 /**
michael@0 244 * Returns the VALUE parameter specifying the type of dates (ie. date or
michael@0 245 * date-time) stored in this date list.
michael@0 246 *
michael@0 247 * @return Returns a Value parameter.
michael@0 248 */
michael@0 249 public final Value getType() {
michael@0 250 return type;
michael@0 251 }
michael@0 252
michael@0 253 /**
michael@0 254 * Indicates whether this list is in local or UTC format. This property will
michael@0 255 * have no affect if the type of the list is not DATE-TIME.
michael@0 256 *
michael@0 257 * @return Returns true if in UTC format, otherwise false.
michael@0 258 */
michael@0 259 public final boolean isUtc() {
michael@0 260 return utc;
michael@0 261 }
michael@0 262
michael@0 263 /**
michael@0 264 * Sets whether this list is in UTC or local time format.
michael@0 265 *
michael@0 266 * @param utc
michael@0 267 * The utc to set.
michael@0 268 */
michael@0 269 public final void setUtc(final boolean utc) {
michael@0 270 if (!Value.DATE.equals(type)) {
michael@0 271 for (final Iterator i = iterator(); i.hasNext();) {
michael@0 272 ((DateTime) i.next()).setUtc(utc);
michael@0 273 }
michael@0 274 }
michael@0 275 this.timeZone = null;
michael@0 276 this.utc = utc;
michael@0 277 }
michael@0 278
michael@0 279 /**
michael@0 280 * Applies the specified timezone to all dates in the list.
michael@0 281 * All dates added to this list will also have this timezone
michael@0 282 * applied.
michael@0 283 * @param timeZone a timezone to apply to contained dates
michael@0 284 */
michael@0 285 public final void setTimeZone(final TimeZone timeZone) {
michael@0 286 if (!Value.DATE.equals(type)) {
michael@0 287 for (final Iterator i = iterator(); i.hasNext();) {
michael@0 288 ((DateTime) i.next()).setTimeZone(timeZone);
michael@0 289 }
michael@0 290 }
michael@0 291 this.timeZone = timeZone;
michael@0 292 this.utc = false;
michael@0 293 }
michael@0 294
michael@0 295 /**
michael@0 296 * @return Returns the timeZone.
michael@0 297 */
michael@0 298 public final TimeZone getTimeZone() {
michael@0 299 return timeZone;
michael@0 300 }
michael@0 301
michael@0 302 public final void add(int arg0, Object arg1) {
michael@0 303 dates.add(arg0, arg1);
michael@0 304 }
michael@0 305
michael@0 306 public final boolean addAll(Collection arg0) {
michael@0 307 return dates.addAll(arg0);
michael@0 308 }
michael@0 309
michael@0 310 public final boolean addAll(int arg0, Collection arg1) {
michael@0 311 return dates.addAll(arg0, arg1);
michael@0 312 }
michael@0 313
michael@0 314 public final void clear() {
michael@0 315 dates.clear();
michael@0 316 }
michael@0 317
michael@0 318 public final boolean contains(Object o) {
michael@0 319 return dates.contains(o);
michael@0 320 }
michael@0 321
michael@0 322 public final boolean containsAll(Collection arg0) {
michael@0 323 return dates.containsAll(arg0);
michael@0 324 }
michael@0 325
michael@0 326 public final Object get(int index) {
michael@0 327 return dates.get(index);
michael@0 328 }
michael@0 329
michael@0 330 public final int indexOf(Object o) {
michael@0 331 return dates.indexOf(o);
michael@0 332 }
michael@0 333
michael@0 334 public final boolean isEmpty() {
michael@0 335 return dates.isEmpty();
michael@0 336 }
michael@0 337
michael@0 338 public final Iterator iterator() {
michael@0 339 return dates.iterator();
michael@0 340 }
michael@0 341
michael@0 342 public final int lastIndexOf(Object o) {
michael@0 343 return dates.lastIndexOf(o);
michael@0 344 }
michael@0 345
michael@0 346 public final ListIterator listIterator() {
michael@0 347 return dates.listIterator();
michael@0 348 }
michael@0 349
michael@0 350 public final ListIterator listIterator(int index) {
michael@0 351 return dates.listIterator(index);
michael@0 352 }
michael@0 353
michael@0 354 public final Object remove(int index) {
michael@0 355 return dates.remove(index);
michael@0 356 }
michael@0 357
michael@0 358 public final boolean remove(Object o) {
michael@0 359 return dates.remove(o);
michael@0 360 }
michael@0 361
michael@0 362 public final boolean removeAll(Collection arg0) {
michael@0 363 return dates.removeAll(arg0);
michael@0 364 }
michael@0 365
michael@0 366 public final boolean retainAll(Collection arg0) {
michael@0 367 return dates.retainAll(arg0);
michael@0 368 }
michael@0 369
michael@0 370 public final Object set(int arg0, Object arg1) {
michael@0 371 return dates.set(arg0, arg1);
michael@0 372 }
michael@0 373
michael@0 374 public final int size() {
michael@0 375 return dates.size();
michael@0 376 }
michael@0 377
michael@0 378 public final List subList(int fromIndex, int toIndex) {
michael@0 379 return dates.subList(fromIndex, toIndex);
michael@0 380 }
michael@0 381
michael@0 382 public final Object[] toArray() {
michael@0 383 return dates.toArray();
michael@0 384 }
michael@0 385
michael@0 386 public final Object[] toArray(Object[] arg0) {
michael@0 387 return dates.toArray(arg0);
michael@0 388 }
michael@0 389
michael@0 390 public final boolean equals(Object obj) {
michael@0 391 if (!getClass().isAssignableFrom(obj.getClass())) {
michael@0 392 return false;
michael@0 393 }
michael@0 394 final DateList rhs = (DateList) obj;
michael@0 395 return new EqualsBuilder().append(dates, rhs.dates)
michael@0 396 .append(type, rhs.type)
michael@0 397 .append(timeZone, rhs.timeZone)
michael@0 398 .append(utc, utc)
michael@0 399 .isEquals();
michael@0 400 }
michael@0 401
michael@0 402 public final int hashCode() {
michael@0 403 return new HashCodeBuilder().append(dates)
michael@0 404 .append(type)
michael@0 405 .append(timeZone)
michael@0 406 .append(utc)
michael@0 407 .toHashCode();
michael@0 408 }
michael@0 409 }

mercurial