All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.core.event.AbstractEventSupport Maven / Gradle / Ivy

There is a newer version: 9.44.0.Final
Show newest version
/*
 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed 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.drools.core.event;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collections;
import java.util.EventListener;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;

import org.kie.internal.runtime.Closeable;

/**
 * Base class for Thread-safe Event Support in Drools. Note that subclasses wishing to access
 * the listeners should do so via the getEventListenersIterator method. This
 * will provide an Iterator accessing the current snapshot of the underlying list, freeing the
 * subclasss of thread problems.
 * 

* Please note that for lists of small sizes, and few modifications, the CopyOnWriteArrayList * provides best performance. If the list is modified more often, than a simple ArrayList * with synchonized operations, and copying of the array for iteration is faster. */ public abstract class AbstractEventSupport implements Externalizable { private static final long serialVersionUID = 510l; private List listeners = new CopyOnWriteArrayList(); @SuppressWarnings("unchecked") public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { listeners = (List) in.readObject(); } public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(listeners); } public void notifyAllListeners(Consumer consumer) { if (!listeners.isEmpty()) { listeners.forEach( l -> consumer.accept( l ) ); } } protected final Iterator getEventListenersIterator() { return listeners.iterator(); } /** * Adds the specified listener to the list of listeners. Note that this method needs to be * synchonized because it performs two independent operations on the underlying list * * @param listener to add */ public final synchronized void addEventListener(final E listener) { if (!this.listeners.contains(listener)) { this.listeners.add(listener); } } /** * Removes all event listeners of the specified class. Note that this method needs to be * synchonized because it performs two independent operations on the underlying list * * @param cls class of listener to remove */ public final synchronized void removeEventListener(final Class cls) { for (int listenerIndex = 0; listenerIndex < this.listeners.size();) { E listener = this.listeners.get(listenerIndex); if (cls.isAssignableFrom(listener.getClass())) { this.listeners.remove(listenerIndex); } else { listenerIndex++; } } } public final void removeEventListener(final E listener) { this.listeners.remove(listener); } public List getEventListeners() { return Collections.unmodifiableList(this.listeners); } public final int size() { return this.listeners.size(); } public boolean isEmpty() { return this.listeners.isEmpty(); } public void clear() { if (listeners != null) { for (EventListener listener : listeners) { if (listener instanceof Closeable) { ((Closeable) listener).close(); } } } this.listeners.clear(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy