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

jadex.rules.state.javaimpl.OAVEventHandler Maven / Gradle / Ivy

package jadex.rules.state.javaimpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import jadex.commons.collection.IdentityHashSet;
import jadex.rules.state.IOAVState;
import jadex.rules.state.IOAVStateListener;
import jadex.rules.state.OAVAttributeType;
import jadex.rules.state.OAVObjectType;


/**
 *  This class handles the collection and distribution OAV
 *  events to registered listeners.
 */
public class OAVEventHandler
{
	//-------- attributes --------
	
	/** The state. */
	protected IOAVState state;
	
	/** The bunch state listeners. */
	protected List listeners;
	
	/** The direct state listeners. */
	protected List directlisteners;
	
	/** The collected change events. */
	protected Set oavevents;

	/** The collected bean events (may be added from external thread). */
	protected Set beanevents;

	/** The objects that have been removed in current change set. */
	protected Set removed_objects;
	
	/** The objects that have been added in current change set. */
	protected Set added_objects;
	
	/** Flag that is only true, while listeners are being notified. */
	protected boolean	notifying;

	//-------- constructors --------
	
	/**
	 *  Create a new OAV event handler.
	 */
	public OAVEventHandler(IOAVState state)
	{
		this.state	= state;
		this.listeners	= new ArrayList();
		this.directlisteners = new ArrayList();
		this.oavevents = new LinkedHashSet();
//		this.oavevents = new CheckedCollection(new LinkedHashSet());
		this.beanevents = Collections.synchronizedSet(new LinkedHashSet());
//		this.beanevents = Collections.synchronizedSet(new CheckedCollection(new LinkedHashSet()));
	}
	
	//-------- state observers --------
	
	/**
	 *  Add a new state listener.
	 *  @param listener The state listener.
	 */
	public void addStateListener(IOAVStateListener listener, boolean bunch)
	{
		if(bunch)
			this.listeners.add(listener);
		else
			this.directlisteners.add(listener);
	}
	
	/**
	 *  Remove a state listener.
	 *  @param listener The state listener.
	 */
	public void removeStateListener(IOAVStateListener listener)
	{
		if(!this.listeners.remove(listener))
			if(!this.directlisteners.remove(listener))
				throw new RuntimeException("Listener not found: "+listener);
	}
	
	/**
	 *  Throw collected events and notify the listeners.
	 */
	public void notifyEventListeners()
	{
		this.notifying	= true;
		if(!beanevents.isEmpty())
		{
			Object[]	abeanevents;
			synchronized(beanevents)
			{
				abeanevents	= beanevents.toArray();
				beanevents.clear();
			}
			for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy