diff --git a/src/main/java/net/fortuna/ical4j/model/Component.java b/src/main/java/net/fortuna/ical4j/model/Component.java index dc1351cf5..6186a73d5 100644 --- a/src/main/java/net/fortuna/ical4j/model/Component.java +++ b/src/main/java/net/fortuna/ical4j/model/Component.java @@ -190,6 +190,22 @@ public final Property getProperty(final String name) { return getProperties().getProperty(name); } + /** + * Convenience method for retrieving a named property. + * + * @param name name of the property to retrieve + * @param optional flag to indicate whether an exception should be thrown for missing property + * @return the first matching property in the property list with the specified name + * @throws ConstraintViolationException when a property is not found and the optional flag is false + */ + protected final Property getProperty(String name, boolean optional) throws ConstraintViolationException { + Property p = getProperties().getProperty(name); + if (p == null && !optional) { + throw new ConstraintViolationException(String.format("Missing %s property", name)); + } + return p; + } + /** * Perform validation on a component and its properties. * diff --git a/src/main/java/net/fortuna/ical4j/model/component/Observance.java b/src/main/java/net/fortuna/ical4j/model/component/Observance.java index b952be167..c36e23274 100644 --- a/src/main/java/net/fortuna/ical4j/model/component/Observance.java +++ b/src/main/java/net/fortuna/ical4j/model/component/Observance.java @@ -155,10 +155,7 @@ public final Date getLatestOnset(final Date date) { if (initialOnset == null) { try { - DtStart dtStart = (DtStart) getProperty(Property.DTSTART); - if (dtStart == null) { - throw new ConstraintViolationException("Missing DTSTART property"); - } + DtStart dtStart = (DtStart) getProperty(Property.DTSTART, false); initialOnset = applyOffsetFrom(calculateOnset(dtStart.getDate())); } catch (ParseException e) { Logger log = LoggerFactory.getLogger(Observance.class);