Permalink
Browse files

Use closures to reduce duplication of code

  • Loading branch information...
benfortuna committed Oct 10, 2015
1 parent 3af3420 commit fe20ea33b6dd1810766050825e9d442be7810b59
@@ -5,6 +5,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;
/**
* <pre>
@@ -45,20 +49,25 @@
private static final long serialVersionUID = 1L;
- public void validate(VFreeBusy target) throws ValidationException {
+ public void validate(final VFreeBusy target) throws ValidationException {
// FREEBUSY is 1+ in RFC2446 but 0+ in Calsify
- PropertyValidator.getInstance().assertOne(Property.ATTENDEE, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.DTSTAMP, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.DTEND, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.DTSTART, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.ORGANIZER, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.UID, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.ATTENDEE, Property.DTSTAMP, Property.DTEND, Property.DTSTART,
+ Property.ORGANIZER, Property.UID), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOne(input, target.getProperties());
+ }
+ });
PropertyValidator.getInstance().assertOneOrLess(Property.URL, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.DURATION, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.SEQUENCE, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.DURATION, Property.SEQUENCE), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertNone(input, target.getProperties());
+ }
+ });
}
}
@@ -5,6 +5,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;
/**
* METHOD:REQUEST Validator.
@@ -43,18 +47,23 @@
private static final long serialVersionUID = 1L;
- public void validate(VFreeBusy target) throws ValidationException {
+ public void validate(final VFreeBusy target) throws ValidationException {
PropertyValidator.getInstance().assertOneOrMore(Property.ATTENDEE, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.DTEND, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.DTSTAMP, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.DTSTART, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.ORGANIZER, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.UID, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.DTEND, Property.DTSTAMP, Property.DTSTART, Property.ORGANIZER,
+ Property.UID), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOne(input, target.getProperties());
+ }
+ });
- PropertyValidator.getInstance().assertNone(Property.FREEBUSY, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.DURATION, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.REQUEST_STATUS, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.URL, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.FREEBUSY, Property.DURATION, Property.REQUEST_STATUS,
+ Property.URL), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertNone(input, target.getProperties());
+ }
+ });
}
}
@@ -5,6 +5,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;
/**
* <pre>
@@ -54,21 +58,22 @@
private static final long serialVersionUID = 1L;
- public void validate(VJournal target) throws ValidationException {
- PropertyValidator.getInstance().assertOne(Property.DESCRIPTION, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.DTSTAMP, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.DTSTART, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.ORGANIZER, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.SEQUENCE, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.UID, target.getProperties());
+ public void validate(final VJournal target) throws ValidationException {
+ CollectionUtils.forAllDo(Arrays.asList(Property.DESCRIPTION, Property.DTSTAMP, Property.DTSTART, Property.ORGANIZER,
+ Property.SEQUENCE, Property.UID), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOne(input, target.getProperties());
+ }
+ });
- PropertyValidator.getInstance().assertOneOrLess(Property.CATEGORIES, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CLASS, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CREATED, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LAST_MODIFIED, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.STATUS, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SUMMARY, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.URL, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.CATEGORIES, Property.CLASS, Property.CREATED, Property.LAST_MODIFIED,
+ Property.STATUS, Property.SUMMARY, Property.URL), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, target.getProperties());
+ }
+ });
PropertyValidator.getInstance().assertNone(Property.ATTENDEE, target.getProperties());
PropertyValidator.getInstance().assertNone(Property.RECURRENCE_ID, target.getProperties());
@@ -5,6 +5,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;
/**
* <pre>
@@ -57,22 +61,20 @@
private static final long serialVersionUID = 1L;
- public void validate(VJournal target) throws ValidationException {
+ public void validate(final VJournal target) throws ValidationException {
PropertyValidator.getInstance().assertOne(Property.DTSTAMP, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.ORGANIZER, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.SEQUENCE, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.UID, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CATEGORIES, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CLASS, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CREATED, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DESCRIPTION, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DTSTART, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LAST_MODIFIED, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RECURRENCE_ID, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.STATUS, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SUMMARY, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.URL, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.CATEGORIES, Property.CLASS, Property.CREATED, Property.DESCRIPTION,
+ Property.DTSTART, Property.LAST_MODIFIED, Property.RECURRENCE_ID, Property.STATUS, Property.SUMMARY,
+ Property.URL), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, target.getProperties());
+ }
+ });
PropertyValidator.getInstance().assertNone(Property.REQUEST_STATUS, target.getProperties());
}
@@ -5,6 +5,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;
/**
* <pre>
@@ -58,22 +62,20 @@
private static final long serialVersionUID = 1L;
- public void validate(VJournal target) throws ValidationException {
+ public void validate(final VJournal target) throws ValidationException {
PropertyValidator.getInstance().assertOne(Property.DESCRIPTION, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.DTSTAMP, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.DTSTART, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.ORGANIZER, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.UID, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CATEGORIES, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CLASS, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CREATED, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LAST_MODIFIED, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RECURRENCE_ID, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SEQUENCE, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.STATUS, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SUMMARY, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.URL, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.CATEGORIES, Property.CLASS, Property.CREATED, Property.LAST_MODIFIED,
+ Property.RECURRENCE_ID, Property.SEQUENCE, Property.STATUS, Property.SUMMARY, Property.URL), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, target.getProperties());
+ }
+ });
PropertyValidator.getInstance().assertNone(Property.ATTENDEE, target.getProperties());
}
@@ -7,6 +7,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;
/**
* <pre>
@@ -67,28 +71,22 @@
private static final long serialVersionUID = 1L;
- public void validate(VToDo target) throws ValidationException {
+ public void validate(final VToDo target) throws ValidationException {
PropertyValidator.getInstance().assertOne(Property.DTSTAMP, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.ORGANIZER, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.PRIORITY, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.SEQUENCE, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.SUMMARY, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.UID, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CATEGORIES, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CLASS, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CREATED, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DESCRIPTION, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DTSTART, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DUE, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DURATION, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.GEO, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LAST_MODIFIED, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LOCATION, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.PERCENT_COMPLETE, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RESOURCES, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.STATUS, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.URL, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.CATEGORIES, Property.CLASS, Property.CREATED, Property.DESCRIPTION,
+ Property.DTSTART, Property.DUE, Property.DURATION, Property.GEO, Property.LAST_MODIFIED, Property.LOCATION,
+ Property.PERCENT_COMPLETE, Property.RESOURCES, Property.STATUS, Property.URL), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, target.getProperties());
+ }
+ });
PropertyValidator.getInstance().assertNone(Property.RECURRENCE_ID, target.getProperties());
PropertyValidator.getInstance().assertNone(Property.REQUEST_STATUS, target.getProperties());
@@ -7,6 +7,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;
/**
* <pre>
@@ -70,28 +74,21 @@
private static final long serialVersionUID = 1L;
- public void validate(VToDo target) throws ValidationException {
+ public void validate(final VToDo target) throws ValidationException {
PropertyValidator.getInstance().assertOne(Property.UID, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.DTSTAMP, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.ORGANIZER, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.SEQUENCE, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CATEGORIES, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CLASS, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.CREATED, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DESCRIPTION, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DTSTART, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DUE, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DURATION, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.GEO, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LAST_MODIFIED, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.LOCATION, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.PERCENT_COMPLETE, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RECURRENCE_ID, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RESOURCES, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.PRIORITY, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.STATUS, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.URL, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.CATEGORIES, Property.CLASS, Property.CREATED, Property.DESCRIPTION,
+ Property.DTSTART, Property.DUE, Property.DURATION, Property.GEO, Property.LAST_MODIFIED, Property.LOCATION,
+ Property.PERCENT_COMPLETE, Property.RECURRENCE_ID, Property.RESOURCES, Property.PRIORITY, Property.STATUS,
+ Property.URL), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, target.getProperties());
+ }
+ });
PropertyValidator.getInstance().assertNone(Property.REQUEST_STATUS, target.getProperties());
Oops, something went wrong.

0 comments on commit fe20ea3

Please sign in to comment.