Skip to content

Commit

Permalink
Provide convenience methods for required objects
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jun 13, 2020
1 parent ba84b41 commit 42af23d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
12 changes: 11 additions & 1 deletion src/main/java/net/fortuna/ical4j/model/Calendar.java
Expand Up @@ -225,7 +225,12 @@ public final <C extends CalendarComponent> ComponentList<C> getComponents(final
* @return the first matching component in the component list with the specified name
*/
public final <T extends CalendarComponent> Optional<T> getComponent(final String name) {
return (Optional<T>) getComponents().getComponent(name);
return getComponents().getComponent(name);
}

public final <T extends CalendarComponent> T getRequiredComponent(String name) throws ConstraintViolationException {
Optional<T> component = getComponent(name);
return component.orElseThrow(() -> new ConstraintViolationException(String.format("Missing %s component", name)));
}

/**
Expand Down Expand Up @@ -253,6 +258,11 @@ public final <T extends Property> Optional<T> getProperty(final String name) {
return getProperties().getProperty(name);
}

public final <T extends Property> T getRequiredProperty(String name) throws ConstraintViolationException {
Optional<T> property = getProperty(name);
return property.orElseThrow(() -> new ConstraintViolationException(String.format("Missing %s property", name)));
}

/**
* Perform validation on the calendar, its properties and its components in its current state.
* @throws ValidationException where the calendar is not in a valid state
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/fortuna/ical4j/model/Component.java
Expand Up @@ -198,9 +198,9 @@ public <T extends Property> Optional<T> getProperty(final String name) {
* @return the first matching property in the property list with the specified name
* @throws ConstraintViolationException when a property is not found
*/
protected final <T extends Property> T getRequiredProperty(String name) throws ConstraintViolationException {
return (T) getProperties().getProperty(name).orElseThrow(() ->
new ConstraintViolationException(String.format("Missing %s property", name)));
public final <T extends Property> T getRequiredProperty(String name) throws ConstraintViolationException {
Optional<T> property = getProperty(name);
return property.orElseThrow(() -> new ConstraintViolationException(String.format("Missing %s property", name)));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/fortuna/ical4j/model/ComponentList.java
Expand Up @@ -95,10 +95,10 @@ public final String toString() {
* @param aName name of component to return
* @return a component or null if no matching component found
*/
public final Optional<T> getComponent(final String aName) {
public final <R extends T> Optional<R> getComponent(final String aName) {
for (final T c : this) {
if (c.getName().equals(aName)) {
return Optional.of(c);
return Optional.of((R) c);
}
}
return Optional.empty();
Expand Down
26 changes: 13 additions & 13 deletions src/test/java/net/fortuna/ical4j/model/property/ExDateTest.java
Expand Up @@ -83,8 +83,8 @@ public void testTimeZones() throws Exception {
CalendarBuilder builder = new CalendarBuilder();
Calendar calendar = builder.build(getClass().getResourceAsStream("/samples/valid/EXDATE.ics"));

Optional<VEvent> event = calendar.getComponent(Component.VEVENT);
List<Property> exdates = event.get().getProperties(Property.EXDATE);
VEvent event = calendar.getRequiredComponent(Component.VEVENT);
List<Property> exdates = event.getProperties(Property.EXDATE);
for (Property exDate : exdates) {
assertTrue("This EXDATE should have a timezone", exDate.getParameter(Parameter.TZID).isPresent());
}
Expand All @@ -94,25 +94,25 @@ public void testDstOnlyVTimeZones() throws Exception {
CalendarBuilder builder = new CalendarBuilder();

Calendar ical = builder.build(getClass().getResourceAsStream("/samples/valid/dst-only-vtimezone.ics"));
Optional<VTimeZone> vTZ = ical.getComponent(VTimeZone.VTIMEZONE);
VTimeZone vTZ = ical.getRequiredComponent(VTimeZone.VTIMEZONE);

String id = vTZ.get().getProperty(Property.TZID).get().getValue();
String id = vTZ.getRequiredProperty(Property.TZID).getValue();
assertEquals("Europe/Berlin", id);
assertEquals(vTZ.get().getObservances().get(0), vTZ.get().getApplicableObservance(TemporalAdapter.parse("20180403").getTemporal()));
assertEquals(vTZ.getObservances().get(0), vTZ.getApplicableObservance(TemporalAdapter.parse("20180403").getTemporal()));

Optional<VEvent> vEvent = ical.getComponent(VEvent.VEVENT);
Optional<DtStart<?>> start = vEvent.get().getStartDate();
Optional<TzId> startTzId = start.get().getParameter(Parameter.TZID);
assertTrue(startTzId.equals(vTZ.get().getTimeZoneId().get().getParameter(Parameter.TZID)));
assertEquals(1522738800000L, Instant.from(start.get().getDate()).toEpochMilli());
VEvent vEvent = ical.getRequiredComponent(VEvent.VEVENT);
DtStart<?> start = vEvent.getRequiredProperty("DTSTART");
Optional<TzId> startTzId = start.getParameter(Parameter.TZID);
assertEquals(startTzId, vTZ.getRequiredProperty("TZID").getParameter(Parameter.TZID));
assertEquals(1522738800000L, Instant.from(start.getDate()).toEpochMilli());
}

public void testShouldPreserveUtcTimezoneForExDate() throws Exception {
CalendarBuilder builder = new CalendarBuilder();
Calendar calendar = builder.build(getClass().getResourceAsStream("/samples/valid/EXDATE-IN-UTC.ics"));

Optional<VEvent> event = calendar.getComponent(Component.VEVENT);
List<Property> exdates = event.get().getProperties(Property.EXDATE);
VEvent event = calendar.getRequiredComponent(Component.VEVENT);
List<Property> exdates = event.getProperties(Property.EXDATE);
for (Property exDate : exdates) {
for (Instant dateEx : ((ExDate<Instant>) exDate).getDates().getDates()) {
assertNotNull(dateEx);
Expand All @@ -125,7 +125,7 @@ public void testShouldPreserveUtcTimezoneForExDate() throws Exception {
*/
public void testRelaxedParsing() throws DateTimeParseException {
try {
new ExDate(new ArrayList<>(), "20080315");
new ExDate<Instant>(new ArrayList<>(), "20080315");
fail("Should throw DateTimeParseException");
} catch (DateTimeParseException pe) {
LOG.trace("Caught exception: " + pe.getMessage());
Expand Down

0 comments on commit 42af23d

Please sign in to comment.