package net.pterodactylus.fcp;
-import java.util.List;
-import java.util.concurrent.CopyOnWriteArrayList;
+import net.pterodactylus.util.event.AbstractListenerManager;
/**
* Manages FCP listeners and event firing.
*
* @author David ‘Bombe’ Roden <bombe@pterodactylus.net>
*/
-public class FcpListenerManager {
-
- /** The source FCP connection. */
- private final FcpConnection fcpConnection;
-
- /** The registered listeners. */
- private final List<FcpListener> fcpListeners = new CopyOnWriteArrayList<FcpListener>();
+public class FcpListenerManager extends AbstractListenerManager<FcpConnection, FcpListener> {
/**
* Creates a new listener manager.
* The source FCP connection
*/
public FcpListenerManager(FcpConnection fcpConnection) {
- this.fcpConnection = fcpConnection;
- }
-
- /**
- * Adds the given FCP listener to the list of registered listeners.
- *
- * @param fcpListener
- * The FCP listener to add
- */
- public void addListener(FcpListener fcpListener) {
- fcpListeners.add(fcpListener);
- }
-
- /**
- * Removes the given FCP listener from the list of registered listeners.
- *
- * @param fcpListener
- * The FCP listener to remove
- */
- public void removeListener(FcpListener fcpListener) {
- fcpListeners.remove(fcpListener);
+ super(fcpConnection);
}
/**
* The “NodeHello” message
*/
public void fireReceivedNodeHello(NodeHello nodeHello) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedNodeHello(fcpConnection, nodeHello);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedNodeHello(getSource(), nodeHello);
}
}
* The “CloseConnectionDuplicateClientName” message
*/
public void fireReceivedCloseConnectionDuplicateClientName(CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedCloseConnectionDuplicateClientName(fcpConnection, closeConnectionDuplicateClientName);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedCloseConnectionDuplicateClientName(getSource(), closeConnectionDuplicateClientName);
}
}
* The “SSKKeypair” message
*/
public void fireReceivedSSKKeypair(SSKKeypair sskKeypair) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedSSKKeypair(fcpConnection, sskKeypair);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedSSKKeypair(getSource(), sskKeypair);
}
}
* The “Peer” message
*/
public void fireReceivedPeer(Peer peer) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedPeer(fcpConnection, peer);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedPeer(getSource(), peer);
}
}
* The “EndListPeers” message
*/
public void fireReceivedEndListPeers(EndListPeers endListPeers) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedEndListPeers(fcpConnection, endListPeers);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedEndListPeers(getSource(), endListPeers);
}
}
* @param peerNote
*/
public void fireReceivedPeerNote(PeerNote peerNote) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedPeerNote(fcpConnection, peerNote);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedPeerNote(getSource(), peerNote);
}
}
* The “EndListPeerNotes” message
*/
public void fireReceivedEndListPeerNotes(EndListPeerNotes endListPeerNotes) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedEndListPeerNotes(fcpConnection, endListPeerNotes);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedEndListPeerNotes(getSource(), endListPeerNotes);
}
}
* The “PeerRemoved” message
*/
public void fireReceivedPeerRemoved(PeerRemoved peerRemoved) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedPeerRemoved(fcpConnection, peerRemoved);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedPeerRemoved(getSource(), peerRemoved);
}
}
* The “NodeData” message
*/
public void fireReceivedNodeData(NodeData nodeData) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedNodeData(fcpConnection, nodeData);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedNodeData(getSource(), nodeData);
}
}
* The “TestDDAReply” message
*/
public void fireReceivedTestDDAReply(TestDDAReply testDDAReply) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedTestDDAReply(fcpConnection, testDDAReply);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedTestDDAReply(getSource(), testDDAReply);
}
}
* The “TestDDAComplete” message
*/
public void fireReceivedTestDDAComplete(TestDDAComplete testDDAComplete) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedTestDDAComplete(fcpConnection, testDDAComplete);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedTestDDAComplete(getSource(), testDDAComplete);
}
}
* The “PersistentGet” message
*/
public void fireReceivedPersistentGet(PersistentGet persistentGet) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedPersistentGet(fcpConnection, persistentGet);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedPersistentGet(getSource(), persistentGet);
}
}
* The “PersistentPut” message
*/
public void fireReceivedPersistentPut(PersistentPut persistentPut) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedPersistentPut(fcpConnection, persistentPut);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedPersistentPut(getSource(), persistentPut);
}
}
* The “EndListPersistentRequests” message
*/
public void fireReceivedEndListPersistentRequests(EndListPersistentRequests endListPersistentRequests) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedEndListPersistentRequests(fcpConnection, endListPersistentRequests);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedEndListPersistentRequests(getSource(), endListPersistentRequests);
}
}
* The “URIGenerated” message
*/
public void fireReceivedURIGenerated(URIGenerated uriGenerated) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedURIGenerated(fcpConnection, uriGenerated);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedURIGenerated(getSource(), uriGenerated);
}
}
* The “DataFound” message
*/
public void fireReceivedDataFound(DataFound dataFound) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedDataFound(fcpConnection, dataFound);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedDataFound(getSource(), dataFound);
}
}
* The “AllData” message
*/
public void fireReceivedAllData(AllData allData) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedAllData(fcpConnection, allData);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedAllData(getSource(), allData);
}
}
* The “SimpleProgress” message
*/
public void fireReceivedSimpleProgress(SimpleProgress simpleProgress) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedSimpleProgress(fcpConnection, simpleProgress);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedSimpleProgress(getSource(), simpleProgress);
}
}
* The “StartedCompression” message
*/
public void fireReceivedStartedCompression(StartedCompression startedCompression) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedStartedCompression(fcpConnection, startedCompression);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedStartedCompression(getSource(), startedCompression);
}
}
* The “FinishedCompression” message
*/
public void fireReceivedFinishedCompression(FinishedCompression finishedCompression) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receviedFinishedCompression(fcpConnection, finishedCompression);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receviedFinishedCompression(getSource(), finishedCompression);
}
}
* The “UnknownPeerNoteType” message
*/
public void fireReceivedUnknownPeerNoteType(UnknownPeerNoteType unknownPeerNoteType) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedUnknownPeerNoteType(fcpConnection, unknownPeerNoteType);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedUnknownPeerNoteType(getSource(), unknownPeerNoteType);
}
}
* The “UnknownNodeIdentifier” message
*/
public void fireReceivedUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedUnknownNodeIdentifier(fcpConnection, unknownNodeIdentifier);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedUnknownNodeIdentifier(getSource(), unknownNodeIdentifier);
}
}
* The “ConfigData” message
*/
public void fireReceivedConfigData(ConfigData configData) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedConfigData(fcpConnection, configData);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedConfigData(getSource(), configData);
}
}
* The “GetFailed” message
*/
public void fireReceivedGetFailed(GetFailed getFailed) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedGetFailed(fcpConnection, getFailed);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedGetFailed(getSource(), getFailed);
}
}
* The “PutFailed” message
*/
public void fireReceivedPutFailed(PutFailed putFailed) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedPutFailed(fcpConnection, putFailed);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedPutFailed(getSource(), putFailed);
}
}
* The “IdentifierCollision” message
*/
public void fireReceivedIdentifierCollision(IdentifierCollision identifierCollision) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedIdentifierCollision(fcpConnection, identifierCollision);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedIdentifierCollision(getSource(), identifierCollision);
}
}
* The “PersistentPutDir” message
*/
public void fireReceivedPersistentPutDir(PersistentPutDir persistentPutDir) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedPersistentPutDir(fcpConnection, persistentPutDir);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedPersistentPutDir(getSource(), persistentPutDir);
}
}
* The “PersistentRequestRemoved” message
*/
public void fireReceivedPersistentRequestRemoved(PersistentRequestRemoved persistentRequestRemoved) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedPersistentRequestRemoved(fcpConnection, persistentRequestRemoved);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedPersistentRequestRemoved(getSource(), persistentRequestRemoved);
}
}
* The “SubscribedUSKUpdate” message
*/
public void fireReceivedSubscribedUSKUpdate(SubscribedUSKUpdate subscribedUSKUpdate) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedSubscribedUSKUpdate(fcpConnection, subscribedUSKUpdate);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedSubscribedUSKUpdate(getSource(), subscribedUSKUpdate);
}
}
* The “PluginInfo” message
*/
public void fireReceivedPluginInfo(PluginInfo pluginInfo) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedPluginInfo(fcpConnection, pluginInfo);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedPluginInfo(getSource(), pluginInfo);
}
}
* The “FCPPluginReply” message
*/
public void fireReceivedFCPPluginReply(FCPPluginReply fcpPluginReply) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedFCPPluginReply(fcpConnection, fcpPluginReply);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedFCPPluginReply(getSource(), fcpPluginReply);
}
}
* The “PersistentRequestModified” message
*/
public void fireReceivedPersistentRequestModified(PersistentRequestModified persistentRequestModified) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedPersistentRequestModified(fcpConnection, persistentRequestModified);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedPersistentRequestModified(getSource(), persistentRequestModified);
}
}
* The “PutSuccessful” message
*/
public void fireReceivedPutSuccessful(PutSuccessful putSuccessful) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedPutSuccessful(fcpConnection, putSuccessful);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedPutSuccessful(getSource(), putSuccessful);
}
}
* The “PutFetchable” message
*/
public void fireReceivedPutFetchable(PutFetchable putFetchable) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedPutFetchable(fcpConnection, putFetchable);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedPutFetchable(getSource(), putFetchable);
}
}
* The “ProtocolError” message
*/
public void fireReceivedProtocolError(ProtocolError protocolError) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedProtocolError(fcpConnection, protocolError);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedProtocolError(getSource(), protocolError);
}
}
* The message that was received
*/
public void fireMessageReceived(FcpMessage fcpMessage) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.receivedMessage(fcpConnection, fcpMessage);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.receivedMessage(getSource(), fcpMessage);
}
}
* @see FcpListener#connectionClosed(FcpConnection, Throwable)
*/
public void fireConnectionClosed(Throwable throwable) {
- for (FcpListener fcpListener : fcpListeners) {
- fcpListener.connectionClosed(fcpConnection, throwable);
+ for (FcpListener fcpListener : getListeners()) {
+ fcpListener.connectionClosed(getSource(), throwable);
}
}
--- /dev/null
+/*
+ * ftw - AbstractListenerManager.java - Copyright © 2009 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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.util.event;
+
+import java.util.EventListener;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.Executor;
+
+import net.pterodactylus.util.thread.CurrentThreadExecutor;
+
+/**
+ * Abstract implementation of a listener support class. The listener support
+ * takes care of adding and removing {@link EventListener} implementations, and
+ * subclasses are responsible for firing appropriate events.
+ *
+ * @param <S>
+ * The type of the source
+ *@param <L>
+ * The type of the event listeners
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public abstract class AbstractListenerManager<S, L extends EventListener> {
+
+ /** The source that emits the events. */
+ private final S source;
+
+ /** The list of listeners. */
+ private final List<L> listeners = new CopyOnWriteArrayList<L>();
+
+ /** Service that executes event threads. */
+ private final Executor executor;
+
+ /**
+ * Creates a new listener support that emits events from the given source.
+ *
+ * @param source
+ * The source of the events
+ */
+ public AbstractListenerManager(S source) {
+ this(source, new CurrentThreadExecutor());
+ }
+
+ /**
+ * Creates a new listener support that emits events from the given source.
+ *
+ * @param source
+ * The source of the events
+ * @param executor
+ * The executor used to fire events
+ */
+ public AbstractListenerManager(S source, Executor executor) {
+ this.source = source;
+ this.executor = executor;
+ }
+
+ /**
+ * Adds the given listener to the list of reigstered listeners.
+ *
+ * @param listener
+ * The listener to add
+ */
+ public void addListener(L listener) {
+ synchronized (listeners) {
+ listeners.add(listener);
+ }
+ }
+
+ /**
+ * Removes the given listener from the list of registered listeners.
+ *
+ * @param listener
+ * The listener to remove
+ */
+ public void removeListener(L listener) {
+ synchronized (listeners) {
+ listeners.remove(listener);
+ }
+ }
+
+ //
+ // PROTECTED METHODS
+ //
+
+ /**
+ * Returns the source for the events.
+ *
+ * @return The event source
+ */
+ protected S getSource() {
+ return source;
+ }
+
+ /**
+ * Returns the executor for the event firing.
+ *
+ * @return The executor
+ */
+ protected Executor getExecutor() {
+ return executor;
+ }
+
+ /**
+ * Returns a list of all registered listeners. The returned list is a copy
+ * of the original list so structural modifications will never occur when
+ * using the returned list.
+ *
+ * @return The list of all registered listeners
+ */
+ protected List<L> getListeners() {
+ return listeners;
+ }
+
+}