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

com.king.platform.net.http.netty.eventbus.DefaultEventBus Maven / Gradle / Ivy

There is a newer version: 3.0.26
Show newest version
// Copyright (C) king.com Ltd 2015
// https://github.com/king/king-http-client
// Author: Magnus Gustafsson
// License: Apache 2.0, https://raw.github.com/king/king-http-client/LICENSE-APACHE

package com.king.platform.net.http.netty.eventbus;

import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static org.slf4j.LoggerFactory.getLogger;

public class DefaultEventBus implements RequestEventBus, RootEventBus {
	private final Logger logger = getLogger(getClass());

	private final ConcurrentHashMap> event1Callbacks;
	private final ConcurrentHashMap> event2Callbacks;

	private final ConcurrentHashMap> persistentEvent1Callbacks;
	private final ConcurrentHashMap> persistentEvent2Callbacks;


	public DefaultEventBus() {
		event1Callbacks = new ConcurrentHashMap<>();
		event2Callbacks = new ConcurrentHashMap<>();

		persistentEvent1Callbacks = new ConcurrentHashMap<>();
		persistentEvent2Callbacks = new ConcurrentHashMap<>();
	}


	@Override
	public  void subscribe(Event1 event, EventBusCallback1 callback) {
		subscribe(event1Callbacks, event, callback);
	}


	@Override
	public  void subscribe(Event2 event, EventBusCallback2 callback) {
		subscribe(event2Callbacks, event, callback);
	}


	@Override
	public  void subscribePermanently(Event1 event, EventBusCallback1 callback) {
		subscribe(persistentEvent1Callbacks, event, callback);
	}

	@Override
	public  void subscribePermanently(Event2 event, EventBusCallback2 callback) {
		subscribe(persistentEvent2Callbacks, event, callback);
	}


	private void subscribe(ConcurrentHashMap> map, Event event, EventBusCallback callback) {
		ArrayList eventList = map.get(event);

		if (eventList == null) {
			eventList = new ArrayList<>();
			ArrayList oldValue = map.putIfAbsent(event, eventList);
			if (oldValue != null) {
				eventList = oldValue;
			}
		}
		eventList.add(callback);
	}


	@Override
	public void triggerEvent(Event1 event) {
		triggerEvent(event, null);

	}

	@Override
	public  void triggerEvent(Event1 event, T payload) {
		triggerEvent1(event, payload, persistentEvent1Callbacks);
		triggerEvent1(event, payload, event1Callbacks);
	}


	@Override
	public  void triggerEvent(Event2 event, T1 payload1, T2 payload2) {
		triggerEvent2(event, payload1, payload2, persistentEvent2Callbacks);
		triggerEvent2(event, payload1, payload2, event2Callbacks);
	}


	private  void triggerEvent1(Event1 event, T payload, ConcurrentHashMap> callbacks) {
		ArrayList eventBusCallback1s = callbacks.get(event);

		if (eventBusCallback1s == null) {
			return;
		}

		for (EventBusCallback eventBusCallback : eventBusCallback1s) {
			EventBusCallback1 callback = (EventBusCallback1) eventBusCallback;
			callback.onEvent(payload);
		}
	}

	private  void triggerEvent2(Event2 event, T1 payload1, T2 payload2, ConcurrentHashMap> callbacks) {
		ArrayList eventBusCallback2s = callbacks.get(event);

		if (eventBusCallback2s == null) {
			return;
		}

		for (EventBusCallback eventBusCallback : eventBusCallback2s) {
			EventBusCallback2 callback = (EventBusCallback2) eventBusCallback;
			callback.onEvent(payload1, payload2);
		}
	}


	@Override
	public RequestEventBus createRequestEventBus() {
		DefaultEventBus cleanEventBus = new DefaultEventBus();

		for (Map.Entry> entry : persistentEvent1Callbacks.entrySet()) {
			for (EventBusCallback eventBusCallback : entry.getValue()) {
				cleanEventBus.subscribePermanently((Event1) entry.getKey(), (EventBusCallback1) eventBusCallback);
			}
		}

		for (Map.Entry> entry : persistentEvent2Callbacks.entrySet()) {
			for (EventBusCallback eventBusCallback : entry.getValue()) {
				cleanEventBus.subscribePermanently((Event2) entry.getKey(), (EventBusCallback2) eventBusCallback);
			}
		}

		return cleanEventBus;
	}


	ConcurrentHashMap> getEvent1Callbacks() {
		return event1Callbacks;
	}

	ConcurrentHashMap> getEvent2Callbacks() {
		return event2Callbacks;
	}

	ConcurrentHashMap> getPersistentEvent1Callbacks() {
		return persistentEvent1Callbacks;
	}

	ConcurrentHashMap> getPersistentEvent2Callbacks() {
		return persistentEvent2Callbacks;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy