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.property; michael@0: michael@0: import java.io.IOException; michael@0: import java.net.URISyntaxException; michael@0: import java.text.ParseException; michael@0: michael@0: import net.fortuna.ical4j.model.DateList; michael@0: import net.fortuna.ical4j.model.Parameter; michael@0: import net.fortuna.ical4j.model.ParameterList; michael@0: import net.fortuna.ical4j.model.Property; michael@0: import net.fortuna.ical4j.model.PropertyFactory; michael@0: import net.fortuna.ical4j.model.TimeZone; michael@0: import net.fortuna.ical4j.model.parameter.TzId; michael@0: import net.fortuna.ical4j.model.parameter.Value; michael@0: import net.fortuna.ical4j.util.Strings; michael@0: michael@0: /** michael@0: * $Id$ michael@0: * michael@0: * Created on 11/08/2005 michael@0: * michael@0: * Base class for properties with a list of dates as a value. michael@0: * @author Ben Fortuna michael@0: */ michael@0: public abstract class DateListProperty extends Property { michael@0: michael@0: /** michael@0: * michael@0: */ michael@0: private static final long serialVersionUID = 5233773091972759919L; michael@0: michael@0: private DateList dates; michael@0: michael@0: private TimeZone timeZone; michael@0: michael@0: /** michael@0: * @param name the property name michael@0: */ michael@0: public DateListProperty(final String name, PropertyFactory factory) { michael@0: this(name, new DateList(Value.DATE_TIME), factory); michael@0: } michael@0: michael@0: /** michael@0: * @param name the property name michael@0: * @param parameters property parameters michael@0: */ michael@0: public DateListProperty(final String name, final ParameterList parameters, PropertyFactory factory) { michael@0: super(name, parameters, factory); michael@0: } michael@0: michael@0: /** michael@0: * @param name the property name michael@0: * @param dates a list of initial dates for the property michael@0: */ michael@0: public DateListProperty(final String name, final DateList dates, PropertyFactory factory) { michael@0: this(name, new ParameterList(), dates, factory); michael@0: } michael@0: michael@0: /** michael@0: * @param name the property name michael@0: * @param parameters property parameters michael@0: * @param dates a list of initial dates for the property michael@0: */ michael@0: public DateListProperty(final String name, final ParameterList parameters, final DateList dates, michael@0: PropertyFactory factory) { michael@0: super(name, parameters, factory); michael@0: this.dates = dates; michael@0: if (dates != null && !Value.DATE_TIME.equals(dates.getType())) { michael@0: getParameters().replace(dates.getType()); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * @return Returns the dates. michael@0: */ michael@0: public final DateList getDates() { michael@0: return dates; michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public void setValue(final String aValue) throws ParseException { michael@0: dates = new DateList(aValue, (Value) getParameter(Parameter.VALUE), michael@0: timeZone); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public String getValue() { michael@0: return Strings.valueOf(dates); michael@0: } michael@0: michael@0: /** michael@0: * Sets the timezone associated with this property. michael@0: * @param timezone a timezone to associate with this property michael@0: */ michael@0: public void setTimeZone(final TimeZone timezone) { michael@0: if (dates == null) { michael@0: throw new UnsupportedOperationException( michael@0: "TimeZone is not applicable to current value"); michael@0: } michael@0: this.timeZone = timezone; michael@0: if (timezone != null) { michael@0: if (!Value.DATE_TIME.equals(getDates().getType())) { michael@0: throw new UnsupportedOperationException( michael@0: "TimeZone is not applicable to current value"); michael@0: } michael@0: dates.setTimeZone(timezone); michael@0: getParameters().remove(getParameter(Parameter.TZID)); michael@0: final TzId tzId = new TzId(timezone.getID()); michael@0: getParameters().replace(tzId); michael@0: } michael@0: else { michael@0: // use setUtc() to reset timezone.. michael@0: setUtc(false); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * @return the timezone michael@0: */ michael@0: public final TimeZone getTimeZone() { michael@0: return timeZone; michael@0: } michael@0: michael@0: /** michael@0: * Resets the timezone associated with the property. If utc is true, any TZID parameters are removed and the Java michael@0: * timezone is updated to UTC time. If utc is false, TZID parameters are removed and the Java timezone is set to the michael@0: * default timezone (i.e. represents a "floating" local time) michael@0: * @param utc the UTC value michael@0: */ michael@0: public final void setUtc(final boolean utc) { michael@0: if (dates == null || !Value.DATE_TIME.equals(dates.getType())) { michael@0: throw new UnsupportedOperationException( michael@0: "TimeZone is not applicable to current value"); michael@0: } michael@0: dates.setUtc(utc); michael@0: getParameters().remove(getParameter(Parameter.TZID)); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final Property copy() throws IOException, URISyntaxException, ParseException { michael@0: final Property copy = super.copy(); michael@0: michael@0: ((DateListProperty) copy).timeZone = timeZone; michael@0: ((DateListProperty) copy).setValue(getValue()); michael@0: michael@0: return copy; michael@0: } michael@0: }