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;
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;
this.webOfTrustUpdater = webOfTrustUpdater;
this.eventBus = eventBus;
this.database = database;
+ preferences = new Preferences(this.eventBus, options);
}
@VisibleForTesting
this.webOfTrustUpdater = webOfTrustUpdater;
this.eventBus = eventBus;
this.database = database;
+ preferences = new Preferences(this.eventBus, options);
}
//
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);
}
*/
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))));
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.
*/
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;
}
*/
public Preferences setInsertionDelay(Integer insertionDelay) {
options.getIntegerOption("InsertionDelay").set(insertionDelay);
+ eventBus.post(new InsertionDelayChangedEvent(getInsertionDelay()));
return this;
}
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;
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;
}
}
- 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());
}
/**
--- /dev/null
+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;
+ }
+
+}
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;
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();
}
@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();
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;
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;
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;
@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));
}