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

com.github.thorbenkuck.keller.event.NativeEventBus Maven / Gradle / Ivy

The newest version!
package com.github.thorbenkuck.keller.event;

import com.github.thorbenkuck.keller.datatypes.interfaces.Value;
import com.github.thorbenkuck.keller.utility.Keller;

import java.util.*;

final class NativeEventBus implements EventBus {

	private final Value dispatcherStrategy = Value.synchronize(new NativeDispatcherStrategy(this));
	private final Value creationStrategy = Value.synchronize(new NativeDispatcherCreationStrategy());
	private final Map, List> mapping = new HashMap<>();

	private void add(final EventBridge eventBridge) {
		synchronized (mapping) {
			mapping.computeIfAbsent(eventBridge.getEventType(), key -> new ArrayList<>());
			mapping.get(eventBridge.getEventType()).add(eventBridge);
		}
	}

	private List copyEventBridges(final Class clazz) {
		final List result = new ArrayList<>();
		synchronized (mapping) {
			result.addAll(mapping.get(clazz));
		}

		return result;
	}

	private List getEventBridges(final Class clazz) {
		final List result;
		synchronized (mapping) {
			result = mapping.get(clazz);
		}

		return result != null ? result : new ArrayList<>();
	}

	private DispatcherStrategy getDispatcherStrategy() {
		return dispatcherStrategy.get();
	}

	private void unregister(final Collection collection) {
		for(final EventBridge eventBridge : collection) {
			final List stored = getEventBridges(eventBridge.getEventType());
			stored.remove(eventBridge);
		}
	}

	private void register(final Collection eventBridgeCollection) {
		for(final EventBridge eventBridge : eventBridgeCollection) {
			add(eventBridge);
		}
	}

	private void dispatchDeadEvent(final Object event) {
		final DeadEvent deadEvent = new DeadEvent(event);
		final List bridges = copyEventBridges(deadEvent.getClass());
		getDispatcherStrategy().dispatchDeadEvent(deadEvent, bridges);
	}

	private boolean tryDispatch(final Object object) {
		Keller.parameterNotNull(object);
		final List bridges = copyEventBridges(object.getClass());
		if(bridges.isEmpty()) {
			return false;
		}
		getDispatcherStrategy().dispatch(object, bridges);
		return true;
	}

	@Override
	public void setDispatcherStrategy(final DispatcherStrategy dispatcherStrategy) {
		this.dispatcherStrategy.set(dispatcherStrategy);
	}

	@Override
	public void setCreationStrategy(final DispatcherCreationStrategy creationStrategy) {
		this.creationStrategy.set(creationStrategy);
	}

	@Override
	public void register(final Object object) {
		register(creationStrategy.get().create(object));
	}

	@Override
	public void unregister(final Object object) {
		unregister(creationStrategy.get().create(object));
	}

	@Override
	public void post(final Object object) {
		if(!tryDispatch(object)) {
			dispatchDeadEvent(object);
		}
	}

	@Override
	public void clear() {
		synchronized (mapping) {
			mapping.clear();
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy