From: David ‘Bombe’ Roden Date: Sun, 28 Sep 2014 14:00:59 +0000 (+0200) Subject: Use events to communicate changes to insertion delay configuration. X-Git-Tag: 0.9-rc1^2~3^2~94 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=61e6c72dbbc5b698ce3a5e6d6c474356a5c0c9f6;p=Sone.git Use events to communicate changes to insertion delay configuration. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index c9c6da3..f7b35e9 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -46,7 +46,6 @@ import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound import net.pterodactylus.sone.core.Options.DefaultOption; import net.pterodactylus.sone.core.SoneChangeDetector.PostProcessor; import net.pterodactylus.sone.core.SoneChangeDetector.PostReplyProcessor; -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; @@ -129,7 +128,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, private final Options options = new Options(); /** The preferences. */ - private final Preferences preferences = new Preferences(options); + private final Preferences preferences; /** The event bus. */ private final EventBus eventBus; @@ -229,6 +228,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, this.webOfTrustUpdater = webOfTrustUpdater; this.eventBus = eventBus; this.database = database; + preferences = new Preferences(this.eventBus, options); } @VisibleForTesting @@ -243,6 +243,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, this.webOfTrustUpdater = webOfTrustUpdater; this.eventBus = eventBus; this.database = database; + preferences = new Preferences(this.eventBus, options); } // @@ -686,6 +687,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, sone.setKnown(true); /* TODO - load posts ’n stuff */ SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId()); + eventBus.register(soneInserter); synchronized (soneInserters) { soneInserters.put(sone, soneInserter); } @@ -1718,7 +1720,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 SetInsertionDelay())); + options.addIntegerOption("InsertionDelay", new DefaultOption(60, new IntegerRangePredicate(0, Integer.MAX_VALUE))); 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/Preferences.java b/src/main/java/net/pterodactylus/sone/core/Preferences.java index 16d9453..c413f89 100644 --- a/src/main/java/net/pterodactylus/sone/core/Preferences.java +++ b/src/main/java/net/pterodactylus/sone/core/Preferences.java @@ -17,9 +17,12 @@ package net.pterodactylus.sone.core; +import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent; import net.pterodactylus.sone.fcp.FcpInterface; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; +import com.google.common.eventbus.EventBus; + /** * Convenience interface for external classes that want to access the core’s * configuration. @@ -28,16 +31,11 @@ import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; */ public class Preferences { - /** The wrapped options. */ + private final EventBus eventBus; private final Options options; - /** - * Creates a new preferences object wrapped around the given options. - * - * @param options - * The options to wrap - */ - public Preferences(Options options) { + public Preferences(EventBus eventBus, Options options) { + this.eventBus = eventBus; this.options = options; } @@ -72,6 +70,7 @@ public class Preferences { */ public Preferences setInsertionDelay(Integer insertionDelay) { options.getIntegerOption("InsertionDelay").set(insertionDelay); + eventBus.post(new InsertionDelayChangedEvent(getInsertionDelay())); return this; } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 9f1f164..c40fc86 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -35,6 +35,7 @@ import java.util.logging.Logger; import net.pterodactylus.sone.core.Options.Option; import net.pterodactylus.sone.core.Options.OptionWatcher; import net.pterodactylus.sone.core.SoneModificationDetector.LockableFingerprintProvider; +import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent; import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent; import net.pterodactylus.sone.core.event.SoneInsertedEvent; import net.pterodactylus.sone.core.event.SoneInsertingEvent; @@ -62,6 +63,7 @@ import com.google.common.base.Optional; import com.google.common.collect.FluentIterable; import com.google.common.collect.Ordering; import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.Subscribe; import freenet.client.async.ManifestElement; import freenet.keys.FreenetURI; @@ -267,13 +269,9 @@ public class SoneInserter extends AbstractService { } } - static class SetInsertionDelay implements OptionWatcher { - - @Override - public void optionChanged(Option option, Integer oldValue, Integer newValue) { - setInsertionDelay(newValue); - } - + @Subscribe + public void insertionDelayChanged(InsertionDelayChangedEvent insertionDelayChangedEvent) { + setInsertionDelay(insertionDelayChangedEvent.getInsertionDelay()); } /** diff --git a/src/main/java/net/pterodactylus/sone/core/event/InsertionDelayChangedEvent.java b/src/main/java/net/pterodactylus/sone/core/event/InsertionDelayChangedEvent.java new file mode 100644 index 0000000..a3dc2ce --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/core/event/InsertionDelayChangedEvent.java @@ -0,0 +1,23 @@ +package net.pterodactylus.sone.core.event; + +import com.google.common.eventbus.EventBus; + +/** + * Notifies interested {@link EventBus} clients that the Sone insertion delay + * has changed. + * + * @author David ‘Bombe’ Roden + */ +public class InsertionDelayChangedEvent { + + private final int insertionDelay; + + public InsertionDelayChangedEvent(int insertionDelay) { + this.insertionDelay = insertionDelay; + } + + public int getInsertionDelay() { + return insertionDelay; + } + +} diff --git a/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java b/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java index cd9130d..e2caaae 100644 --- a/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java +++ b/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java @@ -4,13 +4,16 @@ import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import net.pterodactylus.sone.core.Options.Option; +import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; +import com.google.common.eventbus.EventBus; import org.junit.Before; import org.junit.Test; @@ -24,7 +27,8 @@ public class PreferencesTest { private static final int INTEGER_VALUE = 1; private static final String STRING_VALUE = "string-value"; private final Options options = mock(Options.class); - private final Preferences preferences = new Preferences(options); + private final EventBus eventBus = mock(EventBus.class); + private final Preferences preferences = new Preferences(eventBus, options); private final Option integerOption = when(mock(Option.class).get()).thenReturn(INTEGER_VALUE).getMock(); private final Option booleanOption = when(mock(Option.class).get()).thenReturn(true).getMock(); private final Option stringOption = when(mock(Option.class).get()).thenReturn(STRING_VALUE).getMock(); @@ -64,6 +68,12 @@ public class PreferencesTest { } @Test + public void settingInsertionDelayIsForwardedToEventBus() { + assertThat(preferences.setInsertionDelay(INTEGER_VALUE), instanceOf(Preferences.class)); + verify(eventBus).post(any(InsertionDelayChangedEvent.class)); + } + + @Test public void testGettingPostsPerPage() { assertThat(preferences.getPostsPerPage(), is(INTEGER_VALUE)); verify(integerOption).get(); diff --git a/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java index 28ead14..d43f383 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java @@ -2,6 +2,7 @@ package net.pterodactylus.sone.core; import static com.google.common.base.Optional.absent; import static com.google.common.base.Optional.of; +import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.instanceOf; @@ -20,7 +21,7 @@ import static org.mockito.Mockito.when; import java.util.HashMap; import net.pterodactylus.sone.core.SoneInserter.InsertInformation; -import net.pterodactylus.sone.core.SoneInserter.SetInsertionDelay; +import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent; import net.pterodactylus.sone.core.event.SoneEvent; import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent; import net.pterodactylus.sone.core.event.SoneInsertedEvent; @@ -31,6 +32,7 @@ import net.pterodactylus.sone.data.Sone; import freenet.keys.FreenetURI; import com.google.common.base.Optional; +import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.EventBus; import org.junit.Before; import org.junit.Test; @@ -58,8 +60,9 @@ public class SoneInserterTest { @Test public void insertionDelayIsForwardedToSoneInserter() { - SetInsertionDelay setInsertionDelay = new SetInsertionDelay(); - setInsertionDelay.optionChanged(null, null, 15); + EventBus eventBus = new AsyncEventBus(sameThreadExecutor()); + eventBus.register(new SoneInserter(core, eventBus, freenetInterface, "SoneId")); + eventBus.post(new InsertionDelayChangedEvent(15)); assertThat(SoneInserter.getInsertionDelay().get(), is(15)); }