Permalink
Browse files

Use closures to reduce duplication of code

  • Loading branch information...
benfortuna committed Oct 5, 2016
1 parent 7dd01ec commit 4860a732968ca910ed477c460431e2279d2aedf8
@@ -37,6 +37,10 @@
import net.fortuna.ical4j.model.property.DtStart;
import net.fortuna.ical4j.validate.PropertyValidator;
import net.fortuna.ical4j.validate.ValidationException;
+import org.apache.commons.collections4.Closure;
+import org.apache.commons.collections4.CollectionUtils;
+
+import java.util.Arrays;
/**
* $Id$ [05-Apr-2004]
@@ -113,12 +117,12 @@ public final void validate(final boolean recurse)
/*
* ; dtstamp / dtstart / uid are required, but MUST NOT occur more than once /
*/
- PropertyValidator.getInstance().assertOne(Property.DTSTART,
- getProperties());
- PropertyValidator.getInstance().assertOne(Property.DTSTAMP,
- getProperties());
- PropertyValidator.getInstance().assertOne(Property.UID,
- getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.DTSTART, Property.DTSTAMP, Property.UID), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOne(input, getProperties());
+ }
+ });
/* If specified, the "DTSTART" and "DTEND" properties in
* "VAVAILABILITY" components and "AVAILABLE" sub-components MUST be
@@ -138,16 +142,13 @@ public final void validate(final boolean recurse)
* created / last-mod / recurid / rrule /
* summary /
*/
- PropertyValidator.getInstance().assertOneOrLess(Property.CREATED,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LAST_MODIFIED,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RECURRENCE_ID,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RRULE,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SUMMARY,
- getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.CREATED, Property.LAST_MODIFIED, Property.RECURRENCE_ID,
+ Property.RRULE, Property.SUMMARY), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, getProperties());
+ }
+ });
/*
; either a 'dtend' or a 'duration' is required
@@ -41,6 +41,10 @@
import net.fortuna.ical4j.validate.PropertyValidator;
import net.fortuna.ical4j.validate.ValidationException;
import net.fortuna.ical4j.validate.Validator;
+import org.apache.commons.collections4.Closure;
+import org.apache.commons.collections4.CollectionUtils;
+
+import java.util.Arrays;
/**
* $Id$ [Apr 5, 2004]
@@ -179,12 +183,12 @@ public final void validate(final boolean recurse)
/*
* ; dtstamp / dtstart / uid are required, but MUST NOT occur more than once /
*/
- PropertyValidator.getInstance().assertOne(Property.DTSTART,
- getProperties());
- PropertyValidator.getInstance().assertOne(Property.DTSTAMP,
- getProperties());
- PropertyValidator.getInstance().assertOne(Property.UID,
- getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.DTSTART, Property.DTSTAMP, Property.UID), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOne(input, getProperties());
+ }
+ });
/* If specified, the "DTSTART" and "DTEND" properties in
* "VAVAILABILITY" components and "AVAILABLE" sub-components MUST be
@@ -225,20 +229,13 @@ public final void validate(final boolean recurse)
* busytype / created / last-mod /
* organizer / seq / summary / url /
*/
- PropertyValidator.getInstance().assertOneOrLess(Property.BUSYTYPE,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CREATED,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LAST_MODIFIED,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.ORGANIZER,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SEQUENCE,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SUMMARY,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.URL,
- getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.BUSYTYPE, Property.CREATED, Property.LAST_MODIFIED,
+ Property.ORGANIZER, Property.SEQUENCE, Property.SUMMARY, Property.URL), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, getProperties());
+ }
+ });
/*
* ; the following are optional, ; and MAY occur more than once
@@ -41,12 +41,15 @@
import net.fortuna.ical4j.validate.ValidationException;
import net.fortuna.ical4j.validate.Validator;
import net.fortuna.ical4j.validate.component.*;
+import org.apache.commons.collections4.Closure;
+import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -344,40 +347,15 @@ public final void validate(final boolean recurse) throws ValidationException {
* geo / last-mod / location / organizer / priority / dtstamp / seq / status / summary / transp / uid / url /
* recurid /
*/
- PropertyValidator.getInstance().assertOneOrLess(Property.CLASS,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CREATED,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DESCRIPTION,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DTSTART,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.GEO,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LAST_MODIFIED,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LOCATION,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.ORGANIZER,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.PRIORITY,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DTSTAMP,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SEQUENCE,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.STATUS,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SUMMARY,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.TRANSP,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.UID,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.URL,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RECURRENCE_ID,
- getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.CLASS, Property.CREATED, Property.DESCRIPTION,
+ Property.DTSTART, Property.GEO, Property.LAST_MODIFIED, Property.LOCATION, Property.ORGANIZER,
+ Property.PRIORITY, Property.DTSTAMP, Property.SEQUENCE, Property.STATUS, Property.SUMMARY,
+ Property.TRANSP, Property.UID, Property.URL, Property.RECURRENCE_ID), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, getProperties());
+ }
+ });
final Status status = (Status) getProperty(Property.STATUS);
if (status != null && !Status.VEVENT_TENTATIVE.getValue().equals(status.getValue())
@@ -41,7 +41,10 @@
import net.fortuna.ical4j.validate.component.VFreeBusyPublishValidator;
import net.fortuna.ical4j.validate.component.VFreeBusyReplyValidator;
import net.fortuna.ical4j.validate.component.VFreeBusyRequestValidator;
+import org.apache.commons.collections4.Closure;
+import org.apache.commons.collections4.CollectionUtils;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -497,20 +500,17 @@ public final void validate(final boolean recurse) throws ValidationException {
getProperties());
}
- final PropertyValidator validator = PropertyValidator.getInstance();
-
/*
* ; the following are optional, ; but MUST NOT occur more than once contact / dtstart / dtend / duration /
* dtstamp / organizer / uid / url /
*/
- validator.assertOneOrLess(Property.CONTACT, getProperties());
- validator.assertOneOrLess(Property.DTSTART, getProperties());
- validator.assertOneOrLess(Property.DTEND, getProperties());
- validator.assertOneOrLess(Property.DURATION, getProperties());
- validator.assertOneOrLess(Property.DTSTAMP, getProperties());
- validator.assertOneOrLess(Property.ORGANIZER, getProperties());
- validator.assertOneOrLess(Property.UID, getProperties());
- validator.assertOneOrLess(Property.URL, getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.CONTACT, Property.DTSTART, Property.DTEND, Property.DURATION,
+ Property.DTSTAMP, Property.ORGANIZER, Property.UID, Property.URL), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, getProperties());
+ }
+ });
/*
* ; the following are optional, ; and MAY occur more than once attendee / comment / freebusy / rstatus / x-prop
@@ -521,10 +521,12 @@ public final void validate(final boolean recurse) throws ValidationException {
* calendar component. Any recurring events are resolved into their individual busy time periods using the
* "FREEBUSY" property.
*/
- validator.assertNone(Property.RRULE, getProperties());
- validator.assertNone(Property.EXRULE, getProperties());
- validator.assertNone(Property.RDATE, getProperties());
- validator.assertNone(Property.EXDATE, getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.RRULE, Property.EXRULE, Property.RDATE, Property.EXDATE), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertNone(input, getProperties());
+ }
+ });
// DtEnd value must be later in time that DtStart..
final DtStart dtStart = (DtStart) getProperty(Property.DTSTART);
@@ -40,7 +40,10 @@
import net.fortuna.ical4j.validate.component.VJournalAddValidator;
import net.fortuna.ical4j.validate.component.VJournalCancelValidator;
import net.fortuna.ical4j.validate.component.VJournalPublishValidator;
+import org.apache.commons.collections4.Closure;
+import org.apache.commons.collections4.CollectionUtils;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -173,32 +176,14 @@ public final void validate(final boolean recurse)
* ; the following are optional, ; but MUST NOT occur more than once class / created / description / dtstart /
* dtstamp / last-mod / organizer / recurid / seq / status / summary / uid / url /
*/
- PropertyValidator.getInstance().assertOneOrLess(Property.CLASS,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CREATED,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DESCRIPTION,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DTSTART,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DTSTAMP,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LAST_MODIFIED,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.ORGANIZER,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RECURRENCE_ID,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SEQUENCE,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.STATUS,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SUMMARY,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.UID,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.URL,
- getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.CLASS, Property.CREATED, Property.DESCRIPTION, Property.DTSTART,
+ Property.DTSTAMP, Property.LAST_MODIFIED, Property.ORGANIZER, Property.RECURRENCE_ID, Property.SEQUENCE,
+ Property.STATUS, Property.SUMMARY, Property.UID, Property.URL), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, getProperties());
+ }
+ });
final Status status = (Status) getProperty(Property.STATUS);
if (status != null && !Status.VJOURNAL_DRAFT.getValue().equals(status.getValue())
@@ -39,12 +39,15 @@
import net.fortuna.ical4j.validate.ValidationException;
import net.fortuna.ical4j.validate.Validator;
import net.fortuna.ical4j.validate.component.*;
+import org.apache.commons.collections4.Closure;
+import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -251,42 +254,15 @@ public final void validate(final boolean recurse)
* dtstamp / dtstart / geo / last-mod / location / organizer / percent / priority / recurid / seq / status /
* summary / uid / url /
*/
- PropertyValidator.getInstance().assertOneOrLess(Property.CLASS,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.COMPLETED,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CREATED,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DESCRIPTION,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DTSTAMP,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DTSTART,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.GEO,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LAST_MODIFIED,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LOCATION,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.ORGANIZER,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(
- Property.PERCENT_COMPLETE, getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.PRIORITY,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RECURRENCE_ID,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SEQUENCE,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.STATUS,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SUMMARY,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.UID,
- getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.URL,
- getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.CLASS, Property.COMPLETED, Property.CREATED, Property.DESCRIPTION,
+ Property.DTSTAMP, Property.DTSTART, Property.GEO, Property.LAST_MODIFIED, Property.LOCATION, Property.ORGANIZER,
+ Property.PERCENT_COMPLETE, Property.PRIORITY, Property.RECURRENCE_ID, Property.SEQUENCE, Property.STATUS,
+ Property.SUMMARY, Property.UID, Property.URL), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, getProperties());
+ }
+ });
final Status status = (Status) getProperty(Property.STATUS);
if (status != null && !Status.VTODO_NEEDS_ACTION.getValue().equals(status.getValue())
Oops, something went wrong.

0 comments on commit 4860a73

Please sign in to comment.