|
Java example source code file (EventListenerSupport.java)
The EventListenerSupport.java Java example source code/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3.event;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.commons.lang3.Validate;
/**
* <p>An EventListenerSupport object can be used to manage a list of event
* listeners of a particular type. The class provides
* {@link #addListener(Object)} and {@link #removeListener(Object)} methods
* for registering listeners, as well as a {@link #fire()} method for firing
* events to the listeners.
* </p>
*
* <p>
* To use this class, suppose you want to support ActionEvents. You would do:
* </p>
* <pre>
*
* <p>
* Serializing an {@link EventListenerSupport} instance will result in any
* non-{@link Serializable} listeners being silently dropped.
* </p>
*
* @param <L> the type of event listener that is supported by this proxy.
*
* @since 3.0
*/
public class EventListenerSupport<L> implements Serializable {
/** Serialization version */
private static final long serialVersionUID = 3593265990380473632L;
/**
* The list used to hold the registered listeners. This list is
* intentionally a thread-safe copy-on-write-array so that traversals over
* the list of listeners will be atomic.
*/
private List<L> listeners = new CopyOnWriteArrayList is
* <code>null.
* @throws IllegalArgumentException if <code>listenerInterface is
* not an interface.
*/
public static <T> EventListenerSupportnull .
* @throws IllegalArgumentException if <code>listenerInterface is
* not an interface.
*/
public EventListenerSupport(final Class<L> listenerInterface, final ClassLoader classLoader) {
this();
Validate.notNull(listenerInterface, "Listener interface cannot be null.");
Validate.notNull(classLoader, "ClassLoader cannot be null.");
Validate.isTrue(listenerInterface.isInterface(), "Class {0} is not an interface",
listenerInterface.getName());
initializeTransientFields(listenerInterface, classLoader);
}
/**
* Create a new EventListenerSupport instance.
* Serialization-friendly constructor.
*/
private EventListenerSupport() {
}
/**
* Returns a proxy object which can be used to call listener methods on all
* of the registered event listeners. All calls made to this proxy will be
* forwarded to all registered listeners.
*
* @return a proxy object which can be used to call listener methods on all
* of the registered event listeners
*/
public L fire() {
return proxy;
}
//**********************************************************************************************************************
// Other Methods
//**********************************************************************************************************************
/**
* Registers an event listener.
*
* @param listener the event listener (may not be <code>null).
*
* @throws NullPointerException if <code>listener is
* <code>null.
*/
public void addListener(final L listener) {
addListener(listener, true);
}
/**
* Registers an event listener. Will not add a pre-existing listener
* object to the list if <code>allowDuplicate is false.
*
* @param listener the event listener (may not be <code>null).
* @param allowDuplicate the flag for determining if duplicate listener
* objects are allowed to be registered.
*
* @throws NullPointerException if <code>listener is
* <code>null.
*/
public void addListener(final L listener, boolean allowDuplicate) {
Validate.notNull(listener, "Listener object cannot be null.");
if (allowDuplicate) {
listeners.add(listener);
} else if (!listeners.contains(listener)) {
listeners.add(listener);
}
}
/**
* Returns the number of registered listeners.
*
* @return the number of registered listeners.
*/
int getListenerCount() {
return listeners.size();
}
/**
* Unregisters an event listener.
*
* @param listener the event listener (may not be <code>null).
*
* @throws NullPointerException if <code>listener is
* <code>null.
*/
public void removeListener(final L listener) {
Validate.notNull(listener, "Listener object cannot be null.");
listeners.remove(listener);
}
/**
* Get an array containing the currently registered listeners.
* Modification to this array's elements will have no effect on the
* {@link EventListenerSupport} instance.
* @return L[]
*/
public L[] getListeners() {
return listeners.toArray(prototypeArray);
}
/**
* Serialize.
* @param objectOutputStream the output stream
* @throws IOException if an IO error occurs
*/
private void writeObject(final ObjectOutputStream objectOutputStream) throws IOException {
final ArrayList<L> serializableListeners = new ArrayListOther Java examples (source code examples)Here is a short list of links related to this Java EventListenerSupport.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.