net.fortuna.ical4j.model.component.VTimeZone Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ical4j Show documentation
Show all versions of ical4j Show documentation
A Java library for reading and writing iCalendar (*.ics) files
/**
* Copyright (c) 2012, Ben Fortuna
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* o Neither the name of Ben Fortuna nor the names of any other contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.fortuna.ical4j.model.component;
import net.fortuna.ical4j.model.*;
import net.fortuna.ical4j.model.property.LastModified;
import net.fortuna.ical4j.model.property.Method;
import net.fortuna.ical4j.model.property.TzId;
import net.fortuna.ical4j.model.property.TzUrl;
import net.fortuna.ical4j.util.Strings;
import net.fortuna.ical4j.validate.ComponentValidator;
import net.fortuna.ical4j.validate.ValidationException;
import net.fortuna.ical4j.validate.ValidationRule;
import net.fortuna.ical4j.validate.Validator;
import net.fortuna.ical4j.validate.component.VTimeZoneValidator;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.jooq.lambda.Unchecked;
import java.time.OffsetDateTime;
import java.time.temporal.Temporal;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import static net.fortuna.ical4j.model.Property.*;
import static net.fortuna.ical4j.validate.ValidationRule.ValidationType.One;
import static net.fortuna.ical4j.validate.ValidationRule.ValidationType.OneOrLess;
/**
* $Id$ [Apr 5, 2004]
*
* Defines an iCalendar VTIMEZONE component.
*
*
* 4.6.5 Time Zone Component
*
* Component Name: VTIMEZONE
*
* Purpose: Provide a grouping of component properties that defines a
* time zone.
*
* Formal Definition: A "VTIMEZONE" calendar component is defined by the
* following notation:
*
* timezonec = "BEGIN" ":" "VTIMEZONE" CRLF
*
* 2*(
*
* ; 'tzid' is required, but MUST NOT occur more
* ; than once
*
* tzid /
*
* ; 'last-mod' and 'tzurl' are optional,
* but MUST NOT occur more than once
*
* last-mod / tzurl /
*
* ; one of 'standardc' or 'daylightc' MUST occur
* ..; and each MAY occur more than once.
*
* standardc / daylightc /
*
* ; the following is optional,
* ; and MAY occur more than once
*
* x-prop
*
* )
*
* "END" ":" "VTIMEZONE" CRLF
*
* standardc = "BEGIN" ":" "STANDARD" CRLF
*
* tzprop
*
* "END" ":" "STANDARD" CRLF
*
* daylightc = "BEGIN" ":" "DAYLIGHT" CRLF
*
* tzprop
*
* "END" ":" "DAYLIGHT" CRLF
*
* tzprop = 3*(
*
* ; the following are each REQUIRED,
* ; but MUST NOT occur more than once
*
* dtstart / tzoffsetto / tzoffsetfrom /
*
* ; the following are optional,
* ; and MAY occur more than once
*
* comment / rdate / rrule / tzname / x-prop
*
* )
*
*
* @author Ben Fortuna
*/
public class VTimeZone extends CalendarComponent {
private static final long serialVersionUID = 5629679741050917815L;
private final Validator itipValidator = new VTimeZoneValidator();
private final Validator validator = new ComponentValidator<>(
new ValidationRule<>(One, TZID),
new ValidationRule<>(OneOrLess, LAST_MODIFIED, TZURL)
);
private ComponentList observances;
/**
* Default constructor.
*/
public VTimeZone() {
super(VTIMEZONE);
this.observances = new ComponentList<>();
}
/**
* Constructs a new instance containing the specified properties.
* @param properties a list of properties
*/
public VTimeZone(final PropertyList properties) {
super(VTIMEZONE, properties);
this.observances = new ComponentList<>();
}
/**
* Constructs a new vtimezone component with no properties and the specified list of type components.
* @param observances a list of type components
*/
public VTimeZone(final ComponentList observances) {
super(VTIMEZONE);
this.observances = observances;
}
/**
* Constructor.
* @param properties a list of properties
* @param observances a list of timezone types
*/
public VTimeZone(final PropertyList properties, final ComponentList observances) {
super(VTIMEZONE, properties);
this.observances = observances;
}
/**
* Add an observance definition to the timezone.
* @param observance the definition to add
*/
public void add(Observance observance) {
this.observances = (ComponentList) observances.add(observance);
}
/**
* {@inheritDoc}
*/
public final String toString() {
return BEGIN +
':' +
getName() +
Strings.LINE_SEPARATOR +
getProperties() +
observances +
END +
':' +
getName() +
Strings.LINE_SEPARATOR;
}
/**
* {@inheritDoc}
*/
public final void validate(final boolean recurse) throws ValidationException {
validator.validate(this);
/*
* ; 'tzid' is required, but MUST NOT occur more ; than once tzid /
*/
/*
* ; 'last-mod' and 'tzurl' are optional, but MUST NOT occur more than once last-mod / tzurl /
*/
/*
* ; one of 'standardc' or 'daylightc' MUST occur ..; and each MAY occur more than once. standardc / daylightc /
*/
if (!getObservances().getFirst(Observance.STANDARD).isPresent()
&& !getObservances().getFirst(Observance.DAYLIGHT).isPresent()) {
throw new ValidationException("Sub-components ["
+ Observance.STANDARD + "," + Observance.DAYLIGHT
+ "] must be specified at least once");
}
for (final Observance observance : getObservances().getAll()) {
observance.validate(recurse);
}
/*
* ; the following is optional, ; and MAY occur more than once x-prop
*/
if (recurse) {
validateProperties();
}
}
@Override
public void validate(Method method) throws ValidationException {
itipValidator.validate(this);
}
/**
* @return Returns the types.
*/
public final ComponentList getObservances() {
return observances;
}
/**
* Returns the latest applicable timezone observance for the specified date.
* @param date the latest possible date for a timezone observance onset
* @return the latest applicable timezone observance for the specified date or null if there are no applicable
* observances
*/
public final Observance getApplicableObservance(final Temporal date) {
return getApplicableObservance(date, getObservances().getAll());
}
/**
* Returns the latest applicable timezone observance for the specified date.
* @param date the latest possible date for a timezone observance onset
* @param observances a list of observances to choose from
* @return the latest applicable timezone observance for the specified date or null if there are no applicable
* observances
*/
public static Observance getApplicableObservance(final Temporal date, List observances) {
Observance latestObservance = null;
OffsetDateTime latestOnset = null;
for (final Observance observance : observances) {
final OffsetDateTime onset = observance.getLatestOnset(date);
if (latestOnset == null || (onset != null && onset.isAfter(latestOnset))) {
latestOnset = onset;
latestObservance = observance;
}
}
return latestObservance;
}
/**
* @return the mandatory timezone identifier property
* @deprecated use {@link VTimeZone#getProperty(String)}
*/
@Deprecated
public final Optional getTimeZoneId() {
return getProperty(TZID);
}
/**
* @return the optional last-modified property
* @deprecated use {@link VTimeZone#getProperty(String)}
*/
@Deprecated
public final Optional getLastModified() {
return getProperty(LAST_MODIFIED);
}
/**
* @return the optional timezone url property
* @deprecated use {@link VTimeZone#getProperty(String)}
*/
@Deprecated
public final Optional getTimeZoneUrl() {
return getProperty(TZURL);
}
/**
* {@inheritDoc}
*/
public boolean equals(final Object arg0) {
if (arg0 instanceof VTimeZone) {
return super.equals(arg0)
&& Objects.equals(observances, ((VTimeZone) arg0)
.getObservances());
}
return super.equals(arg0);
}
/**
* {@inheritDoc}
*/
public int hashCode() {
return new HashCodeBuilder().append(getName()).append(getProperties())
.append(getObservances()).toHashCode();
}
/**
* Overrides default copy method to add support for copying observance sub-components.
* @return a copy of the instance
* @see net.fortuna.ical4j.model.Component#copy()
*/
public VTimeZone copy() {
return newFactory().createComponent(
new PropertyList(properties.getAll().stream()
.map(Unchecked.function(Property::copy)).collect(Collectors.toList())),
new ComponentList<>(observances.getAll().stream()
.map(Unchecked.function(Observance::copy)).collect(Collectors.toList())));
}
@Override
protected ComponentFactory newFactory() {
return new Factory();
}
public static class Factory extends Content.Factory implements ComponentFactory {
public Factory() {
super(VTIMEZONE);
}
@Override
public VTimeZone createComponent() {
return new VTimeZone();
}
@Override
public VTimeZone createComponent(PropertyList properties) {
return new VTimeZone(properties);
}
@Override @SuppressWarnings("unchecked")
public VTimeZone createComponent(PropertyList properties, ComponentList> subComponents) {
return new VTimeZone(properties, (ComponentList) subComponents);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy