Add option validation.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 10 May 2011 04:57:23 +0000 (06:57 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 10 May 2011 04:57:23 +0000 (06:57 +0200)
src/main/java/net/pterodactylus/sone/core/Options.java

index 94fbec1..7392da2 100644 (file)
@@ -24,6 +24,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import net.pterodactylus.util.validation.Validator;
+
 /**
  * Stores various options that influence Sone’s behaviour.
  *
@@ -64,12 +66,26 @@ public class Options {
                public T getReal();
 
                /**
+                * Validates the given value. Note that {@code null} is always a valid
+                * value!
+                *
+                * @param value
+                *            The value to validate
+                * @return {@code true} if this option does not have a {@link Validator}
+                *         , or the {@link Validator} validates this object, {@code
+                *         false} otherwise
+                */
+               public boolean validate(T value);
+
+               /**
                 * Sets the current value of the option.
                 *
                 * @param value
                 *            The new value of the option
+                * @throws IllegalArgumentException
+                *             if the value is not valid for this option
                 */
-               public void set(T value);
+               public void set(T value) throws IllegalArgumentException;
 
        }
 
@@ -113,6 +129,9 @@ public class Options {
                /** The current value. */
                private volatile T value;
 
+               /** The validator. */
+               private Validator<T> validator;
+
                /** The option watcher. */
                private final List<OptionWatcher<T>> optionWatchers = new ArrayList<OptionWatcher<T>>();
 
@@ -125,7 +144,22 @@ public class Options {
                 *            The option watchers
                 */
                public DefaultOption(T defaultValue, OptionWatcher<T>... optionWatchers) {
+                       this(defaultValue, null, optionWatchers);
+               }
+
+               /**
+                * Creates a new default option.
+                *
+                * @param defaultValue
+                *            The default value of the option
+                * @param validator
+                *            The validator for value validation
+                * @param optionWatchers
+                *            The option watchers
+                */
+               public DefaultOption(T defaultValue, Validator<T> validator, OptionWatcher<T>... optionWatchers) {
                        this.defaultValue = defaultValue;
+                       this.validator = validator;
                        this.optionWatchers.addAll(Arrays.asList(optionWatchers));
                }
 
@@ -159,8 +193,18 @@ public class Options {
                /**
                 * {@inheritDoc}
                 */
+               public boolean validate(T value) {
+                       return (validator == null) || (value == null) || validator.validate(value);
+               }
+
+               /**
+                * {@inheritDoc}
+                */
                @Override
                public void set(T value) {
+                       if ((value != null) && (validator != null) && (!validator.validate(value))) {
+                               throw new IllegalArgumentException("New Value (" + value + ") could not be validated.");
+                       }
                        T oldValue = this.value;
                        this.value = value;
                        if (!get().equals(oldValue)) {