Replace utils’ Validator with Guava’s Predicate.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 17 Jan 2013 14:17:48 +0000 (15:17 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 17 Jan 2013 14:17:48 +0000 (15:17 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/Options.java
src/main/java/net/pterodactylus/sone/utils/IntegerRangePredicate.java [new file with mode: 0644]

index 581e2d5..9713ddc 100644 (file)
@@ -75,6 +75,7 @@ import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
 import net.pterodactylus.sone.main.SonePlugin;
+import net.pterodactylus.sone.utils.IntegerRangePredicate;
 import net.pterodactylus.util.config.Configuration;
 import net.pterodactylus.util.config.ConfigurationException;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.config.Configuration;
 import net.pterodactylus.util.config.ConfigurationException;
 import net.pterodactylus.util.logging.Logging;
@@ -82,11 +83,9 @@ import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.thread.NamedThreadFactory;
 import net.pterodactylus.util.thread.Ticker;
 import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.thread.NamedThreadFactory;
 import net.pterodactylus.util.thread.Ticker;
-import net.pterodactylus.util.validation.EqualityValidator;
-import net.pterodactylus.util.validation.IntegerRangeValidator;
-import net.pterodactylus.util.validation.OrValidator;
 
 import com.google.common.base.Predicate;
 
 import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
 import com.google.common.collect.Collections2;
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
 import com.google.common.collect.Collections2;
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
@@ -2188,7 +2187,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider
        @SuppressWarnings("unchecked")
        private void loadConfiguration() {
                /* create options. */
        @SuppressWarnings("unchecked")
        private void loadConfiguration() {
                /* create options. */
-               options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new IntegerRangeValidator(0, Integer.MAX_VALUE), new OptionWatcher<Integer>() {
+               options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new OptionWatcher<Integer>() {
 
                        @Override
                        public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
 
                        @Override
                        public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
@@ -2196,13 +2195,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider
                        }
 
                }));
                        }
 
                }));
