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

com.github.linushp.commons.ebus.EventBus Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package com.github.linushp.commons.ebus;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


/**
 * 子类实现单例即可
 */
public class EventBus {

    private static final Logger LOGGER = LoggerFactory.getLogger(EventBus.class);

    protected List eventListenerList = new ArrayList<>();


    public void addEventListener(String name, EventListener eventListener) {
        this.removeEventListener(name);
        this.eventListenerList.add(new EventListenerWrapper(name, eventListener));
    }


    public void removeEventListener(String name) {
        List eventListenerList = new ArrayList<>(this.eventListenerList);
        for (EventListenerWrapper eventListenerWrapper : eventListenerList) {
            if (name.equals(eventListenerWrapper.name)) {
                this.eventListenerList.remove(eventListenerWrapper);
            }
        }
    }


    public void emit(Event event) {
        for (EventListenerWrapper eventListenerWrapper : eventListenerList) {
            try {
                EventListener eventListener = eventListenerWrapper.eventListener;
                eventListener.onEvent(event);
            } catch (Exception e) {
                LOGGER.error("", e);
            }
        }
    }


    private static class EventListenerWrapper {
        public String name;
        public EventListener eventListener;

        public EventListenerWrapper(String name, EventListener eventListener) {
            this.name = name;
            this.eventListener = eventListener;
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy