com.github.events1000.api.Events Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of events1000 Show documentation
Show all versions of events1000 Show documentation
A Lightweight Event System.
The newest version!
package com.github.events1000.api;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.events1000.emitter.api.AsynchronousEventEmitter;
import com.github.events1000.emitter.api.SimpleSynchronousEventEmitter;
import com.github.events1000.emitter.api.SingleThreadAsynchronousEventEmitter;
import com.github.events1000.emitter.api.SynchronousEventEmitter;
import com.github.events1000.listener.api.AsynchronousEventListener;
import com.github.events1000.listener.api.EventListener;
import com.github.events1000.listener.api.SynchronousEventListener;
public class Events {
private static final Logger logger = LoggerFactory.getLogger(Events.class);
public static final int DEFAULT_HISTORY_SIZE = 1000;
private static class InstanceHolder {
private static final Events instance = new Events();
}
public static Events getInstance() {
return InstanceHolder.instance;
}
private final SynchronousEventEmitter synchronousEventEmitter;
private final AsynchronousEventEmitter asynchronousEventEmitter;
private final ArrayList history;
private final int historySize;
private Events() {
synchronousEventEmitter = new SimpleSynchronousEventEmitter();
asynchronousEventEmitter = new SingleThreadAsynchronousEventEmitter();
historySize = DEFAULT_HISTORY_SIZE;
history = new ArrayList<>(historySize);
}
public synchronized void emit(final SynchronousEvent event) {
synchronousEventEmitter.emit(event);
trimHistory();
history.add(event);
}
public synchronized void emit(final AsynchronousEvent event) {
asynchronousEventEmitter.emit(event);
trimHistory();
history.add(event);
// if(logger.isDebugEnabled()) {
// logger.debug(event + " fired");
// }
}
public synchronized void emit(final Event event) {
if (event instanceof SynchronousEvent) {
emit((SynchronousEvent) event);
} else if (event instanceof AsynchronousEvent) {
emit((AsynchronousEvent) event);
} else if (logger.isDebugEnabled()) {
logger.debug("Unsupported event type " + event.getClass());
}
}
private void trimHistory() {
if (history.size() >= historySize) {
history.subList(historySize - 1, history.size()).clear();
}
}
public synchronized void registerListener(final EventTopic topic, final EventListener listener) {
if (listener instanceof SynchronousEventListener) {
registerListener(topic, (SynchronousEventListener) listener);
} else if (listener instanceof AsynchronousEventListener) {
registerListener(topic, (AsynchronousEventListener) listener);
}
}
synchronized void registerListener(final EventTopic topic, final SynchronousEventListener listener) {
synchronousEventEmitter.registerEventListener(topic, listener);
}
synchronized void registerListener(final EventTopic topic, final AsynchronousEventListener listener) {
asynchronousEventEmitter.registerEventListener(topic, listener);
}
public synchronized void unregisterListener(final EventListener listener) {
if (listener instanceof SynchronousEventListener) {
unregisterListener((SynchronousEventListener) listener);
} else if (listener instanceof AsynchronousEventListener) {
unregisterListener((AsynchronousEventListener) listener);
}
}
synchronized void unregisterListener(final SynchronousEventListener listener) {
synchronousEventEmitter.unregisterEventListener(listener);
}
synchronized void unregisterListener(final AsynchronousEventListener listener) {
asynchronousEventEmitter.unregisterEventListener(listener);
}
public synchronized List getHistory() {
return history;
}
public void stop() {
synchronousEventEmitter.stop();
asynchronousEventEmitter.stop();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy