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 * http://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 */ 029 030public class ListenerList implements Serializable, Iterable<EventListener> { 031 private static final long serialVersionUID = -1934227607974228213L; 032 033 private final CopyOnWriteArrayList<EventListener> listeners; 034 035 public ListenerList() { 036 listeners = new CopyOnWriteArrayList<>(); 037 } 038 039 public void addListener(final EventListener listener) { 040 listeners.add(listener); 041 } 042 043 public int getListenerCount() { 044 return listeners.size(); 045 } 046 047 /** 048 * Return an {@link Iterator} for the {@link EventListener} instances. 049 * 050 * @return an {@link Iterator} for the {@link EventListener} instances 051 * @since 2.0 TODO Check that this is a good defensive strategy 052 */ 053 @Override 054 public Iterator<EventListener> iterator() { 055 return listeners.iterator(); 056 } 057 058 private void readObject(final ObjectInputStream in) { 059 throw new UnsupportedOperationException("Serialization is not supported"); 060 } 061 062 /* 063 * Serialization is unnecessary for this class. Reject attempts to do so until such time as the Serializable attribute can be dropped. 064 */ 065 066 public void removeListener(final EventListener listener) { 067 listeners.remove(listener); 068 } 069 070 private void writeObject(final ObjectOutputStream out) { 071 throw new UnsupportedOperationException("Serialization is not supported"); 072 } 073 074}