From 03946e6554d5a59f54058fbf89b407ef327344ca Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 17 Jan 2013 15:17:48 +0100 Subject: [PATCH] =?utf8?q?Replace=20utils=E2=80=99=20Validator=20with=20Gu?= =?utf8?q?ava=E2=80=99s=20Predicate.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/core/Core.java | 19 ++++--- .../java/net/pterodactylus/sone/core/Options.java | 17 +++--- .../sone/utils/IntegerRangePredicate.java | 62 ++++++++++++++++++++++ 3 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/utils/IntegerRangePredicate.java diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 581e2d5..9713ddc 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -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.utils.IntegerRangePredicate; 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.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.Predicates; 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. */ - options.addIntegerOption("InsertionDelay", new DefaultOption(60, new IntegerRangeValidator(0, Integer.MAX_VALUE), new OptionWatcher() { + options.addIntegerOption("InsertionDelay", new DefaultOption(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new OptionWatcher() { @Override public void optionChanged(Option option, Integer oldValue, Integer newValue) { @@ -2196,13 +2195,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider } })); - options.addIntegerOption("PostsPerPage", new DefaultOption(10, new IntegerRangeValidator(1, Integer.MAX_VALUE))); - options.addIntegerOption("ImagesPerPage", new DefaultOption(9, new IntegerRangeValidator(1, Integer.MAX_VALUE))); - options.addIntegerOption("CharactersPerPost", new DefaultOption(400, new OrValidator(new IntegerRangeValidator(50, Integer.MAX_VALUE), new EqualityValidator(-1)))); - options.addIntegerOption("PostCutOffLength", new DefaultOption(200, new OrValidator(new IntegerRangeValidator(50, Integer.MAX_VALUE), new EqualityValidator(-1)))); + options.addIntegerOption("PostsPerPage", new DefaultOption(10, new IntegerRangePredicate(1, Integer.MAX_VALUE))); + options.addIntegerOption("ImagesPerPage", new DefaultOption(9, new IntegerRangePredicate(1, Integer.MAX_VALUE))); + options.addIntegerOption("CharactersPerPost", new DefaultOption(400, Predicates.or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1)))); + options.addIntegerOption("PostCutOffLength", new DefaultOption(200, Predicates.or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1)))); options.addBooleanOption("RequireFullAccess", new DefaultOption(false)); - options.addIntegerOption("PositiveTrust", new DefaultOption(75, new IntegerRangeValidator(0, 100))); - options.addIntegerOption("NegativeTrust", new DefaultOption(-25, new IntegerRangeValidator(-100, 100))); + options.addIntegerOption("PositiveTrust", new DefaultOption(75, new IntegerRangePredicate(0, 100))); + options.addIntegerOption("NegativeTrust", new DefaultOption(-25, new IntegerRangePredicate(-100, 100))); options.addStringOption("TrustComment", new DefaultOption("Set from Sone Web Interface")); options.addBooleanOption("ActivateFcpInterface", new DefaultOption(false, new OptionWatcher() { diff --git a/src/main/java/net/pterodactylus/sone/core/Options.java b/src/main/java/net/pterodactylus/sone/core/Options.java index d226f5c..c8e3589 100644 --- a/src/main/java/net/pterodactylus/sone/core/Options.java +++ b/src/main/java/net/pterodactylus/sone/core/Options.java @@ -21,7 +21,7 @@ import java.util.Collections; 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. @@ -68,9 +68,8 @@ public class Options { * * @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); @@ -127,7 +126,7 @@ public class Options { private volatile T value; /** The validator. */ - private Validator validator; + private Predicate validator; /** The option watcher. */ private final OptionWatcher optionWatcher; @@ -150,7 +149,7 @@ public class Options { * @param validator * The validator for value validation (may be {@code null}) */ - public DefaultOption(T defaultValue, Validator validator) { + public DefaultOption(T defaultValue, Predicate validator) { this(defaultValue, validator, null); } @@ -176,7 +175,7 @@ public class Options { * @param optionWatcher * The option watcher (may be {@code null}) */ - public DefaultOption(T defaultValue, Validator validator, OptionWatcher optionWatcher) { + public DefaultOption(T defaultValue, Predicate validator, OptionWatcher optionWatcher) { this.defaultValue = defaultValue; this.validator = validator; this.optionWatcher = optionWatcher; @@ -214,7 +213,7 @@ public class Options { */ @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) { - 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; 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 index 0000000..db6a841 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/utils/IntegerRangePredicate.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +public class IntegerRangePredicate implements Predicate { + + /** 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); + } + +} -- 2.7.4