org.oscim.event.EventDispatcher Maven / Gradle / Ivy
/*
* Copyright 2013 Hannes Janetzek
* Copyright 2016 Andrey Novikov
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see .
*/
package org.oscim.event;
import org.oscim.utils.pool.LList;
/**
* The Class EventDispatcher.
*
* Events MUST be dispatched from main-loop! To add events from other
* threads use:
* Map.post(new Runnable(){ public void run(tell(event,data);)};);
*
* @param the event source type
* @param the event 'data' type
*/
public abstract class EventDispatcher {
/**
* The list of listeners.
*/
protected LList mListeners;
/**
* Bind listener for event notifications.
*/
public void bind(E listener) {
if (LList.find(mListeners, listener) != null) {
return;
}
mListeners = LList.push(mListeners, listener);
}
/**
* Remove listener.
*/
public void unbind(E listener) {
mListeners = LList.remove(mListeners, listener);
}
/**
* Tell listeners whats going on.
*
* @param event the event
* @param data the data
*/
public abstract void tell(E listener, Event event, T data);
public void fire(Event event, T data) {
for (LList l = mListeners; l != null; l = l.next) {
tell(l.data, event, data);
}
}
}