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.Date; michael@0: import net.fortuna.ical4j.model.DateTime; 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.ValidationException; 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.ParameterValidator; michael@0: import net.fortuna.ical4j.util.Strings; michael@0: michael@0: /** michael@0: * $Id$ michael@0: * michael@0: * Created on 9/07/2005 michael@0: * michael@0: * Base class for properties with a DATE or DATE-TIME value. Note that some sub-classes may only allow either a DATE or michael@0: * a DATE-TIME value, for which additional rules/validation should be specified. michael@0: * @author Ben Fortuna michael@0: */ michael@0: public abstract class DateProperty extends Property { michael@0: michael@0: private static final long serialVersionUID = 3160883132732961321L; michael@0: michael@0: private Date date; michael@0: michael@0: private TimeZone timeZone; michael@0: michael@0: /** michael@0: * @param name the property name michael@0: * @param parameters a list of initial parameters michael@0: */ michael@0: public DateProperty(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: */ michael@0: public DateProperty(final String name, PropertyFactory factory) { michael@0: super(name, factory); michael@0: } michael@0: michael@0: /** michael@0: * Creates a new instance of the named property with an initial timezone. michael@0: * @param name property name michael@0: * @param timezone initial timezone michael@0: */ michael@0: public DateProperty(final String name, TimeZone timezone, PropertyFactory factory) { michael@0: super(name, factory); michael@0: updateTimeZone(timezone); michael@0: } michael@0: michael@0: /** michael@0: * @return Returns the date. michael@0: */ michael@0: public final Date getDate() { michael@0: return date; michael@0: } michael@0: michael@0: /** michael@0: * Sets the date value of this property. The timezone and value of this michael@0: * instance will also be updated accordingly. michael@0: * @param date The date to set. michael@0: */ michael@0: public final void setDate(final Date date) { michael@0: this.date = date; michael@0: if (date instanceof DateTime) { michael@0: if (Value.DATE.equals(getParameter(Parameter.VALUE))) { michael@0: getParameters().replace(Value.DATE_TIME); michael@0: } michael@0: updateTimeZone(((DateTime) date).getTimeZone()); michael@0: } michael@0: else { michael@0: if (date != null) { michael@0: getParameters().replace(Value.DATE); michael@0: } michael@0: /* michael@0: else { michael@0: getParameters().removeAll(Parameter.VALUE); michael@0: } michael@0: */ michael@0: // ensure timezone is null for VALUE=DATE or null properties.. michael@0: updateTimeZone(null); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Default setValue() implementation. Allows for either DATE or DATE-TIME values. michael@0: * michael@0: * @param value a string representation of a DATE or DATE-TIME value michael@0: * @throws ParseException where the specified value is not a valid DATE or DATE-TIME michael@0: * representation michael@0: */ michael@0: public void setValue(final String value) throws ParseException { michael@0: // value can be either a date-time or a date.. michael@0: if (Value.DATE.equals(getParameter(Parameter.VALUE))) { michael@0: // ensure timezone is null for VALUE=DATE properties.. michael@0: updateTimeZone(null); michael@0: this.date = new Date(value); michael@0: } michael@0: else { michael@0: this.date = new DateTime(value, timeZone); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public String getValue() { michael@0: return Strings.valueOf(getDate()); michael@0: } michael@0: michael@0: /** michael@0: * Publically available method to update the current timezone. michael@0: * @param timezone a timezone instance michael@0: */ michael@0: public void setTimeZone(final TimeZone timezone) { michael@0: updateTimeZone(timezone); 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: * {@inheritDoc} michael@0: */ michael@0: public int hashCode() { michael@0: return getDate().hashCode(); michael@0: } michael@0: michael@0: /** michael@0: * Updates the timezone associated with the property's value. If the specified timezone is equivalent to UTC any michael@0: * existing TZID parameters will be removed. Note that this method is only applicable where the current date is an michael@0: * instance of DateTime. For all other cases an UnsupportedOperationException will be michael@0: * thrown. michael@0: * @param vTimeZone michael@0: */ michael@0: private void updateTimeZone(final TimeZone timezone) { michael@0: this.timeZone = timezone; michael@0: if (timezone != null) { michael@0: if (getDate() != null && !(getDate() instanceof DateTime)) { michael@0: throw new UnsupportedOperationException( michael@0: "TimeZone is not applicable to current value"); michael@0: } michael@0: if (getDate() != null) { michael@0: ((DateTime) getDate()).setTimeZone(timezone); michael@0: } michael@0: michael@0: getParameters().replace(new TzId(timezone.getID())); michael@0: } michael@0: else { michael@0: // use setUtc() to reset timezone.. michael@0: setUtc(isUtc()); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Resets the VTIMEZONE 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 a UTC value michael@0: */ michael@0: public final void setUtc(final boolean utc) { michael@0: if (getDate() != null && (getDate() instanceof DateTime)) { michael@0: ((DateTime) getDate()).setUtc(utc); michael@0: } michael@0: getParameters().remove(getParameter(Parameter.TZID)); michael@0: } michael@0: michael@0: /** michael@0: * Indicates whether the current date value is specified in UTC time. michael@0: * @return true if the property is in UTC time, otherwise false michael@0: */ michael@0: public final boolean isUtc() { michael@0: if (getDate() instanceof DateTime) { michael@0: return ((DateTime) getDate()).isUtc(); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public void validate() throws ValidationException { michael@0: michael@0: ParameterValidator.getInstance().assertOneOrLess(Parameter.VALUE, michael@0: getParameters()); michael@0: michael@0: if (isUtc()) { michael@0: ParameterValidator.getInstance().assertNone(Parameter.TZID, michael@0: getParameters()); michael@0: } michael@0: else { michael@0: ParameterValidator.getInstance().assertOneOrLess(Parameter.TZID, michael@0: getParameters()); michael@0: } michael@0: michael@0: final Value value = (Value) getParameter(Parameter.VALUE); michael@0: michael@0: if (getDate() instanceof DateTime) { michael@0: michael@0: if (value != null && !Value.DATE_TIME.equals(value)) { michael@0: throw new ValidationException("VALUE parameter [" + value michael@0: + "] is invalid for DATE-TIME instance"); michael@0: } michael@0: michael@0: final DateTime dateTime = (DateTime) date; michael@0: michael@0: // ensure tzid matches date-time timezone.. michael@0: final Parameter tzId = getParameter(Parameter.TZID); michael@0: if (dateTime.getTimeZone() != null michael@0: && (tzId == null || !tzId.getValue().equals( michael@0: dateTime.getTimeZone().getID()))) { michael@0: michael@0: throw new ValidationException("TZID parameter [" + tzId michael@0: + "] does not match the timezone [" michael@0: + dateTime.getTimeZone().getID() + "]"); michael@0: } michael@0: } michael@0: else if (getDate() != null) { michael@0: michael@0: if (value == null) { michael@0: throw new ValidationException("VALUE parameter [" + Value.DATE michael@0: + "] must be specified for DATE instance"); michael@0: } michael@0: else if (!Value.DATE.equals(value)) { michael@0: throw new ValidationException("VALUE parameter [" + value michael@0: + "] is invalid for DATE instance"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public Property copy() throws IOException, URISyntaxException, ParseException { michael@0: final Property copy = super.copy(); michael@0: michael@0: ((DateProperty) copy).timeZone = timeZone; michael@0: ((DateProperty) copy).setValue(getValue()); michael@0: michael@0: return copy; michael@0: } michael@0: }