src/net/fortuna/ical4j/model/property/DateProperty.java

Tue, 10 Feb 2015 18:12:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 18:12:00 +0100
changeset 0
fb9019fb1bf7
permissions
-rw-r--r--

Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.

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.property;
michael@0 33
michael@0 34 import java.io.IOException;
michael@0 35 import java.net.URISyntaxException;
michael@0 36 import java.text.ParseException;
michael@0 37
michael@0 38 import net.fortuna.ical4j.model.Date;
michael@0 39 import net.fortuna.ical4j.model.DateTime;
michael@0 40 import net.fortuna.ical4j.model.Parameter;
michael@0 41 import net.fortuna.ical4j.model.ParameterList;
michael@0 42 import net.fortuna.ical4j.model.Property;
michael@0 43 import net.fortuna.ical4j.model.PropertyFactory;
michael@0 44 import net.fortuna.ical4j.model.TimeZone;
michael@0 45 import net.fortuna.ical4j.model.ValidationException;
michael@0 46 import net.fortuna.ical4j.model.parameter.TzId;
michael@0 47 import net.fortuna.ical4j.model.parameter.Value;
michael@0 48 import net.fortuna.ical4j.util.ParameterValidator;
michael@0 49 import net.fortuna.ical4j.util.Strings;
michael@0 50
michael@0 51 /**
michael@0 52 * $Id$
michael@0 53 *
michael@0 54 * Created on 9/07/2005
michael@0 55 *
michael@0 56 * 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 57 * a DATE-TIME value, for which additional rules/validation should be specified.
michael@0 58 * @author Ben Fortuna
michael@0 59 */
michael@0 60 public abstract class DateProperty extends Property {
michael@0 61
michael@0 62 private static final long serialVersionUID = 3160883132732961321L;
michael@0 63
michael@0 64 private Date date;
michael@0 65
michael@0 66 private TimeZone timeZone;
michael@0 67
michael@0 68 /**
michael@0 69 * @param name the property name
michael@0 70 * @param parameters a list of initial parameters
michael@0 71 */
michael@0 72 public DateProperty(final String name, final ParameterList parameters, PropertyFactory factory) {
michael@0 73 super(name, parameters, factory);
michael@0 74 }
michael@0 75
michael@0 76 /**
michael@0 77 * @param name the property name
michael@0 78 */
michael@0 79 public DateProperty(final String name, PropertyFactory factory) {
michael@0 80 super(name, factory);
michael@0 81 }
michael@0 82
michael@0 83 /**
michael@0 84 * Creates a new instance of the named property with an initial timezone.
michael@0 85 * @param name property name
michael@0 86 * @param timezone initial timezone
michael@0 87 */
michael@0 88 public DateProperty(final String name, TimeZone timezone, PropertyFactory factory) {
michael@0 89 super(name, factory);
michael@0 90 updateTimeZone(timezone);
michael@0 91 }
michael@0 92
michael@0 93 /**
michael@0 94 * @return Returns the date.
michael@0 95 */
michael@0 96 public final Date getDate() {
michael@0 97 return date;
michael@0 98 }
michael@0 99
michael@0 100 /**
michael@0 101 * Sets the date value of this property. The timezone and value of this
michael@0 102 * instance will also be updated accordingly.
michael@0 103 * @param date The date to set.
michael@0 104 */
michael@0 105 public final void setDate(final Date date) {
michael@0 106 this.date = date;
michael@0 107 if (date instanceof DateTime) {
michael@0 108 if (Value.DATE.equals(getParameter(Parameter.VALUE))) {
michael@0 109 getParameters().replace(Value.DATE_TIME);
michael@0 110 }
michael@0 111 updateTimeZone(((DateTime) date).getTimeZone());
michael@0 112 }
michael@0 113 else {
michael@0 114 if (date != null) {
michael@0 115 getParameters().replace(Value.DATE);
michael@0 116 }
michael@0 117 /*
michael@0 118 else {
michael@0 119 getParameters().removeAll(Parameter.VALUE);
michael@0 120 }
michael@0 121 */
michael@0 122 // ensure timezone is null for VALUE=DATE or null properties..
michael@0 123 updateTimeZone(null);
michael@0 124 }
michael@0 125 }
michael@0 126
michael@0 127 /**
michael@0 128 * Default setValue() implementation. Allows for either DATE or DATE-TIME values.
michael@0 129 *
michael@0 130 * @param value a string representation of a DATE or DATE-TIME value
michael@0 131 * @throws ParseException where the specified value is not a valid DATE or DATE-TIME
michael@0 132 * representation
michael@0 133 */
michael@0 134 public void setValue(final String value) throws ParseException {
michael@0 135 // value can be either a date-time or a date..
michael@0 136 if (Value.DATE.equals(getParameter(Parameter.VALUE))) {
michael@0 137 // ensure timezone is null for VALUE=DATE properties..
michael@0 138 updateTimeZone(null);
michael@0 139 this.date = new Date(value);
michael@0 140 }
michael@0 141 else {
michael@0 142 this.date = new DateTime(value, timeZone);
michael@0 143 }
michael@0 144 }
michael@0 145
michael@0 146 /**
michael@0 147 * {@inheritDoc}
michael@0 148 */
michael@0 149 public String getValue() {
michael@0 150 return Strings.valueOf(getDate());
michael@0 151 }
michael@0 152
michael@0 153 /**
michael@0 154 * Publically available method to update the current timezone.
michael@0 155 * @param timezone a timezone instance
michael@0 156 */
michael@0 157 public void setTimeZone(final TimeZone timezone) {
michael@0 158 updateTimeZone(timezone);
michael@0 159 }
michael@0 160
michael@0 161 /**
michael@0 162 * @return the timezone
michael@0 163 */
michael@0 164 public final TimeZone getTimeZone() {
michael@0 165 return timeZone;
michael@0 166 }
michael@0 167
michael@0 168 /**
michael@0 169 * {@inheritDoc}
michael@0 170 */
michael@0 171 public int hashCode() {
michael@0 172 return getDate().hashCode();
michael@0 173 }
michael@0 174
michael@0 175 /**
michael@0 176 * Updates the timezone associated with the property's value. If the specified timezone is equivalent to UTC any
michael@0 177 * existing TZID parameters will be removed. Note that this method is only applicable where the current date is an
michael@0 178 * instance of <code>DateTime</code>. For all other cases an <code>UnsupportedOperationException</code> will be
michael@0 179 * thrown.
michael@0 180 * @param vTimeZone
michael@0 181 */
michael@0 182 private void updateTimeZone(final TimeZone timezone) {
michael@0 183 this.timeZone = timezone;
michael@0 184 if (timezone != null) {
michael@0 185 if (getDate() != null && !(getDate() instanceof DateTime)) {
michael@0 186 throw new UnsupportedOperationException(
michael@0 187 "TimeZone is not applicable to current value");
michael@0 188 }
michael@0 189 if (getDate() != null) {
michael@0 190 ((DateTime) getDate()).setTimeZone(timezone);
michael@0 191 }
michael@0 192
michael@0 193 getParameters().replace(new TzId(timezone.getID()));
michael@0 194 }
michael@0 195 else {
michael@0 196 // use setUtc() to reset timezone..
michael@0 197 setUtc(isUtc());
michael@0 198 }
michael@0 199 }
michael@0 200
michael@0 201 /**
michael@0 202 * Resets the VTIMEZONE associated with the property. If utc is true, any TZID parameters are removed and the Java
michael@0 203 * timezone is updated to UTC time. If utc is false, TZID parameters are removed and the Java timezone is set to the
michael@0 204 * default timezone (i.e. represents a "floating" local time)
michael@0 205 * @param utc a UTC value
michael@0 206 */
michael@0 207 public final void setUtc(final boolean utc) {
michael@0 208 if (getDate() != null && (getDate() instanceof DateTime)) {
michael@0 209 ((DateTime) getDate()).setUtc(utc);
michael@0 210 }
michael@0 211 getParameters().remove(getParameter(Parameter.TZID));
michael@0 212 }
michael@0 213
michael@0 214 /**
michael@0 215 * Indicates whether the current date value is specified in UTC time.
michael@0 216 * @return true if the property is in UTC time, otherwise false
michael@0 217 */
michael@0 218 public final boolean isUtc() {
michael@0 219 if (getDate() instanceof DateTime) {
michael@0 220 return ((DateTime) getDate()).isUtc();
michael@0 221 }
michael@0 222 return false;
michael@0 223 }
michael@0 224
michael@0 225 /**
michael@0 226 * {@inheritDoc}
michael@0 227 */
michael@0 228 public void validate() throws ValidationException {
michael@0 229
michael@0 230 ParameterValidator.getInstance().assertOneOrLess(Parameter.VALUE,
michael@0 231 getParameters());
michael@0 232
michael@0 233 if (isUtc()) {
michael@0 234 ParameterValidator.getInstance().assertNone(Parameter.TZID,
michael@0 235 getParameters());
michael@0 236 }
michael@0 237 else {
michael@0 238 ParameterValidator.getInstance().assertOneOrLess(Parameter.TZID,
michael@0 239 getParameters());
michael@0 240 }
michael@0 241
michael@0 242 final Value value = (Value) getParameter(Parameter.VALUE);
michael@0 243
michael@0 244 if (getDate() instanceof DateTime) {
michael@0 245
michael@0 246 if (value != null && !Value.DATE_TIME.equals(value)) {
michael@0 247 throw new ValidationException("VALUE parameter [" + value
michael@0 248 + "] is invalid for DATE-TIME instance");
michael@0 249 }
michael@0 250
michael@0 251 final DateTime dateTime = (DateTime) date;
michael@0 252
michael@0 253 // ensure tzid matches date-time timezone..
michael@0 254 final Parameter tzId = getParameter(Parameter.TZID);
michael@0 255 if (dateTime.getTimeZone() != null
michael@0 256 && (tzId == null || !tzId.getValue().equals(
michael@0 257 dateTime.getTimeZone().getID()))) {
michael@0 258
michael@0 259 throw new ValidationException("TZID parameter [" + tzId
michael@0 260 + "] does not match the timezone ["
michael@0 261 + dateTime.getTimeZone().getID() + "]");
michael@0 262 }
michael@0 263 }
michael@0 264 else if (getDate() != null) {
michael@0 265
michael@0 266 if (value == null) {
michael@0 267 throw new ValidationException("VALUE parameter [" + Value.DATE
michael@0 268 + "] must be specified for DATE instance");
michael@0 269 }
michael@0 270 else if (!Value.DATE.equals(value)) {
michael@0 271 throw new ValidationException("VALUE parameter [" + value
michael@0 272 + "] is invalid for DATE instance");
michael@0 273 }
michael@0 274 }
michael@0 275 }
michael@0 276
michael@0 277 /**
michael@0 278 * {@inheritDoc}
michael@0 279 */
michael@0 280 public Property copy() throws IOException, URISyntaxException, ParseException {
michael@0 281 final Property copy = super.copy();
michael@0 282
michael@0 283 ((DateProperty) copy).timeZone = timeZone;
michael@0 284 ((DateProperty) copy).setValue(getValue());
michael@0 285
michael@0 286 return copy;
michael@0 287 }
michael@0 288 }

mercurial