From e3d95929c7ef6c53bf09d9b0ba06e9eb9ce1ffe1 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:36:17 +0100 Subject: [PATCH] Convert update events to EventBus-based events. --- .../java/net/pterodactylus/sone/core/Core.java | 20 +------- .../net/pterodactylus/sone/core/UpdateChecker.java | 46 ++++++----------- .../pterodactylus/sone/core/UpdateListener.java | 44 ----------------- .../sone/core/UpdateListenerManager.java | 57 ---------------------- 4 files changed, 17 insertions(+), 150 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/core/UpdateListener.java delete mode 100644 src/main/java/net/pterodactylus/sone/core/UpdateListenerManager.java diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 57913b2..bd1bf37 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -47,7 +47,6 @@ import net.pterodactylus.sone.core.event.PostReplyRemovedEvent; import net.pterodactylus.sone.core.event.SoneLockedEvent; import net.pterodactylus.sone.core.event.SoneRemovedEvent; import net.pterodactylus.sone.core.event.SoneUnlockedEvent; -import net.pterodactylus.sone.core.event.UpdateFoundEvent; import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Image; @@ -79,7 +78,6 @@ import net.pterodactylus.util.validation.EqualityValidator; import net.pterodactylus.util.validation.IntegerRangeValidator; import net.pterodactylus.util.validation.OrValidator; import net.pterodactylus.util.validation.Validation; -import net.pterodactylus.util.version.Version; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; @@ -94,7 +92,7 @@ import freenet.keys.FreenetURI; * * @author David ‘Bombe’ Roden */ -public class Core extends AbstractService implements IdentityListener, UpdateListener, SoneProvider, PostProvider { +public class Core extends AbstractService implements IdentityListener, SoneProvider, PostProvider { /** The logger. */ private static final Logger logger = Logging.getLogger(Core.class); @@ -219,7 +217,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis this.identityManager = identityManager; this.soneDownloader = new SoneDownloader(this, freenetInterface); this.imageInserter = new ImageInserter(freenetInterface); - this.updateChecker = new UpdateChecker(freenetInterface); + this.updateChecker = new UpdateChecker(eventBus, freenetInterface); this.webOfTrustUpdater = webOfTrustUpdater; this.eventBus = eventBus; } @@ -1886,7 +1884,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis @Override public void serviceStart() { loadConfiguration(); - updateChecker.addUpdateListener(this); updateChecker.start(); identityManager.addIdentityListener(this); identityManager.start(); @@ -1927,7 +1924,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis saveConfiguration(); webOfTrustUpdater.stop(); updateChecker.stop(); - updateChecker.removeUpdateListener(this); soneDownloader.stop(); identityManager.removeIdentityListener(this); identityManager.stop(); @@ -2414,18 +2410,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis eventBus.post(new SoneRemovedEvent(sone)); } - // - // INTERFACE UpdateListener - // - - /** - * {@inheritDoc} - */ - @Override - public void updateFound(Version version, long releaseTime, long latestEdition) { - eventBus.post(new UpdateFoundEvent(version, releaseTime, latestEdition)); - } - /** * Deletes the temporary image. * diff --git a/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java b/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java index 5ccc96d..236ac12 100644 --- a/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java +++ b/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java @@ -27,10 +27,15 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.sone.core.FreenetInterface.Fetched; +import net.pterodactylus.sone.core.event.UpdateFoundEvent; import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.version.Version; + +import com.google.common.eventbus.EventBus; +import com.google.inject.Inject; + import freenet.keys.FreenetURI; import freenet.support.api.Bucket; @@ -50,12 +55,12 @@ public class UpdateChecker { /** The current latest known edition. */ private static final int LATEST_EDITION = 55; + /** The event bus. */ + private final EventBus eventBus; + /** The Freenet interface. */ private final FreenetInterface freenetInterface; - /** The update listener manager. */ - private final UpdateListenerManager updateListenerManager = new UpdateListenerManager(); - /** The current URI of the homepage. */ private FreenetURI currentUri; @@ -71,38 +76,18 @@ public class UpdateChecker { /** * Creates a new update checker. * + * @param eventBus + * The event bus * @param freenetInterface * The freenet interface to use */ - public UpdateChecker(FreenetInterface freenetInterface) { + @Inject + public UpdateChecker(EventBus eventBus, FreenetInterface freenetInterface) { + this.eventBus = eventBus; this.freenetInterface = freenetInterface; } // - // EVENT LISTENER MANAGEMENT - // - - /** - * Adds the given listener to the list of registered listeners. - * - * @param updateListener - * The listener to add - */ - public void addUpdateListener(UpdateListener updateListener) { - updateListenerManager.addListener(updateListener); - } - - /** - * Removes the given listener from the list of registered listeners. - * - * @param updateListener - * The listener to remove - */ - public void removeUpdateListener(UpdateListener updateListener) { - updateListenerManager.removeListener(updateListener); - } - - // // ACCESSORS // @@ -201,8 +186,7 @@ public class UpdateChecker { * Parses the properties of the latest version and fires events, if * necessary. * - * @see UpdateListener#updateFound(Version, long, long) - * @see UpdateListenerManager#fireUpdateFound(Version, long, long) + * @see UpdateFoundEvent * @param propertiesInputStream * The input stream to parse * @param edition @@ -240,7 +224,7 @@ public class UpdateChecker { currentLatestVersion = version; latestVersionDate = releaseTime; logger.log(Level.INFO, String.format("Found new version: %s (%tc)", version, new Date(releaseTime))); - updateListenerManager.fireUpdateFound(version, releaseTime, edition); + eventBus.post(new UpdateFoundEvent(version, releaseTime, edition)); } } diff --git a/src/main/java/net/pterodactylus/sone/core/UpdateListener.java b/src/main/java/net/pterodactylus/sone/core/UpdateListener.java deleted file mode 100644 index 2d52f16..0000000 --- a/src/main/java/net/pterodactylus/sone/core/UpdateListener.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Sone - UpdateListener.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.util.version.Version; - -/** - * Listener interface for {@link UpdateChecker} events. - * - * @author David ‘Bombe’ Roden - */ -public interface UpdateListener extends EventListener { - - /** - * Notifies a listener that a newer version than the current version was - * found. - * - * @param version - * The version that was found - * @param releaseTime - * The release time of the version - * @param latestEdition - * The latest edition of the Sone homepage - */ - public void updateFound(Version version, long releaseTime, long latestEdition); - -} diff --git a/src/main/java/net/pterodactylus/sone/core/UpdateListenerManager.java b/src/main/java/net/pterodactylus/sone/core/UpdateListenerManager.java deleted file mode 100644 index 31fdbed..0000000 --- a/src/main/java/net/pterodactylus/sone/core/UpdateListenerManager.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Sone - UpdateListenerManager.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.util.event.AbstractListenerManager; -import net.pterodactylus.util.version.Version; - -/** - * Listener manager for {@link UpdateListener} events. - * - * @author David ‘Bombe’ Roden - */ -public class UpdateListenerManager extends AbstractListenerManager { - - /** - * Creates a new update listener manager. - */ - public UpdateListenerManager() { - super(null); - } - - // - // ACTIONS - // - - /** - * Notifies all listeners that a new version has been found. - * - * @param version - * The new version - * @param releaseTime - * The release time of the new version - * @param latestEdition - * The latest edition of the Sone homepage - */ - void fireUpdateFound(Version version, long releaseTime, long latestEdition) { - for (UpdateListener updateListener : getListeners()) { - updateListener.updateFound(version, releaseTime, latestEdition); - } - } - -} -- 2.7.4