Skip to content

Commit

Permalink
Added convenience method for retrieving required property
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Oct 6, 2016
1 parent c4fbee1 commit 7328d41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/main/java/net/fortuna/ical4j/model/Component.java
Expand Up @@ -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.
*
Expand Down
Expand Up @@ -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);
Expand Down

0 comments on commit 7328d41

Please sign in to comment.