1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/model/property/DateProperty.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,288 @@ 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.property; 1.36 + 1.37 +import java.io.IOException; 1.38 +import java.net.URISyntaxException; 1.39 +import java.text.ParseException; 1.40 + 1.41 +import net.fortuna.ical4j.model.Date; 1.42 +import net.fortuna.ical4j.model.DateTime; 1.43 +import net.fortuna.ical4j.model.Parameter; 1.44 +import net.fortuna.ical4j.model.ParameterList; 1.45 +import net.fortuna.ical4j.model.Property; 1.46 +import net.fortuna.ical4j.model.PropertyFactory; 1.47 +import net.fortuna.ical4j.model.TimeZone; 1.48 +import net.fortuna.ical4j.model.ValidationException; 1.49 +import net.fortuna.ical4j.model.parameter.TzId; 1.50 +import net.fortuna.ical4j.model.parameter.Value; 1.51 +import net.fortuna.ical4j.util.ParameterValidator; 1.52 +import net.fortuna.ical4j.util.Strings; 1.53 + 1.54 +/** 1.55 + * $Id$ 1.56 + * 1.57 + * Created on 9/07/2005 1.58 + * 1.59 + * Base class for properties with a DATE or DATE-TIME value. Note that some sub-classes may only allow either a DATE or 1.60 + * a DATE-TIME value, for which additional rules/validation should be specified. 1.61 + * @author Ben Fortuna 1.62 + */ 1.63 +public abstract class DateProperty extends Property { 1.64 + 1.65 + private static final long serialVersionUID = 3160883132732961321L; 1.66 + 1.67 + private Date date; 1.68 + 1.69 + private TimeZone timeZone; 1.70 + 1.71 + /** 1.72 + * @param name the property name 1.73 + * @param parameters a list of initial parameters 1.74 + */ 1.75 + public DateProperty(final String name, final ParameterList parameters, PropertyFactory factory) { 1.76 + super(name, parameters, factory); 1.77 + } 1.78 + 1.79 + /** 1.80 + * @param name the property name 1.81 + */ 1.82 + public DateProperty(final String name, PropertyFactory factory) { 1.83 + super(name, factory); 1.84 + } 1.85 + 1.86 + /** 1.87 + * Creates a new instance of the named property with an initial timezone. 1.88 + * @param name property name 1.89 + * @param timezone initial timezone 1.90 + */ 1.91 + public DateProperty(final String name, TimeZone timezone, PropertyFactory factory) { 1.92 + super(name, factory); 1.93 + updateTimeZone(timezone); 1.94 + } 1.95 + 1.96 + /** 1.97 + * @return Returns the date. 1.98 + */ 1.99 + public final Date getDate() { 1.100 + return date; 1.101 + } 1.102 + 1.103 + /** 1.104 + * Sets the date value of this property. The timezone and value of this 1.105 + * instance will also be updated accordingly. 1.106 + * @param date The date to set. 1.107 + */ 1.108 + public final void setDate(final Date date) { 1.109 + this.date = date; 1.110 + if (date instanceof DateTime) { 1.111 + if (Value.DATE.equals(getParameter(Parameter.VALUE))) { 1.112 + getParameters().replace(Value.DATE_TIME); 1.113 + } 1.114 + updateTimeZone(((DateTime) date).getTimeZone()); 1.115 + } 1.116 + else { 1.117 + if (date != null) { 1.118 + getParameters().replace(Value.DATE); 1.119 + } 1.120 + /* 1.121 + else { 1.122 + getParameters().removeAll(Parameter.VALUE); 1.123 + } 1.124 + */ 1.125 + // ensure timezone is null for VALUE=DATE or null properties.. 1.126 + updateTimeZone(null); 1.127 + } 1.128 + } 1.129 + 1.130 + /** 1.131 + * Default setValue() implementation. Allows for either DATE or DATE-TIME values. 1.132 + * 1.133 + * @param value a string representation of a DATE or DATE-TIME value 1.134 + * @throws ParseException where the specified value is not a valid DATE or DATE-TIME 1.135 + * representation 1.136 + */ 1.137 + public void setValue(final String value) throws ParseException { 1.138 + // value can be either a date-time or a date.. 1.139 + if (Value.DATE.equals(getParameter(Parameter.VALUE))) { 1.140 + // ensure timezone is null for VALUE=DATE properties.. 1.141 + updateTimeZone(null); 1.142 + this.date = new Date(value); 1.143 + } 1.144 + else { 1.145 + this.date = new DateTime(value, timeZone); 1.146 + } 1.147 + } 1.148 + 1.149 + /** 1.150 + * {@inheritDoc} 1.151 + */ 1.152 + public String getValue() { 1.153 + return Strings.valueOf(getDate()); 1.154 + } 1.155 + 1.156 + /** 1.157 + * Publically available method to update the current timezone. 1.158 + * @param timezone a timezone instance 1.159 + */ 1.160 + public void setTimeZone(final TimeZone timezone) { 1.161 + updateTimeZone(timezone); 1.162 + } 1.163 + 1.164 + /** 1.165 + * @return the timezone 1.166 + */ 1.167 + public final TimeZone getTimeZone() { 1.168 + return timeZone; 1.169 + } 1.170 + 1.171 + /** 1.172 + * {@inheritDoc} 1.173 + */ 1.174 + public int hashCode() { 1.175 + return getDate().hashCode(); 1.176 + } 1.177 + 1.178 + /** 1.179 + * Updates the timezone associated with the property's value. If the specified timezone is equivalent to UTC any 1.180 + * existing TZID parameters will be removed. Note that this method is only applicable where the current date is an 1.181 + * instance of <code>DateTime</code>. For all other cases an <code>UnsupportedOperationException</code> will be 1.182 + * thrown. 1.183 + * @param vTimeZone 1.184 + */ 1.185 + private void updateTimeZone(final TimeZone timezone) { 1.186 + this.timeZone = timezone; 1.187 + if (timezone != null) { 1.188 + if (getDate() != null && !(getDate() instanceof DateTime)) { 1.189 + throw new UnsupportedOperationException( 1.190 + "TimeZone is not applicable to current value"); 1.191 + } 1.192 + if (getDate() != null) { 1.193 + ((DateTime) getDate()).setTimeZone(timezone); 1.194 + } 1.195 + 1.196 + getParameters().replace(new TzId(timezone.getID())); 1.197 + } 1.198 + else { 1.199 + // use setUtc() to reset timezone.. 1.200 + setUtc(isUtc()); 1.201 + } 1.202 + } 1.203 + 1.204 + /** 1.205 + * Resets the VTIMEZONE associated with the property. If utc is true, any TZID parameters are removed and the Java 1.206 + * timezone is updated to UTC time. If utc is false, TZID parameters are removed and the Java timezone is set to the 1.207 + * default timezone (i.e. represents a "floating" local time) 1.208 + * @param utc a UTC value 1.209 + */ 1.210 + public final void setUtc(final boolean utc) { 1.211 + if (getDate() != null && (getDate() instanceof DateTime)) { 1.212 + ((DateTime) getDate()).setUtc(utc); 1.213 + } 1.214 + getParameters().remove(getParameter(Parameter.TZID)); 1.215 + } 1.216 + 1.217 + /** 1.218 + * Indicates whether the current date value is specified in UTC time. 1.219 + * @return true if the property is in UTC time, otherwise false 1.220 + */ 1.221 + public final boolean isUtc() { 1.222 + if (getDate() instanceof DateTime) { 1.223 + return ((DateTime) getDate()).isUtc(); 1.224 + } 1.225 + return false; 1.226 + } 1.227 + 1.228 + /** 1.229 + * {@inheritDoc} 1.230 + */ 1.231 + public void validate() throws ValidationException { 1.232 + 1.233 + ParameterValidator.getInstance().assertOneOrLess(Parameter.VALUE, 1.234 + getParameters()); 1.235 + 1.236 + if (isUtc()) { 1.237 + ParameterValidator.getInstance().assertNone(Parameter.TZID, 1.238 + getParameters()); 1.239 + } 1.240 + else { 1.241 + ParameterValidator.getInstance().assertOneOrLess(Parameter.TZID, 1.242 + getParameters()); 1.243 + } 1.244 + 1.245 + final Value value = (Value) getParameter(Parameter.VALUE); 1.246 + 1.247 + if (getDate() instanceof DateTime) { 1.248 + 1.249 + if (value != null && !Value.DATE_TIME.equals(value)) { 1.250 + throw new ValidationException("VALUE parameter [" + value 1.251 + + "] is invalid for DATE-TIME instance"); 1.252 + } 1.253 + 1.254 + final DateTime dateTime = (DateTime) date; 1.255 + 1.256 + // ensure tzid matches date-time timezone.. 1.257 + final Parameter tzId = getParameter(Parameter.TZID); 1.258 + if (dateTime.getTimeZone() != null 1.259 + && (tzId == null || !tzId.getValue().equals( 1.260 + dateTime.getTimeZone().getID()))) { 1.261 + 1.262 + throw new ValidationException("TZID parameter [" + tzId 1.263 + + "] does not match the timezone [" 1.264 + + dateTime.getTimeZone().getID() + "]"); 1.265 + } 1.266 + } 1.267 + else if (getDate() != null) { 1.268 + 1.269 + if (value == null) { 1.270 + throw new ValidationException("VALUE parameter [" + Value.DATE 1.271 + + "] must be specified for DATE instance"); 1.272 + } 1.273 + else if (!Value.DATE.equals(value)) { 1.274 + throw new ValidationException("VALUE parameter [" + value 1.275 + + "] is invalid for DATE instance"); 1.276 + } 1.277 + } 1.278 + } 1.279 + 1.280 + /** 1.281 + * {@inheritDoc} 1.282 + */ 1.283 + public Property copy() throws IOException, URISyntaxException, ParseException { 1.284 + final Property copy = super.copy(); 1.285 + 1.286 + ((DateProperty) copy).timeZone = timeZone; 1.287 + ((DateProperty) copy).setValue(getValue()); 1.288 + 1.289 + return copy; 1.290 + } 1.291 +}