Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Sep 24, 2019
1 parent 344c91b commit 7353603
Show file tree
Hide file tree
Showing 13 changed files with 723 additions and 728 deletions.
96 changes: 65 additions & 31 deletions src/main/java/net/fortuna/ical4j/model/TemporalAdapter.java
Expand Up @@ -3,11 +3,7 @@
import net.fortuna.ical4j.model.parameter.TzId;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.time.*;
import java.time.chrono.ChronoZonedDateTime;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -42,15 +38,31 @@ public class TemporalAdapter<T extends Temporal> implements Serializable {
private static CalendarDateFormat PARSE_FORMAT = new CalendarDateFormat(
"yyyyMMdd['T'HHmmss[X]]", Instant::from, LocalDateTime::from, LocalDate::from);

private final T temporal;
private final String valueString;

private final TzId tzId;

private transient T temporal;

public TemporalAdapter(TemporalAdapter<T> adapter) {
this.temporal = adapter.temporal;
this.valueString = adapter.valueString;
this.tzId = adapter.tzId;
}

public TemporalAdapter(T temporal) {
Objects.requireNonNull(temporal, "temporal");
this.temporal = temporal;
this.valueString = toString(temporal);
if (ChronoUnit.SECONDS.isSupportedBy(temporal) && !isFloating(temporal) && !isUtc(temporal)) {
this.tzId = new TzId.Factory().createParameter(ZoneId.systemDefault().getId());
} else {
this.tzId = null;
}
}

private TemporalAdapter(String valueString) {
this(valueString, null);
}

/**
Expand All @@ -61,45 +73,65 @@ public TemporalAdapter(T temporal) {
* @param tzId a zone id to apply to the parsed value
*/
private TemporalAdapter(String value, TzId tzId) {
temporal = (T) Proxy.newProxyInstance(ChronoZonedDateTime.class.getClassLoader(),
new Class[]{ChronoZonedDateTime.class},
new InvocationHandler() {
private ChronoZonedDateTime<LocalDate> temporal;

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (temporal == null) {
temporal = CalendarDateFormat.FLOATING_DATE_TIME_FORMAT.parse(value, tzId.toZoneId());
}
return method.invoke(temporal, args);
}
});
this.valueString = value;
this.tzId = tzId;
}

public T getTemporal() {
if (temporal == null) {
synchronized (valueString) {
if (temporal == null) {
if (tzId != null) {
temporal = (T) CalendarDateFormat.FLOATING_DATE_TIME_FORMAT.parse(valueString, tzId.toZoneId());
} else {
temporal = (T) PARSE_FORMAT.parse(valueString);
}
/*
temporal = (T) Proxy.newProxyInstance(ChronoZonedDateTime.class.getClassLoader(),
new Class[]{ChronoZonedDateTime.class},
new InvocationHandler() {
private ChronoZonedDateTime<LocalDate> temporal;
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (temporal == null) {
temporal = CalendarDateFormat.FLOATING_DATE_TIME_FORMAT.parse(valueString, tzId.toZoneId());
}
return method.invoke(temporal, args);
}
});
*/
}
}
}
return temporal;
}

@Override
public String toString() {
return toString(getTemporal());
}

