This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-net.git
commit ce88e2920431368df202aa95aa8a3e71892c189f Author: Gary D. Gregory <[email protected]> AuthorDate: Wed Jul 16 13:34:53 2025 -0400 Add generics to ListenerList --- src/changes/changes.xml | 1 + .../org/apache/commons/net/ProtocolCommandSupport.java | 13 ++++++------- .../java/org/apache/commons/net/io/CopyStreamAdapter.java | 15 +++++++-------- .../java/org/apache/commons/net/util/ListenerList.java | 12 +++++++----- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 27f42679..363b3116 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -107,6 +107,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="add" dev="ggregory" due-to="Gary Gregory" issue="NET-727">Add accessing options map for TFTP request packet and allow using 'blksize' option #331.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.net.util.ListenerList.isEmpty().</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.net.ftp.FTPClient.getSystemTypeOverride().</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add generics to ListenerList.</action> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 70 to 85 #261, #278, #280, #285, #298, #293, #300, #345.</action> <action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-lang3 from 3.14.0 to 3.18.0 #268, #273, #281, #354.</action> diff --git a/src/main/java/org/apache/commons/net/ProtocolCommandSupport.java b/src/main/java/org/apache/commons/net/ProtocolCommandSupport.java index a15ffd81..2ef8bab7 100644 --- a/src/main/java/org/apache/commons/net/ProtocolCommandSupport.java +++ b/src/main/java/org/apache/commons/net/ProtocolCommandSupport.java @@ -19,7 +19,6 @@ package org.apache.commons.net; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; -import java.util.EventListener; import org.apache.commons.net.util.ListenerList; @@ -42,7 +41,7 @@ public class ProtocolCommandSupport implements Serializable { /** * The ProtocolCommandListener. */ - private final ListenerList listeners; + private final ListenerList<ProtocolCommandListener> listeners; /** * Creates a ProtocolCommandSupport instance using the indicated source as the source of ProtocolCommandEvents. @@ -50,7 +49,7 @@ public class ProtocolCommandSupport implements Serializable { * @param source The source to use for all generated ProtocolCommandEvents. */ public ProtocolCommandSupport(final Object source) { - this.listeners = new ListenerList(); + this.listeners = new ListenerList<>(); this.source = source; } @@ -73,8 +72,8 @@ public class ProtocolCommandSupport implements Serializable { public void fireCommandSent(final String command, final String message) { if (!listeners.isEmpty()) { final ProtocolCommandEvent event = new ProtocolCommandEvent(source, command, message); - for (final EventListener listener : listeners) { - ((ProtocolCommandListener) listener).protocolCommandSent(event); + for (final ProtocolCommandListener listener : listeners) { + listener.protocolCommandSent(event); } } } @@ -91,8 +90,8 @@ public class ProtocolCommandSupport implements Serializable { public void fireReplyReceived(final int replyCode, final String message) { if (!listeners.isEmpty()) { final ProtocolCommandEvent event = new ProtocolCommandEvent(source, replyCode, message); - for (final EventListener listener : listeners) { - ((ProtocolCommandListener) listener).protocolReplyReceived(event); + for (final ProtocolCommandListener listener : listeners) { + listener.protocolReplyReceived(event); } } } diff --git a/src/main/java/org/apache/commons/net/io/CopyStreamAdapter.java b/src/main/java/org/apache/commons/net/io/CopyStreamAdapter.java index 318c2e52..82863008 100644 --- a/src/main/java/org/apache/commons/net/io/CopyStreamAdapter.java +++ b/src/main/java/org/apache/commons/net/io/CopyStreamAdapter.java @@ -17,8 +17,6 @@ package org.apache.commons.net.io; -import java.util.EventListener; - import org.apache.commons.net.util.ListenerList; /** @@ -33,13 +31,14 @@ import org.apache.commons.net.util.ListenerList; * @see Util */ public class CopyStreamAdapter implements CopyStreamListener { - private final ListenerList internalListeners; + + private final ListenerList<CopyStreamListener> internalListeners; /** * Creates a new copyStreamAdapter. */ public CopyStreamAdapter() { - internalListeners = new ListenerList(); + internalListeners = new ListenerList<>(); } /** @@ -61,8 +60,8 @@ public class CopyStreamAdapter implements CopyStreamListener { */ @Override public void bytesTransferred(final CopyStreamEvent event) { - for (final EventListener listener : internalListeners) { - ((CopyStreamListener) listener).bytesTransferred(event); + for (final CopyStreamListener listener : internalListeners) { + listener.bytesTransferred(event); } } @@ -78,8 +77,8 @@ public class CopyStreamAdapter implements CopyStreamListener { */ @Override public void bytesTransferred(final long totalBytesTransferred, final int bytesTransferred, final long streamSize) { - for (final EventListener listener : internalListeners) { - ((CopyStreamListener) listener).bytesTransferred(totalBytesTransferred, bytesTransferred, streamSize); + for (final CopyStreamListener listener : internalListeners) { + listener.bytesTransferred(totalBytesTransferred, bytesTransferred, streamSize); } } diff --git a/src/main/java/org/apache/commons/net/util/ListenerList.java b/src/main/java/org/apache/commons/net/util/ListenerList.java index 423b3979..636f5e08 100644 --- a/src/main/java/org/apache/commons/net/util/ListenerList.java +++ b/src/main/java/org/apache/commons/net/util/ListenerList.java @@ -26,15 +26,17 @@ import java.util.concurrent.CopyOnWriteArrayList; /** * A list of event listeners. + * + * @param <T> the type of elements returned by the iterator */ -public class ListenerList implements Serializable, Iterable<EventListener> { +public class ListenerList<T extends EventListener> implements Serializable, Iterable<T> { private static final long serialVersionUID = -1934227607974228213L; /** * The thread-safe list of listeners. */ - private final CopyOnWriteArrayList<EventListener> listeners; + private final CopyOnWriteArrayList<T> listeners; /** * Constructs a new instance. @@ -48,7 +50,7 @@ public class ListenerList implements Serializable, Iterable<EventListener> { * * @param listener A listener. */ - public void addListener(final EventListener listener) { + public void addListener(final T listener) { listeners.add(listener); } @@ -78,7 +80,7 @@ public class ListenerList implements Serializable, Iterable<EventListener> { * @since 2.0 TODO Check that this is a good defensive strategy */ @Override - public Iterator<EventListener> iterator() { + public Iterator<T> iterator() { return listeners.iterator(); } @@ -96,7 +98,7 @@ public class ListenerList implements Serializable, Iterable<EventListener> { * * @param listener listener to be removed from this list, if present. */ - public void removeListener(final EventListener listener) { + public void removeListener(final T listener) { listeners.remove(listener); }
