Use events to communicate changes to insertion delay configuration.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Sep 2014 14:00:59 +0000 (16:00 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Sep 2014 18:39:33 +0000 (20:39 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/Preferences.java
src/main/java/net/pterodactylus/sone/core/SoneInserter.java
src/main/java/net/pterodactylus/sone/core/event/InsertionDelayChangedEvent.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/core/PreferencesTest.java
src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java

index c9c6da3..f7b35e9 100644 (file)
@@ -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<Integer>(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new SetInsertionDelay()));
+               options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new IntegerRangePredicate(0, Integer.MAX_VALUE)));
                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.<Integer> or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
index 16d9453..c413f89 100644 (file)
 
 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;
        }
 
index 9f1f164..c40fc86 100644 (file)
@@ -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<Integer> {
-
-               @Override
-               public void optionChanged(Option<Integer> 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 (file)
index 0000000..a3dc2ce
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class InsertionDelayChangedEvent {
+
+       private final int insertionDelay;
+
+       public InsertionDelayChangedEvent(int insertionDelay) {
+               this.insertionDelay = insertionDelay;
+       }
+
+       public int getInsertionDelay() {
+               return insertionDelay;
+       }
+
+}
index cd9130d..e2caaae 100644 (file)
@@ -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<Integer> integerOption = when(mock(Option.class).get()).thenReturn(INTEGER_VALUE).getMock();
        private final Option<Boolean> booleanOption = when(mock(Option.class).get()).thenReturn(true).getMock();
        private final Option<String> 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();
index 28ead14..d43f383 100644 (file)
@@ -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));
        }