private String toString(T temporal) {
if (!ChronoUnit.SECONDS.isSupportedBy(temporal)) {
return toString(CalendarDateFormat.DATE_FORMAT);
return toString(CalendarDateFormat.DATE_FORMAT, temporal);
} else {
if (isFloating(temporal)) {
return toString(CalendarDateFormat.FLOATING_DATE_TIME_FORMAT);
} else if (isUtc(temporal)) {
return toString(CalendarDateFormat.UTC_DATE_TIME_FORMAT);
if (isFloating(getTemporal())) {
return toString(CalendarDateFormat.FLOATING_DATE_TIME_FORMAT, temporal);
} else if (isUtc(getTemporal())) {
return toString(CalendarDateFormat.UTC_DATE_TIME_FORMAT, temporal);
} else {
return toString(CalendarDateFormat.FLOATING_DATE_TIME_FORMAT, ZoneId.systemDefault());
return toString(CalendarDateFormat.FLOATING_DATE_TIME_FORMAT, ZoneId.systemDefault(), temporal);
}
}
}

public String toString(CalendarDateFormat format) {
private String toString(CalendarDateFormat format, T temporal) {
return format.format(temporal);
}

public String toString(CalendarDateFormat format, ZoneId zoneId) {
private String toString(CalendarDateFormat format, ZoneId zoneId, T temporal) {
return format.format(temporal, zoneId);
}

Expand All @@ -108,12 +140,12 @@ public ZonedDateTime toLocalTime() {
}

public ZonedDateTime toLocalTime(ZoneId zoneId) {
if (isFloating(temporal)) {
return ((LocalDateTime) temporal).atZone(zoneId);
} else if (isUtc(temporal)) {
return ((Instant) temporal).atZone(zoneId);
if (isFloating(getTemporal())) {
return ((LocalDate) getTemporal()).atStartOfDay().atZone(zoneId);
} else if (isUtc(getTemporal())) {
return ((Instant) getTemporal()).atZone(zoneId);
} else {
return ZonedDateTime.from(temporal);
return ZonedDateTime.from(getTemporal());
}
}

Expand Down Expand Up @@ -166,6 +198,8 @@ public static TemporalAdapter from(Date date) {
DateTime dateTime = (DateTime) date;
if (dateTime.isUtc()) {
temporal = date.toInstant();
} else if (dateTime.getTimeZone() == null) {
temporal = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
} else {
temporal = ZonedDateTime.ofInstant(date.toInstant(), dateTime.getTimeZone().toZoneId());
}
Expand Down
Expand Up @@ -57,7 +57,7 @@ private List<ZoneOffsetTransitionRule> buildTransitionRules(List<Observance> obs
}

public ZoneRules build() {
Observance current = vTimeZone.getApplicableObservance(Instant.now(),
Observance current = VTimeZone.getApplicableObservance(Instant.now(),
vTimeZone.getObservances().getComponents(Observance.STANDARD));
ZoneOffset standardOffset = current.getOffsetTo().getOffset();
ZoneOffset wallOffset = current.getOffsetFrom().getOffset();
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/fortuna/ical4j/model/parameter/TzId.java
Expand Up @@ -34,7 +34,6 @@
import net.fortuna.ical4j.model.*;
import net.fortuna.ical4j.util.Strings;

import java.net.URISyntaxException;
import java.time.ZoneId;

/**
Expand Down Expand Up @@ -96,14 +95,14 @@ public final String getValue() {
return value;
}

public static class Factory extends Content.Factory implements ParameterFactory {
public static class Factory extends Content.Factory implements ParameterFactory<TzId> {
private static final long serialVersionUID = 1L;

public Factory() {
super(TZID);
}

public Parameter createParameter(final String value) throws URISyntaxException {
public TzId createParameter(final String value) {
return new TzId(Strings.unescape(value));
}
}
Expand Down
36 changes: 19 additions & 17 deletions src/main/java/net/fortuna/ical4j/model/property/DateProperty.java
Expand Up @@ -201,25 +201,27 @@ public void validate() throws ValidationException {

final Value value = getParameter(Parameter.VALUE);

if (date.getTemporal() instanceof LocalDate) {
if (value == null) {
throw new ValidationException("VALUE parameter [" + Value.DATE + "] must be specified for DATE instance");
} else if (!Value.DATE.equals(value)) {
throw new ValidationException("VALUE parameter [" + value + "] is invalid for DATE instance");
}
} else {
if (value != null && !Value.DATE_TIME.equals(value)) {
throw new ValidationException("VALUE parameter [" + value + "] is invalid for DATE-TIME instance");
}
if (date != null) {
if (date.getTemporal() instanceof LocalDate) {
if (value == null) {
throw new ValidationException("VALUE parameter [" + Value.DATE + "] must be specified for DATE instance");
} else if (!Value.DATE.equals(value)) {
throw new ValidationException("VALUE parameter [" + value + "] is invalid for DATE instance");
}
} else {
if (value != null && !Value.DATE_TIME.equals(value)) {
throw new ValidationException("VALUE parameter [" + value + "] is invalid for DATE-TIME instance");
}

if (date.getTemporal() instanceof ZonedDateTime) {
ZonedDateTime dateTime = (ZonedDateTime) date.getTemporal();
if (date.getTemporal() instanceof ZonedDateTime) {
ZonedDateTime dateTime = (ZonedDateTime) date.getTemporal();

// ensure tzid matches date-time timezone..
final Parameter tzId = getParameter(Parameter.TZID);
if (tzId == null || !tzId.getValue().equals(dateTime.getZone().getId())) {
throw new ValidationException("TZID parameter [" + tzId + "] does not match the timezone ["
+ dateTime.getZone().getId() + "]");
// ensure tzid matches date-time timezone..
final TzId tzId = getParameter(Parameter.TZID);
if (tzId == null || !tzId.toZoneId().equals(dateTime.getZone())) {
throw new ValidationException("TZID parameter [" + tzId + "] does not match the timezone ["
+ dateTime.getZone().getId() + "]");
}
}
}
}
Expand Down
@@ -1,6 +1,4 @@
net.fortuna.ical4j.transform.rfc5545.CreatedPropertyRule
net.fortuna.ical4j.transform.rfc5545.DatePropertyRule
net.fortuna.ical4j.transform.rfc5545.DateListPropertyRule
net.fortuna.ical4j.transform.rfc5545.TzIdRule
net.fortuna.ical4j.transform.rfc5545.DTStampRule
net.fortuna.ical4j.transform.rfc5545.AttendeePropertyRule
net.fortuna.ical4j.transform.rfc5545.AttendeePropertyRule
Expand Up @@ -18,6 +18,6 @@ class ZoneRulesProviderImplTest extends Specification {
ZoneRules zoneRules = zoneRulesProvider.provideRules('Australia/Melbourne', false)

then: 'an appropriate rules instance is provided'
zoneRules.getStandardOffset(Instant.now()) == ZoneOffset.ofHours(10)
zoneRules != null && zoneRules.getStandardOffset(Instant.now()) == ZoneOffset.ofHours(10)
}
}
38 changes: 0 additions & 38 deletions src/test/resources/samples/invalid/calconnect.ics

This file was deleted.

0 comments on commit 7353603

Please sign in to comment.