Permalink
Browse files

Created validator implementation for common use case

  • Loading branch information...
benfortuna committed Sep 13, 2015
1 parent 9754c11 commit 923292b1b7d049161a04182dcde59db20088c609
@@ -32,8 +32,7 @@
package net.fortuna.ical4j.model.property;
import net.fortuna.ical4j.model.*;
-import net.fortuna.ical4j.validate.ParameterValidator;
-import net.fortuna.ical4j.validate.ValidationException;
+import net.fortuna.ical4j.validate.property.OneOrLessParameterValidator;
import java.io.IOException;
import java.net.URISyntaxException;
@@ -59,42 +58,25 @@
* Default constructor.
*/
public Region() {
- super(REGION, PropertyFactoryImpl.getInstance());
+ this(null);
}
/**
* @param aValue a value string for this component
*/
public Region(final String aValue) {
- super(REGION, PropertyFactoryImpl.getInstance());
- setValue(aValue);
+ this(new ParameterList(), aValue);
}
/**
* @param aList a list of parameters for this component
* @param aValue a value string for this component
*/
public Region(final ParameterList aList, final String aValue) {
- super(REGION, aList, PropertyFactoryImpl.getInstance());
+ super(REGION, aList, new OneOrLessParameterValidator(Parameter.ABBREV), PropertyFactoryImpl.getInstance());
setValue(aValue);
}
- /**
- * {@inheritDoc}
- */
- public final void validate() throws ValidationException {
-
- /*
- * ; the following are optional, ; but MUST NOT occur more than once (";" abbrev
- */
- ParameterValidator.getInstance().assertOneOrLess(Parameter.ABBREV,
- getParameters());
-
- /*
- * ; the following is optional, ; and MAY occur more than once (";" xparam)
- */
- }
-
/**
* {@inheritDoc}
*/
@@ -0,0 +1,33 @@
+package net.fortuna.ical4j.validate.property;
+
+import net.fortuna.ical4j.model.Property;
+import net.fortuna.ical4j.validate.ParameterValidator;
+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;
+import java.util.List;
+
+/**
+ * A validator implementation that ensures each parameter is specified not more than once on a property instance.
+ */
+public class OneOrLessParameterValidator implements Validator<Property> {
+
+ private final List<String> parameters;
+
+ public OneOrLessParameterValidator(String...parameters) {
+ this.parameters = Arrays.asList(parameters);
+ }
+
+ @Override
+ public void validate(final Property target) throws ValidationException {
+ CollectionUtils.forAllDo(parameters, new Closure<String>() {
+ @Override
+ public void execute(String input) {
+ ParameterValidator.getInstance().assertOneOrLess(input, target.getParameters());
+ }
+ });
+ }
+}

0 comments on commit 923292b

Please sign in to comment.