mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-10-12 09:56:33 +02:00
Auf Gradle Struktur angepasst
This commit is contained in:
58
src/main/java/schule/ngb/zm/events/EventDispatcher.java
Normal file
58
src/main/java/schule/ngb/zm/events/EventDispatcher.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package schule.ngb.zm.events;
|
||||
|
||||
import schule.ngb.zm.util.Validator;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class EventDispatcher<E, L extends Listener<E>> {
|
||||
|
||||
private CopyOnWriteArraySet<L> listeners;
|
||||
|
||||
private ConcurrentMap<String, BiConsumer<E, L>> eventRegistry;
|
||||
|
||||
public EventDispatcher() {
|
||||
listeners = new CopyOnWriteArraySet<>();
|
||||
eventRegistry = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
public void registerEventType( String eventKey, BiConsumer<E, L> dispatcher ) {
|
||||
Validator.requireNotNull(eventKey);
|
||||
Validator.requireNotNull(dispatcher);
|
||||
|
||||
if( !eventRegistered(eventKey) ) {
|
||||
eventRegistry.put(eventKey, dispatcher);
|
||||
}
|
||||
}
|
||||
|
||||
public void addListener( L listener ) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeListener( L listener ) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
public boolean hasListeners() {
|
||||
return !listeners.isEmpty();
|
||||
}
|
||||
|
||||
public boolean eventRegistered( String eventKey ) {
|
||||
return eventRegistry.containsKey(eventKey);
|
||||
}
|
||||
|
||||
public void dispatchEvent( String eventKey, final E event ) {
|
||||
Validator.requireNotNull(eventKey);
|
||||
Validator.requireNotNull(event);
|
||||
|
||||
if( eventRegistered(eventKey) ) {
|
||||
final BiConsumer<E, L> dispatcher = eventRegistry.get(eventKey);
|
||||
listeners.forEach(( listener ) -> {
|
||||
dispatcher.accept(event, listener);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
7
src/main/java/schule/ngb/zm/events/Listener.java
Normal file
7
src/main/java/schule/ngb/zm/events/Listener.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package schule.ngb.zm.events;
|
||||
|
||||
public interface Listener<E> {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -66,6 +66,10 @@ public class Sound implements Audio {
|
||||
this.audioSource = source;
|
||||
}
|
||||
|
||||
public String getSource() {
|
||||
return audioSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user