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

changeset 0
fb9019fb1bf7
equal deleted inserted replaced
-1:000000000000 0:00572264ff79
1 /**
2 * Copyright (c) 2012, Ben Fortuna
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * o Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * o Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * o Neither the name of Ben Fortuna nor the names of any other contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 package net.fortuna.ical4j.model.property;
33
34 import java.io.IOException;
35 import java.net.URISyntaxException;
36 import java.text.ParseException;
37
38 import net.fortuna.ical4j.model.Date;
39 import net.fortuna.ical4j.model.DateTime;
40 import net.fortuna.ical4j.model.Parameter;
41 import net.fortuna.ical4j.model.ParameterList;
42 import net.fortuna.ical4j.model.Property;
43 import net.fortuna.ical4j.model.PropertyFactory;
44 import net.fortuna.ical4j.model.TimeZone;
45 import net.fortuna.ical4j.model.ValidationException;
46 import net.fortuna.ical4j.model.parameter.TzId;
47 import net.fortuna.ical4j.model.parameter.Value;
48 import net.fortuna.ical4j.util.ParameterValidator;
49 import net.fortuna.ical4j.util.Strings;
50
51 /**
52 * $Id$
53 *
54 * Created on 9/07/2005
55 *
56 * Base class for properties with a DATE or DATE-TIME value. Note that some sub-classes may only allow either a DATE or
57 * a DATE-TIME value, for which additional rules/validation should be specified.
58 * @author Ben Fortuna
59 */
60 public abstract class DateProperty extends Property {
61
62 private static final long serialVersionUID = 3160883132732961321L;
63
64 private Date date;
65
66 private TimeZone timeZone;
67
68 /**
69 * @param name the property name
70 * @param parameters a list of initial parameters
71 */
72 public DateProperty(final String name, final ParameterList parameters, PropertyFactory factory) {
73 super(name, parameters, factory);
74 }
75
76 /**
77 * @param name the property name
78 */
79 public DateProperty(final String name, PropertyFactory factory) {
80 super(name, factory);
81 }
82
83 /**
84 * Creates a new instance of the named property with an initial timezone.
85 * @param name property name
86 * @param timezone initial timezone
87 */
88 public DateProperty(final String name, TimeZone timezone, PropertyFactory factory) {
89 super(name, factory);
90 updateTimeZone(timezone);
91 }
92
93 /**
94 * @return Returns the date.
95 */
96 public final Date getDate() {
97 return date;
98 }
99
100 /**
101 * Sets the date value of this property. The timezone and value of this
102 * instance will also be updated accordingly.
103 * @param date The date to set.
104 */
105 public final void setDate(final Date date) {
106 this.date = date;
107 if (date instanceof DateTime) {
108 if (Value.DATE.equals(getParameter(Parameter.VALUE))) {
109 getParameters().replace(Value.DATE_TIME);
110 }
111 updateTimeZone(((DateTime) date).getTimeZone());
112 }
113 else {
114 if (date != null) {
115 getParameters().replace(Value.DATE);
116 }
117 /*
118 else {
119 getParameters().removeAll(Parameter.VALUE);
120 }
121 */
122 // ensure timezone is null for VALUE=DATE or null properties..
123 updateTimeZone(null);
124 }
125 }
126
127 /**
128 * Default setValue() implementation. Allows for either DATE or DATE-TIME values.
129 *
130 * @param value a string representation of a DATE or DATE-TIME value
131 * @throws ParseException where the specified value is not a valid DATE or DATE-TIME
132 * representation
133 */
134 public void setValue(final String value) throws ParseException {
135 // value can be either a date-time or a date..
136 if (Value.DATE.equals(getParameter(Parameter.VALUE))) {
137 // ensure timezone is null for VALUE=DATE properties..
138 updateTimeZone(null);
139 this.date = new Date(value);
140 }
141 else {
142 this.date = new DateTime(value, timeZone);
143 }
144 }
145
146 /**
147 * {@inheritDoc}
148 */
149 public String getValue() {
150 return Strings.valueOf(getDate());
151 }
152
153 /**
154 * Publically available method to update the current timezone.
155 * @param timezone a timezone instance
156 */
157 public void setTimeZone(final TimeZone timezone) {
158 updateTimeZone(timezone);
159 }
160
161 /**
162 * @return the timezone
163 */
164 public final TimeZone getTimeZone() {
165 return timeZone;
166 }
167
168 /**
169 * {@inheritDoc}
170 */
171 public int hashCode() {
172 return getDate().hashCode();
173 }
174
175 /**
176 * Updates the timezone associated with the property's value. If the specified timezone is equivalent to UTC any
177 * existing TZID parameters will be removed. Note that this method is only applicable where the current date is an
178 * instance of <code>DateTime</code>. For all other cases an <code>UnsupportedOperationException</code> will be
179 * thrown.
180 * @param vTimeZone
181 */
182 private void updateTimeZone(final TimeZone timezone) {
183 this.timeZone = timezone;
184 if (timezone != null) {
185 if (getDate() != null && !(getDate() instanceof DateTime)) {
186 throw new UnsupportedOperationException(
187 "TimeZone is not applicable to current value");
188 }
189 if (getDate() != null) {
190 ((DateTime) getDate()).setTimeZone(timezone);
191 }
192
193 getParameters().replace(new TzId(timezone.getID()));
194 }
195 else {
196 // use setUtc() to reset timezone..
197 setUtc(isUtc());
198 }
199 }
200
201 /**
202 * Resets the VTIMEZONE associated with the property. If utc is true, any TZID parameters are removed and the Java
203 * timezone is updated to UTC time. If utc is false, TZID parameters are removed and the Java timezone is set to the
204 * default timezone (i.e. represents a "floating" local time)
205 * @param utc a UTC value
206 */
207 public final void setUtc(final boolean utc) {
208 if (getDate() != null && (getDate() instanceof DateTime)) {
209 ((DateTime) getDate()).setUtc(utc);
210 }
211 getParameters().remove(getParameter(Parameter.TZID));
212 }
213
214 /**
215 * Indicates whether the current date value is specified in UTC time.
216 * @return true if the property is in UTC time, otherwise false
217 */
218 public final boolean isUtc() {
219 if (getDate() instanceof DateTime) {
220 return ((DateTime) getDate()).isUtc();
221 }
222 return false;
223 }
224
225 /**
226 * {@inheritDoc}
227 */
228 public void validate() throws ValidationException {
229
230 ParameterValidator.getInstance().assertOneOrLess(Parameter.VALUE,
231 getParameters());
232
233 if (isUtc()) {
234 ParameterValidator.getInstance().assertNone(Parameter.TZID,
235 getParameters());
236 }
237 else {
238 ParameterValidator.getInstance().assertOneOrLess(Parameter.TZID,
239 getParameters());
240 }
241
242 final Value value = (Value) getParameter(Parameter.VALUE);
243
244 if (getDate() instanceof DateTime) {
245
246 if (value != null && !Value.DATE_TIME.equals(value)) {
247 throw new ValidationException("VALUE parameter [" + value
248 + "] is invalid for DATE-TIME instance");
249 }
250
251 final DateTime dateTime = (DateTime) date;
252
253 // ensure tzid matches date-time timezone..
254 final Parameter tzId = getParameter(Parameter.TZID);
255 if (dateTime.getTimeZone() != null
256 && (tzId == null || !tzId.getValue().equals(
257 dateTime.getTimeZone().getID()))) {
258
259 throw new ValidationException("TZID parameter [" + tzId
260 + "] does not match the timezone ["
261 + dateTime.getTimeZone().getID() + "]");
262 }
263 }
264 else if (getDate() != null) {
265
266 if (value == null) {
267 throw new ValidationException("VALUE parameter [" + Value.DATE
268 + "] must be specified for DATE instance");
269 }
270 else if (!Value.DATE.equals(value)) {
271 throw new ValidationException("VALUE parameter [" + value
272 + "] is invalid for DATE instance");
273 }
274 }
275 }
276
277 /**
278 * {@inheritDoc}
279 */
280 public Property copy() throws IOException, URISyntaxException, ParseException {
281 final Property copy = super.copy();
282
283 ((DateProperty) copy).timeZone = timeZone;
284 ((DateProperty) copy).setValue(getValue());
285
286 return copy;
287 }
288 }

mercurial