From 88731847148b7fbea5d578abe968d42daf3d1b58 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 29 Jul 2014 19:47:03 +0200 Subject: [PATCH] Add class for setting insertion delay from an option. --- .../java/net/pterodactylus/sone/core/Core.java | 10 ++------- .../net/pterodactylus/sone/core/SoneInserter.java | 17 +++++++++++++++ .../pterodactylus/sone/core/SoneInserterTest.java | 24 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 643c835..860effb 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -41,6 +41,7 @@ import java.util.logging.Logger; import net.pterodactylus.sone.core.Options.DefaultOption; import net.pterodactylus.sone.core.Options.Option; import net.pterodactylus.sone.core.Options.OptionWatcher; +import net.pterodactylus.sone.core.SoneInserter.SetInsertionDelay; import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent; import net.pterodactylus.sone.core.event.MarkPostKnownEvent; import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent; @@ -1988,14 +1989,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, */ private void loadConfiguration() { /* create options. */ - options.addIntegerOption("InsertionDelay", new DefaultOption(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new OptionWatcher() { - - @Override - public void optionChanged(Option option, Integer oldValue, Integer newValue) { - SoneInserter.setInsertionDelay(newValue); - } - - })); + options.addIntegerOption("InsertionDelay", new DefaultOption(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new SetInsertionDelay())); 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)))); diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 60eebec..46a3fe7 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -32,6 +32,8 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; +import net.pterodactylus.sone.core.Options.Option; +import net.pterodactylus.sone.core.Options.OptionWatcher; import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent; import net.pterodactylus.sone.core.event.SoneInsertedEvent; import net.pterodactylus.sone.core.event.SoneInsertingEvent; @@ -54,6 +56,7 @@ import net.pterodactylus.util.template.TemplateException; import net.pterodactylus.util.template.TemplateParser; import net.pterodactylus.util.template.XmlFilter; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.FluentIterable; import com.google.common.collect.Ordering; import com.google.common.eventbus.EventBus; @@ -138,6 +141,11 @@ public class SoneInserter extends AbstractService { return this; } + @VisibleForTesting + static AtomicInteger getInsertionDelay() { + return insertionDelay; + } + /** * Changes the insertion delay, i.e. the time the Sone inserter waits after it * has noticed a Sone modification before it starts the insert. @@ -241,6 +249,15 @@ public class SoneInserter extends AbstractService { } } + static class SetInsertionDelay implements OptionWatcher { + + @Override + public void optionChanged(Option option, Integer oldValue, Integer newValue) { + setInsertionDelay(newValue); + } + + } + /** * Container for information that are required to insert a Sone. This * container merely exists to copy all relevant data without holding a lock diff --git a/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java new file mode 100644 index 0000000..ed436fe --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java @@ -0,0 +1,24 @@ +package net.pterodactylus.sone.core; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import net.pterodactylus.sone.core.SoneInserter.SetInsertionDelay; + +import org.junit.Test; + +/** + * Unit test for {@link SoneInserter} and its subclasses. + * + * @author David ‘Bombe’ Roden + */ +public class SoneInserterTest { + + @Test + public void insertionDelayIsForwardedToSoneInserter() { + SetInsertionDelay setInsertionDelay = new SetInsertionDelay(); + setInsertionDelay.optionChanged(null, null, 15); + assertThat(SoneInserter.getInsertionDelay().get(), is(15)); + } + +} -- 2.7.4