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

io.gravitee.common.event.impl.EventManagerImpl Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.common.event.impl;

import io.gravitee.common.event.Event;
import io.gravitee.common.event.EventListener;
import io.gravitee.common.event.EventManager;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author David BRASSELY (brasseld at gmail.com)
 */
public class EventManagerImpl implements EventManager {

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

    private Map> listenersMap = new TreeMap();

    public void publishEvent(Enum type, Object content) {
        this.publishEvent(new SimpleEvent(type, content));
    }

    public void publishEvent(Event event) {
        LOGGER.debug("Publish event {} - {}", event.type(), event.content());

        List listeners = getEventListeners(event.type().getClass());
        for (EventListenerWrapper listener : listeners) {
            listener.eventListener().onEvent(event);
        }
    }

    public  void subscribeForEvents(EventListener eventListener, T... events) {
        for (T event : events) {
            addEventListener(eventListener, (Class) event.getClass(), Arrays.asList(events));
        }
    }

    public  void subscribeForEvents(EventListener eventListener, Class events) {
        addEventListener(eventListener, events, EnumSet.allOf(events));
    }

    private  void addEventListener(EventListener eventListener, Class enumClass, Collection events) {
        LOGGER.info("Register new listener {} for event type {}", eventListener.getClass().getSimpleName(), enumClass);

        List listeners = getEventListeners(enumClass);
        listeners.add(new EventListenerWrapper(eventListener, events));
    }

    private  List getEventListeners(Class eventType) {
        List listeners = this.listenersMap.get(new ComparableEventType(eventType));

        if (listeners == null) {
            listeners = new ArrayList<>();
            this.listenersMap.put(new ComparableEventType(eventType), listeners);
        }

        return listeners;
    }

    private class EventListenerWrapper {

        private final EventListener eventListener;
        private final Set events;

        public EventListenerWrapper(EventListener eventListener, Collection events) {
            this.eventListener = eventListener;
            this.events = new HashSet(events);
        }

        public EventListener eventListener() {
            return eventListener;
        }

        public Set events() {
            return events;
        }
    }

    private class ComparableEventType implements Comparable> {

        private static final int HASH = 7 * 89;
        private final Class wrappedClass;

        public ComparableEventType(Class wrappedClass) {
            this.wrappedClass = wrappedClass;
        }

        @Override
        public int compareTo(ComparableEventType o) {
            return wrappedClass.getCanonicalName().compareTo(o.wrappedClass.getCanonicalName());
        }

        @Override
        public int hashCode() {
            return HASH + (this.wrappedClass != null ? this.wrappedClass.hashCode() : 0);
        }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof ComparableEventType)) {
                return false;
            }

            return compareTo((ComparableEventType) o) == 0;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy