From 4276233daae926e02a1bbb8939645242f99918c7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 17 Jan 2013 06:32:12 +0100 Subject: [PATCH] Convert Sone insert events to EventBus-based events. --- .../java/net/pterodactylus/sone/core/Core.java | 38 +--------- .../sone/core/SoneInsertListener.java | 59 ---------------- .../sone/core/SoneInsertListenerManager.java | 82 ---------------------- .../net/pterodactylus/sone/core/SoneInserter.java | 40 +++-------- 4 files changed, 13 insertions(+), 206 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/core/SoneInsertListener.java delete mode 100644 src/main/java/net/pterodactylus/sone/core/SoneInsertListenerManager.java diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index e278629..57913b2 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -44,9 +44,6 @@ import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent; import net.pterodactylus.sone.core.event.NewSoneFoundEvent; import net.pterodactylus.sone.core.event.PostRemovedEvent; import net.pterodactylus.sone.core.event.PostReplyRemovedEvent; -import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent; -import net.pterodactylus.sone.core.event.SoneInsertedEvent; -import net.pterodactylus.sone.core.event.SoneInsertingEvent; import net.pterodactylus.sone.core.event.SoneLockedEvent; import net.pterodactylus.sone.core.event.SoneRemovedEvent; import net.pterodactylus.sone.core.event.SoneUnlockedEvent; @@ -97,7 +94,7 @@ import freenet.keys.FreenetURI; * * @author David ‘Bombe’ Roden */ -public class Core extends AbstractService implements IdentityListener, UpdateListener, SoneProvider, PostProvider, SoneInsertListener { +public class Core extends AbstractService implements IdentityListener, UpdateListener, SoneProvider, PostProvider { /** The logger. */ private static final Logger logger = Logging.getLogger(Core.class); @@ -824,8 +821,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis sone.setKnown(true); /* TODO - load posts ’n stuff */ sones.put(ownIdentity.getId(), sone); - final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone); - soneInserter.addSoneInsertListener(this); + final SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, sone); soneInserters.put(sone, soneInserter); sone.setStatus(SoneStatus.idle); loadSone(sone); @@ -1205,7 +1201,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } sones.remove(sone.getId()); SoneInserter soneInserter = soneInserters.remove(sone); - soneInserter.removeSoneInsertListener(this); soneInserter.stop(); } webOfTrustUpdater.removeContext((OwnIdentity) sone.getIdentity(), "Sone"); @@ -1925,7 +1920,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis public void serviceStop() { synchronized (sones) { for (Entry soneInserter : soneInserters.entrySet()) { - soneInserter.getValue().removeSoneInsertListener(this); soneInserter.getValue().stop(); saveSone(soneInserter.getKey()); } @@ -2432,34 +2426,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis eventBus.post(new UpdateFoundEvent(version, releaseTime, latestEdition)); } - // - // INTERFACE ImageInsertListener - // - - /** - * {@inheritDoc} - */ - @Override - public void insertStarted(Sone sone) { - eventBus.post(new SoneInsertingEvent(sone)); - } - - /** - * {@inheritDoc} - */ - @Override - public void insertFinished(Sone sone, long insertDuration) { - eventBus.post(new SoneInsertedEvent(sone, insertDuration)); - } - - /** - * {@inheritDoc} - */ - @Override - public void insertAborted(Sone sone, Throwable cause) { - eventBus.post(new SoneInsertAbortedEvent(sone, cause)); - } - /** * Deletes the temporary image. * diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInsertListener.java b/src/main/java/net/pterodactylus/sone/core/SoneInsertListener.java deleted file mode 100644 index 6f99fe6..0000000 --- a/src/main/java/net/pterodactylus/sone/core/SoneInsertListener.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Sone - SoneInsertListener.java - Copyright © 2011–2012 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 . - */ - -package net.pterodactylus.sone.core; - -import java.util.EventListener; - -import net.pterodactylus.sone.data.Sone; - -/** - * Listener for Sone insert events. - * - * @author David ‘Bombe’ Roden - */ -public interface SoneInsertListener extends EventListener { - - /** - * Notifies a listener that a Sone is now being inserted. - * - * @param sone - * The Sone being inserted - */ - public void insertStarted(Sone sone); - - /** - * Notifies a listener that a Sone has been successfully inserted. - * - * @param sone - * The Sone that was inserted - * @param insertDuration - * The duration of the insert (in milliseconds) - */ - public void insertFinished(Sone sone, long insertDuration); - - /** - * Notifies a listener that the insert of the given Sone was aborted. - * - * @param sone - * The Sone that was being inserted - * @param cause - * The cause of the abortion (may be {@code null}) - */ - public void insertAborted(Sone sone, Throwable cause); - -} diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInsertListenerManager.java b/src/main/java/net/pterodactylus/sone/core/SoneInsertListenerManager.java deleted file mode 100644 index 6841e7f..0000000 --- a/src/main/java/net/pterodactylus/sone/core/SoneInsertListenerManager.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Sone - SoneInsertListenerManager.java - Copyright © 2011–2012 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 . - */ - -package net.pterodactylus.sone.core; - -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.util.event.AbstractListenerManager; - -/** - * Manager for {@link SoneInsertListener}s. - * - * @author David ‘Bombe’ Roden - */ -public class SoneInsertListenerManager extends AbstractListenerManager { - - /** - * Creates a new Sone insert listener manager. - * - * @param sone - * The sone being inserted - */ - public SoneInsertListenerManager(Sone sone) { - super(sone); - } - - // - // ACTIONS - // - - /** - * Notifies all listeners that the insert of the Sone has started. - * - * @see SoneInsertListener#insertStarted(Sone) - */ - void fireInsertStarted() { - for (SoneInsertListener soneInsertListener : getListeners()) { - soneInsertListener.insertStarted(getSource()); - } - } - - /** - * Notifies all listeners that the insert of the Sone has finished - * successfully. - * - * @see SoneInsertListener#insertFinished(Sone, long) - * @param insertDuration - * The insert duration (in milliseconds) - */ - void fireInsertFinished(long insertDuration) { - for (SoneInsertListener soneInsertListener : getListeners()) { - soneInsertListener.insertFinished(getSource(), insertDuration); - } - } - - /** - * Notifies all listeners that the insert of the Sone was aborted. - * - * @see SoneInsertListener#insertAborted(Sone, Throwable) - * @param cause - * The cause of the abortion (may be {@code null} - */ - void fireInsertAborted(Throwable cause) { - for (SoneInsertListener soneInsertListener : getListeners()) { - soneInsertListener.insertAborted(getSource(), cause); - } - } - -} diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 69d3db5..80255a1 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -26,6 +26,9 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent; +import net.pterodactylus.sone.core.event.SoneInsertedEvent; +import net.pterodactylus.sone.core.event.SoneInsertingEvent; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; @@ -45,6 +48,7 @@ import net.pterodactylus.util.template.TemplateParser; import net.pterodactylus.util.template.XmlFilter; import com.google.common.collect.Ordering; +import com.google.common.eventbus.EventBus; import freenet.client.async.ManifestElement; import freenet.keys.FreenetURI; @@ -77,15 +81,15 @@ public class SoneInserter extends AbstractService { /** The core. */ private final Core core; + /** The event bus. */ + private final EventBus eventBus; + /** The Freenet interface. */ private final FreenetInterface freenetInterface; /** The Sone to insert. */ private final Sone sone; - /** The insert listener manager. */ - private SoneInsertListenerManager soneInsertListenerManager; - /** Whether a modification has been detected. */ private volatile boolean modified = false; @@ -97,41 +101,19 @@ public class SoneInserter extends AbstractService { * * @param core * The core + * @param eventBus + * The event bus * @param freenetInterface * The freenet interface * @param sone * The Sone to insert */ - public SoneInserter(Core core, FreenetInterface freenetInterface, Sone sone) { + public SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, Sone sone) { super("Sone Inserter for “" + sone.getName() + "”", false); this.core = core; + this.eventBus = eventBus; this.freenetInterface = freenetInterface; this.sone = sone; - this.soneInsertListenerManager = new SoneInsertListenerManager(sone); - } - - // - // LISTENER MANAGEMENT - // - - /** - * Adds a listener for Sone insert events. - * - * @param soneInsertListener - * The Sone insert listener - */ - public void addSoneInsertListener(SoneInsertListener soneInsertListener) { - soneInsertListenerManager.addListener(soneInsertListener); - } - - /** - * Removes a listener for Sone insert events. - * - * @param soneInsertListener - * The Sone insert listener - */ - public void removeSoneInsertListener(SoneInsertListener soneInsertListener) { - soneInsertListenerManager.removeListener(soneInsertListener); } // -- 2.7.4