Permalink
Browse files

Use closures to reduce duplication of code

  • Loading branch information...
benfortuna committed Sep 13, 2015
1 parent 43f9e50 commit e5bae46036a0d351389134474dd6cf4988804785
@@ -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:REPLY Validator.
@@ -71,30 +75,24 @@
private static final long serialVersionUID = 1L;
public void validate(VEvent target) throws ValidationException {
PropertyValidator.getInstance().assertOne(Property.ATTENDEE, target.getProperties());
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.ATTENDEE, 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());
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.DTSTART, 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.SUMMARY, 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.DTSTART, Property.DURATION, Property.GEO,
Property.LAST_MODIFIED, Property.LOCATION, Property.PRIORITY, Property.RESOURCES, Property.STATUS, Property.SUMMARY,
Property.TRANSP, Property.URL), new Closure<String>() {
@Override
public void execute(String input) {
PropertyValidator.getInstance().assertOneOrLess(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:REQUEST Validator.
@@ -68,33 +72,28 @@
private static final long serialVersionUID = 1L;
public void validate(VEvent target) throws ValidationException {
public void validate(final VEvent target) throws ValidationException {
if (!CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)) {
PropertyValidator.getInstance().assertOneOrMore(Property.ATTENDEE, 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.SUMMARY, target.getProperties());
PropertyValidator.getInstance().assertOne(Property.UID, target.getProperties());
CollectionUtils.forAllDo(Arrays.asList(Property.DTSTAMP, Property.DTSTART, Property.ORGANIZER, Property.SUMMARY,
Property.UID), new Closure<String>() {
@Override
public void execute(String input) {
PropertyValidator.getInstance().assertOne(input, 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.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.SEQUENCE, 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.REQUEST);

0 comments on commit e5bae46

Please sign in to comment.