Skip to content

Commit

Permalink
Constraint violation is now an unchecked exception
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jul 29, 2021
1 parent 4cecf11 commit c0d6763
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 64 deletions.
20 changes: 7 additions & 13 deletions src/main/java/net/fortuna/ical4j/model/ComponentGroup.java
Expand Up @@ -33,18 +33,16 @@ public class ComponentGroup<C extends Component> {

private final List<C> components;

private final Predicate<T> componentPredicate;
private final Predicate<C> componentPredicate;

public ComponentGroup(List<C> components, Uid uid) {
this(components, uid, null);
}

public ComponentGroup(List<C> components, Uid uid, RecurrenceId recurrenceId) {
this.components = components;

Predicate<C> componentPredicate;
if (recurrenceId != null) {
componentPredicate = new PropertyEqualToRule<T>(uid).and(new PropertyEqualToRule<>(recurrenceId));
componentPredicate = new PropertyEqualToRule<C>(uid).and(new PropertyEqualToRule<>(recurrenceId));
} else {
componentPredicate = new PropertyEqualToRule<>(uid);
}
Expand Down Expand Up @@ -97,15 +95,11 @@ public <T extends Temporal> List<Period<T>> calculateRecurrenceSet(final Period<

List<Period<T>> finalPeriods = new ArrayList<>(periods);
overrides.forEach(component -> {
try {
RecurrenceId<?> recurrenceId = component.getProperties().getRequired(Property.RECURRENCE_ID);
finalPeriods.removeIf(p -> p.getStart().equals(recurrenceId.getDate()));
component.calculateRecurrenceSet(period).stream()
.filter(p -> p.getStart().equals(recurrenceId.getDate()))
.forEach(finalPeriods::add);
} catch (ConstraintViolationException cve) {
// Should never reach as we previously determined existence of RECURRENCE_ID property..
}
RecurrenceId<?> recurrenceId = component.getProperties().getRequired(Property.RECURRENCE_ID);
finalPeriods.removeIf(p -> p.getStart().equals(recurrenceId.getDate()));
component.calculateRecurrenceSet(period).stream()
.filter(p -> p.getStart().equals(recurrenceId.getDate()))
.forEach(finalPeriods::add);
});

// Natural sort of final list..
Expand Down
Expand Up @@ -40,7 +40,7 @@
* @author Ben
*
*/
public class ConstraintViolationException extends Exception {
public class ConstraintViolationException extends RuntimeException {

private static final long serialVersionUID = 6728653187698888940L;

Expand Down
Expand Up @@ -56,7 +56,7 @@ protected ZoneRules provideRules(String zoneId, boolean forCaching) {
VTimeZone vTimeZone = zoneLoader.loadVTimeZone(localZoneId);
retVal = new ZoneRulesBuilder().vTimeZone(vTimeZone).build();
zoneRulesMap.put(zoneId, retVal);
} catch (IOException | ParserException | ConstraintViolationException e) {
} catch (IOException | ParserException e) {
LOG.error("Error loading zone rules", e);
}
}
Expand Down
22 changes: 7 additions & 15 deletions src/main/java/net/fortuna/ical4j/model/TimeZone.java
Expand Up @@ -103,12 +103,8 @@ public final int getOffset(final int era, final int year, final int month, final
OffsetDateTime date = OffsetDateTime.of(year, month + 1, dayOfMonth, hour, minute, second, ms * 1000, ZoneOffset.ofTotalSeconds(getRawOffset() / 1000));
final Observance observance = vTimeZone.getApplicableObservance(date);
if (observance != null) {
try {
final TzOffsetTo offset = observance.getProperties().getRequired(Property.TZOFFSETTO);
return (int) (offset.getOffset().getTotalSeconds() * 1000L);
} catch (ConstraintViolationException cve) {
LOG.error("Invalid observance", cve);
}
final TzOffsetTo offset = observance.getProperties().getRequired(Property.TZOFFSETTO);
return (int) (offset.getOffset().getTotalSeconds() * 1000L);
}
return 0;
}
Expand All @@ -120,15 +116,11 @@ public final int getOffset(final int era, final int year, final int month, final
public int getOffset(long date) {
final Observance observance = vTimeZone.getApplicableObservance(Instant.ofEpochMilli(date));
if (observance != null) {
try {
final TzOffsetTo offset = observance.getProperties().getRequired(Property.TZOFFSETTO);
if ((offset.getOffset().getTotalSeconds() * 1000L) < getRawOffset()) {
return getRawOffset();
} else {
return (int) (offset.getOffset().getTotalSeconds() * 1000L);
}
} catch (ConstraintViolationException cve) {
LOG.error("Invalid observance", cve);
final TzOffsetTo offset = observance.getProperties().getRequired(Property.TZOFFSETTO);
if ((offset.getOffset().getTotalSeconds() * 1000L) < getRawOffset()) {
return getRawOffset();
} else {
return (int) (offset.getOffset().getTotalSeconds() * 1000L);
}
}
return 0;
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/net/fortuna/ical4j/model/TimeZoneRegistryImpl.java
Expand Up @@ -147,15 +147,11 @@ public final void register(final TimeZone timezone, boolean update) {
}

// use latest timezone definition to build zone rules..
try {
ZoneRules newZoneRules = new ZoneRulesBuilder().vTimeZone(timezones.get(timezone.getID()).getVTimeZone())
.build();
String globalId = "ical4j~" + UUID.randomUUID().toString();
zoneIds.put(globalId, timezone.getID());
zoneRules.put(globalId, newZoneRules);
} catch (ConstraintViolationException cve) {
LoggerFactory.getLogger(TimeZoneRegistryImpl.class).error("Invalid timezone definition", cve);
}
ZoneRules newZoneRules = new ZoneRulesBuilder().vTimeZone(timezones.get(timezone.getID()).getVTimeZone())
.build();
String globalId = "ical4j~" + UUID.randomUUID().toString();
zoneIds.put(globalId, timezone.getID());
zoneRules.put(globalId, newZoneRules);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/fortuna/ical4j/model/ZoneRulesBuilder.java
Expand Up @@ -32,13 +32,13 @@ private List<ZoneOffsetTransition> buildTransitions(List<Observance> observances
for (Observance observance : observances) {
// ignore transitions that have no effect..
Optional<TzOffsetFrom> offsetFrom = observance.getProperties().getFirst(Property.TZOFFSETFROM);
Optional<TzOffsetTo> offsetTo = observance.getProperties().getFirst(Property.TZOFFSETTO);
TzOffsetTo offsetTo = observance.getProperties().getRequired(Property.TZOFFSETTO);

if (offsetFrom.isPresent() && !offsetFrom.get().getOffset().equals(offsetTo.get().getOffset())) {
if (offsetFrom.isPresent() && !offsetFrom.get().getOffset().equals(offsetTo.getOffset())) {
Optional<DtStart<LocalDateTime>> startDate = observance.getProperties().getFirst(Property.DTSTART);
if (startDate.isPresent()) {
transitions.add(ZoneOffsetTransition.of(startDate.get().getDate(),
offsetFrom.get().getOffset(), offsetTo.get().getOffset()));
offsetFrom.get().getOffset(), offsetTo.getOffset()));
} else {
throw new CalendarException("Missing DTSTART property");
}
Expand Down
18 changes: 2 additions & 16 deletions src/main/java/net/fortuna/ical4j/model/component/Observance.java
Expand Up @@ -163,23 +163,9 @@ public final OffsetDateTime getLatestOnset(final Temporal date) {
throw new UnsupportedOperationException("Unable to get timezone observance for date-only temporal.");
}

TzOffsetTo offsetTo;
try {
offsetTo = getProperties().getRequired(TZOFFSETTO);
} catch (ConstraintViolationException e) {
Logger log = LoggerFactory.getLogger(Observance.class);
log.error("Unexpected error calculating latest onset", e);
return null;
}
TzOffsetTo offsetTo = getProperties().getRequired(TZOFFSETTO);

TzOffsetFrom offsetFrom;
try {
offsetFrom = getProperties().getRequired(TZOFFSETFROM);
} catch (ConstraintViolationException e) {
Logger log = LoggerFactory.getLogger(Observance.class);
log.error("Unexpected error calculating latest onset", e);
return null;
}
TzOffsetFrom offsetFrom = getProperties().getRequired(TZOFFSETFROM);

OffsetDateTime offsetDate = LocalDateTime.ofInstant(Instant.from(date), ZoneOffset.UTC).atOffset(
offsetTo.getOffset());
Expand Down
Expand Up @@ -321,12 +321,8 @@ public VFreeBusy(final VFreeBusy request, final List<CalendarComponent> componen
final DtStart<?> start;
final DtEnd<?> end;

try {
start = request.getProperties().getRequired(DTSTART);
end = request.getProperties().getRequired(DTEND);
} catch (ConstraintViolationException cve) {
throw new ValidationException("Missing required property", cve);
}
start = request.getProperties().getRequired(DTSTART);
end = request.getProperties().getRequired(DTEND);

// ensure the request is valid..
request.validate();
Expand Down

0 comments on commit c0d6763

Please sign in to comment.