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

jasima.core.util.observer.NotifierAdapter Maven / Gradle / Ivy

Go to download

JAva SImulatior for MAnufacturing and logistics - A framework for discrete event simulation and computer experiments with a main focus on modelling and analyzing logistic/manufacturing systems.

There is a newer version: 1.3.0
Show newest version
/*******************************************************************************
 * Copyright (c) 2010-2013 Torsten Hildebrandt and jasima contributors
 *
 * This file is part of jasima, v1.0.
 *
 * jasima is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jasima is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jasima.  If not, see .
 *
 * $Id: NotifierAdapter.java 74 2013-01-08 17:31:49Z [email protected] $
 *******************************************************************************/
package jasima.core.util.observer;

import java.io.Serializable;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;

/**
 * Implementation of a {@link Notifier} functionality. A NotifierAdapter handles
 * notifier functionality for some real Notifier.
 * 
 * @author Torsten Hildebrandt 
 * @version "$Id: NotifierAdapter.java 74 2013-01-08 17:31:49Z [email protected] $"
 */
public class NotifierAdapter, E> implements
		Notifier, Serializable {

	private static final long serialVersionUID = 72063783838585993L;

	private ArrayList> listeners = new ArrayList>();
	private ArrayDeque events = null;
	private final N notifier;

	private Iterator> it;

	public NotifierAdapter(final N notifier) {
		if (notifier == null)
			throw new NullPointerException("notifier");
		this.notifier = notifier;
	}

	public void addNotifierListener(NotifierListener listener) {
		if (listener == null)
			throw new NullPointerException("listener");
		if (it != null) // addNotifierListener() called while firing()
			throw new ConcurrentModificationException();
		listeners.add(listener);
	}

	public void removeNotifierListener(NotifierListener listener) {
		if (listener == null)
			throw new NullPointerException("listener");
		if (it != null) {
			int oldPos = 0;
			assert (oldPos = listeners.indexOf(listener)) >= 0;
			it.remove();
			assert (oldPos != listeners.indexOf(listener));
		} else
			listeners.remove(listener);
	}

	public void fire(E event) {
		if (it != null) {
			// already firing, i.e., listener triggered another event
			if (events == null)
				events = new ArrayDeque();
			events.addLast(event);
		} else {
			do {
				it = listeners.iterator();
				while (it.hasNext()) {
					it.next().update(this.notifier, event);
				}
				it = null;

				event = null;
				if (events != null && events.size() > 0)
					event = events.removeFirst();
			} while (event != null);
		}
	}

	public int numListener() {
		return listeners.size();
	}

	@Override
	public NotifierListener getNotifierListener(int index) {
		return listeners.get(index);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy