1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.net.util;
19
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.util.EventListener;
24 import java.util.Iterator;
25 import java.util.concurrent.CopyOnWriteArrayList;
26
27 /**
28 * A list of event listeners.
29 *
30 * @param <T> the type of elements returned by the iterator
31 */
32 public class ListenerList<T extends EventListener> implements Serializable, Iterable<T> {
33
34 private static final long serialVersionUID = -1934227607974228213L;
35
36 /**
37 * The thread-safe list of listeners.
38 */
39 private final CopyOnWriteArrayList<T> listeners;
40
41 /**
42 * Constructs a new instance.
43 */
44 public ListenerList() {
45 listeners = new CopyOnWriteArrayList<>();
46 }
47
48 /**
49 * Adds the given listener to the end of this list.
50 *
51 * @param listener A listener.
52 */
53 public void addListener(final T listener) {
54 listeners.add(listener);
55 }
56
57 /**
58 * Gets the number of elements in this list.
59 *
60 * @return the number of elements in this list
61 */
62 public int getListenerCount() {
63 return listeners.size();
64 }
65
66 /**
67 * Tests whether if this listener list is empty.
68 *
69 * @return whether if this listener list is empty.
70 * @since 3.12.0
71 */
72 public boolean isEmpty() {
73 return getListenerCount() == 0;
74 }
75
76 /**
77 * Return an {@link Iterator} for the {@link EventListener} instances.
78 *
79 * @return an {@link Iterator} for the {@link EventListener} instances
80 * @since 2.0 TODO Check that this is a good defensive strategy
81 */
82 @Override
83 public Iterator<T> iterator() {
84 return listeners.iterator();
85 }
86
87 /**
88 * Throws UnsupportedOperationException.
89 *
90 * @param ignored Ignore.
91 */
92 private void readObject(final ObjectInputStream ignored) {
93 throw new UnsupportedOperationException("Serialization is not supported");
94 }
95
96 /**
97 * Removes the first occurrence of the specified listener from this list, if it is present.
98 *
99 * @param listener listener to be removed from this list, if present.
100 */
101 public void removeListener(final T listener) {
102 listeners.remove(listener);
103 }
104
105 /**
106 * Always throws {@link UnsupportedOperationException}.
107 *
108 * @param ignored ignored.
109 * @throws UnsupportedOperationException Always thrown.
110 */
111 private void writeObject(final ObjectOutputStream ignored) {
112 throw new UnsupportedOperationException("Serialization is not supported");
113 }
114
115 }