-               options.addIntegerOption("PostsPerPage", new DefaultOption<Integer>(10, new IntegerRangeValidator(1, Integer.MAX_VALUE)));
-               options.addIntegerOption("ImagesPerPage", new DefaultOption<Integer>(9, new IntegerRangeValidator(1, Integer.MAX_VALUE)));
-               options.addIntegerOption("CharactersPerPost", new DefaultOption<Integer>(400, new OrValidator<Integer>(new IntegerRangeValidator(50, Integer.MAX_VALUE), new EqualityValidator<Integer>(-1))));
-               options.addIntegerOption("PostCutOffLength", new DefaultOption<Integer>(200, new OrValidator<Integer>(new IntegerRangeValidator(50, Integer.MAX_VALUE), new EqualityValidator<Integer>(-1))));
+               options.addIntegerOption("PostsPerPage", new DefaultOption<Integer>(10, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
+               options.addIntegerOption("ImagesPerPage", new DefaultOption<Integer>(9, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
+               options.addIntegerOption("CharactersPerPost", new DefaultOption<Integer>(400, Predicates.or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
+               options.addIntegerOption("PostCutOffLength", new DefaultOption<Integer>(200, Predicates.or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
                options.addBooleanOption("RequireFullAccess", new DefaultOption<Boolean>(false));
                options.addBooleanOption("RequireFullAccess", new DefaultOption<Boolean>(false));
-               options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75, new IntegerRangeValidator(0, 100)));
-               options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25, new IntegerRangeValidator(-100, 100)));
+               options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75, new IntegerRangePredicate(0, 100)));
+               options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25, new IntegerRangePredicate(-100, 100)));
                options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
                options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false, new OptionWatcher<Boolean>() {
 
                options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
                options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false, new OptionWatcher<Boolean>() {
 
index d226f5c..c8e3589 100644 (file)
@@ -21,7 +21,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
 import java.util.HashMap;
 import java.util.Map;
 
-import net.pterodactylus.util.validation.Validator;
+import com.google.common.base.Predicate;
 
 /**
  * Stores various options that influence Sone’s behaviour.
 
 /**
  * Stores various options that influence Sone’s behaviour.
@@ -68,9 +68,8 @@ public class Options {
                 *
                 * @param value
                 *            The value to validate
                 *
                 * @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
+                * @return {@code true} if this option does not have a validator, or the
+                *         validator validates this object, {@code false} otherwise
                 */
                public boolean validate(T value);
 
                 */
                public boolean validate(T value);
 
@@ -127,7 +126,7 @@ public class Options {
                private volatile T value;
 
                /** The validator. */
                private volatile T value;
 
                /** The validator. */
-               private Validator<T> validator;
+               private Predicate<T> validator;
 
                /** The option watcher. */
                private final OptionWatcher<T> optionWatcher;
 
                /** The option watcher. */
                private final OptionWatcher<T> optionWatcher;
@@ -150,7 +149,7 @@ public class Options {
                 * @param validator
                 *            The validator for value validation (may be {@code null})
                 */
                 * @param validator
                 *            The validator for value validation (may be {@code null})
                 */
-               public DefaultOption(T defaultValue, Validator<T> validator) {
+               public DefaultOption(T defaultValue, Predicate<T> validator) {
                        this(defaultValue, validator, null);
                }
 
                        this(defaultValue, validator, null);
                }
 
@@ -176,7 +175,7 @@ public class Options {
                 * @param optionWatcher
                 *            The option watcher (may be {@code null})
                 */
                 * @param optionWatcher
                 *            The option watcher (may be {@code null})
                 */
-               public DefaultOption(T defaultValue, Validator<T> validator, OptionWatcher<T> optionWatcher) {
+               public DefaultOption(T defaultValue, Predicate<T> validator, OptionWatcher<T> optionWatcher) {
                        this.defaultValue = defaultValue;
                        this.validator = validator;
                        this.optionWatcher = optionWatcher;
                        this.defaultValue = defaultValue;
                        this.validator = validator;
                        this.optionWatcher = optionWatcher;
@@ -214,7 +213,7 @@ public class Options {
                 */
                @Override
                public boolean validate(T value) {
                 */
                @Override
                public boolean validate(T value) {
-                       return (validator == null) || (value == null) || validator.validate(value);
+                       return (validator == null) || (value == null) || validator.apply(value);
                }
 
                /**
                }
 
                /**
@@ -222,7 +221,7 @@ public class Options {
                 */
                @Override
                public void set(T value) {
                 */
                @Override
                public void set(T value) {
-                       if ((value != null) && (validator != null) && (!validator.validate(value))) {
+                       if ((value != null) && (validator != null) && (!validator.apply(value))) {
                                throw new IllegalArgumentException("New Value (" + value + ") could not be validated.");
                        }
                        T oldValue = this.value;
                                throw new IllegalArgumentException("New Value (" + value + ") could not be validated.");
                        }
                        T oldValue = this.value;
diff --git a/src/main/java/net/pterodactylus/sone/utils/IntegerRangePredicate.java b/src/main/java/net/pterodactylus/sone/utils/IntegerRangePredicate.java
new file mode 100644 (file)
index 0000000..db6a841
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Sone - IntegerRangePredicate.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.utils;
+
+import com.google.common.base.Predicate;
+
+/**
+ * {@link Predicate} that verifies that an {@link Integer} value is not
+ * {@code null} and is between a lower and an upper bound. Both bounds are
+ * inclusive.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class IntegerRangePredicate implements Predicate<Integer> {
+
+       /** The lower bound. */
+       private final int lowerBound;
+
+       /** The upper bound. */
+       private final int upperBound;
+
+       /**
+        * Creates a new integer range predicate.
+        *
+        * @param lowerBound
+        *            The lower bound
+        * @param upperBound
+        *            The upper bound
+        */
+       public IntegerRangePredicate(int lowerBound, int upperBound) {
+               this.lowerBound = lowerBound;
+               this.upperBound = upperBound;
+       }
+
+       //
+       // PREDICATE METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean apply(Integer value) {
+               return (value != null) && (value >= lowerBound) && (value <= upperBound);
+       }
+
+}