Permalink
Browse files

Added convenience method for retrieving required property

  • Loading branch information...
benfortuna committed Oct 6, 2016
1 parent c4fbee1 commit 7328d4140facbfdde38419eab6c3bf775704e05a
@@ -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.
*
@@ -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);

0 comments on commit 7328d41

Please sign in to comment.