Permalink
Browse files

Use closures to reduce duplication of code

  • Loading branch information...
benfortuna committed Sep 12, 2015
1 parent 325ff2d commit a04ef6fee7751a2209e8a4383ad4c844b2499fa3
@@ -8,6 +8,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:COUNTER Validator.
@@ -74,33 +78,33 @@
private static final long serialVersionUID = 1L;
- public void validate(VEvent target) throws ValidationException {
- PropertyValidator.getInstance().assertOne(Property.DTSTAMP, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.DTSTART, target.getProperties());
+ public void validate(final VEvent target) throws ValidationException {
+ CollectionUtils.forAllDo(Arrays.asList(Property.DTSTAMP, Property.DTSTART), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOne(input, target.getProperties());
+ }
+ });
if (!CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)) {
PropertyValidator.getInstance().assertOne(Property.ORGANIZER, target.getProperties());
}
- PropertyValidator.getInstance().assertOne(Property.SEQUENCE, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.SUMMARY, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.UID, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.SEQUENCE, Property.SUMMARY, 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.DESCRIPTION, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.DTEND, 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.PRIORITY, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RECURRENCE_ID, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RESOURCES, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.STATUS, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.TRANSP, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.URL, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.CATEGORIES, Property.CLASS, Property.CREATED, Property.DESCRIPTION,
+ Property.DTEND, Property.DURATION, Property.GEO, Property.LAST_MODIFIED, Property.LOCATION, Property.PRIORITY,
+ Property.RECURRENCE_ID, Property.RESOURCES, Property.STATUS, Property.TRANSP, Property.URL), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, target.getProperties());
+ }
+ });
for (final VAlarm alarm : target.getAlarms()) {
alarm.validate(Method.COUNTER);
@@ -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;
/**
* METHOD:DECLINECOUNTER Validator.
@@ -67,38 +71,31 @@
private static final long serialVersionUID = 1L;
- public void validate(VEvent target) throws ValidationException {
- PropertyValidator.getInstance().assertOne(Property.DTSTAMP, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.ORGANIZER, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.UID, target.getProperties());
+ public void validate(final VEvent target) throws ValidationException {
+ CollectionUtils.forAllDo(Arrays.asList(Property.DTSTAMP, Property.ORGANIZER, Property.UID), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOne(input, target.getProperties());
+ }
+ });
- PropertyValidator.getInstance().assertOneOrLess(Property.RECURRENCE_ID, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.SEQUENCE, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.RECURRENCE_ID, Property.SEQUENCE), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, target.getProperties());
+ }
+ });
- PropertyValidator.getInstance().assertNone(Property.ATTACH, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.ATTENDEE, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.CATEGORIES, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.CLASS, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.CONTACT, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.CREATED, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.DESCRIPTION, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.DTEND, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.DTSTART, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.DURATION, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.EXDATE, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.EXRULE, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.GEO, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.LAST_MODIFIED, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.LOCATION, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.PRIORITY, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.RDATE, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.RELATED_TO, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.RESOURCES, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.RRULE, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.STATUS, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.SUMMARY, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.TRANSP, target.getProperties());
- PropertyValidator.getInstance().assertNone(Property.URL, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.ATTACH, Property.ATTENDEE, Property.CATEGORIES, Property.CLASS,
+ Property.CONTACT, Property.CREATED, Property.DESCRIPTION, Property.DTEND, Property.DTSTART, Property.DURATION,
+ Property.EXDATE, Property.EXRULE, Property.GEO, Property.LAST_MODIFIED, Property.LOCATION, Property.PRIORITY,
+ Property.RDATE, Property.RELATED_TO, Property.RESOURCES, Property.RRULE, Property.STATUS, Property.SUMMARY,
+ Property.TRANSP, Property.URL), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertNone(input, target.getProperties());
+ }
+ });
ComponentValidator.assertNone(Component.VALARM, target.getAlarms());
}
@@ -8,6 +8,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:PUBLISH Validator.
@@ -69,33 +73,34 @@
private static final long serialVersionUID = 1L;
- public void validate(VEvent target) throws ValidationException {
- PropertyValidator.getInstance().assertOne(Property.DTSTAMP, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.DTSTART, target.getProperties());
+ public void validate(final VEvent target) throws ValidationException {
+ CollectionUtils.forAllDo(Arrays.asList(Property.DTSTAMP, Property.DTSTART), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOne(input, target.getProperties());
+ }
+ });
if (!CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)) {
- PropertyValidator.getInstance().assertOne(Property.ORGANIZER, target.getProperties());
- PropertyValidator.getInstance().assertOne(Property.SUMMARY, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.ORGANIZER, Property.SUMMARY), new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOne(input, target.getProperties());
+ }
+ });
}
PropertyValidator.getInstance().assertOne(Property.UID, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RECURRENCE_ID, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(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.DTEND, 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.PRIORITY, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.RESOURCES, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.STATUS, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.TRANSP, target.getProperties());
- PropertyValidator.getInstance().assertOneOrLess(Property.URL, target.getProperties());
+ CollectionUtils.forAllDo(Arrays.asList(Property.RECURRENCE_ID, Property.SEQUENCE, Property.CATEGORIES, Property.CLASS,
+ Property.CREATED, Property.DESCRIPTION, Property.DTEND, Property.DURATION, Property.GEO, Property.LAST_MODIFIED,
+ Property.LOCATION, Property.PRIORITY, Property.RESOURCES, Property.STATUS, Property.TRANSP, Property.URL),
+ new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ PropertyValidator.getInstance().assertOneOrLess(input, target.getProperties());
+ }
+ });
if (!CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)) {
PropertyValidator.getInstance().assertNone(Property.ATTENDEE, target.getProperties());

0 comments on commit a04ef6f

Please sign in to comment.