001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.util;
019
020import java.io.ObjectInputStream;
021import java.io.ObjectOutputStream;
022import java.io.Serializable;
023import java.util.EventListener;
024import java.util.Iterator;
025import java.util.concurrent.CopyOnWriteArrayList;
026
027/**
028 * A list of event listeners.
029 *
030 * @param <T> the type of elements returned by the iterator
031 */
032public class ListenerList<T extends EventListener> implements Serializable, Iterable<T> {
033
034    private static final long serialVersionUID = -1934227607974228213L;
035
036    /**
037     * The thread-safe list of listeners.
038     */
039    private final CopyOnWriteArrayList<T> listeners;
040
041    /**
042     * Constructs a new instance.
043     */
044    public ListenerList() {
045        listeners = new CopyOnWriteArrayList<>();
046    }
047
048    /**
049     * Adds the given listener to the end of this list.
050     *
051     * @param listener A listener.
052     */
053    public void addListener(final T listener) {
054        listeners.add(listener);
055    }
056
057    /**
058     * Gets the number of elements in this list.
059     *
060     * @return the number of elements in this list
061     */
062    public int getListenerCount() {
063        return listeners.size();
064    }
065
066    /**
067     * Tests whether if this listener list is empty.
068     *
069     * @return whether if this listener list is empty.
070     * @since 3.12.0
071     */
072    public boolean isEmpty() {
073        return getListenerCount() == 0;
074    }
075
076    /**
077     * Return an {@link Iterator} for the {@link EventListener} instances.
078     *
079     * @return an {@link Iterator} for the {@link EventListener} instances
080     * @since 2.0 TODO Check that this is a good defensive strategy
081     */
082    @Override
083    public Iterator<T> iterator() {
084        return listeners.iterator();
085    }
086
087    /**
088     * Throws UnsupportedOperationException.
089     *
090     * @param ignored Ignore.
091     */
092    private void readObject(final ObjectInputStream ignored) {
093        throw new UnsupportedOperationException("Serialization is not supported");
094    }
095
096    /**
097     * Removes the first occurrence of the specified listener from this list, if it is present.
098     *
099     * @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}