    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 tracked by this list.
    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        if (listener != null) {
    055            listeners.add(listener);
    056        }
    057    }
    058
    059    /**
    060     * Gets the number of elements in this list.
    061     *
    062     * @return the number of elements in this list
    063     */
    064    public int getListenerCount() {
    065        return listeners.size();
    066    }
    067
    068    /**
    069     * Tests whether if this listener list is empty.
    070     *
    071     * @return whether if this listener list is empty.
    072     * @since 3.12.0
    073     */
    074    public boolean isEmpty() {
    075        return getListenerCount() == 0;
    076    }
    077
    078    /**
    079     * Return an {@link Iterator} for the {@link EventListener} instances.
    080     *
    081     * @return an {@link Iterator} for the {@link EventListener} instances
    082     * @since 2.0 TODO Check that this is a good defensive strategy
    083     */
    084    @Override
    085    public Iterator<T> iterator() {
    086        return listeners.iterator();
    087    }
    088
    089    /**
    090     * Throws UnsupportedOperationException.
    091     *
    092     * @param ignored Ignore.
    093     */
    094    private void readObject(final ObjectInputStream ignored) {
    095        throw new UnsupportedOperationException("Serialization is not supported");
    096    }
    097
    098    /**
    099     * Removes the first occurrence of the specified listener from this list, if it is present.
    100     *
    101     * @param listener listener to be removed from this list, if present.
    102     */
    103    public void removeListener(final T listener) {
    104        if (listener != null) {
    105            listeners.remove(listener);
    106        }
    107    }
    108
    109    /**
    110     * Always throws {@link UnsupportedOperationException}.
    111     *
    112     * @param ignored ignored.
    113     * @throws UnsupportedOperationException Always thrown.
    114     */
    115    private void writeObject(final ObjectOutputStream ignored) {
    116        throw new UnsupportedOperationException("Serialization is not supported");
    117    }
    118
    119}