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.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;
@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) {
}
}));
- 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.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>() {
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.
*
* @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);
private volatile T value;
/** The validator. */
- private Validator<T> validator;
+ private Predicate<T> validator;
/** The option watcher. */
private final OptionWatcher<T> optionWatcher;
* @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);
}
* @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;
*/
@Override
public boolean validate(T value) {
- return (validator == null) || (value == null) || validator.validate(value);
+ return (validator == null) || (value == null) || validator.apply(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;
--- /dev/null
+/*
+ * 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);
+ }
+
+}