wcloud/wcloud-driver/src/main/java/de/wcloudservice/driver/event/EventDriver.java
2026-11-24 19:16:50 +01:00

136 lines
4.4 KiB
Java

/*
* Copyright 2025 wCloud team & contributors
*
* 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 de.wcloudservice.driver.event;
import de.wcloudservice.driver.event.annotations.Subscribe;
import de.wcloudservice.driver.event.interfaces.CloudListener;
import de.wcloudservice.driver.event.interfaces.EventAdapter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class EventDriver {
public static final int PRE = -1;
public static final int ALL = 0;
public static final int POST = 1;
private final Map<Class<? extends EventAdapter>, Collection<EventProcessor>> bindings =
new ConcurrentHashMap<>();
private final Set<CloudListener> registeredListeners = ConcurrentHashMap.newKeySet();
public List<EventProcessor> getListenersFor(Class<? extends EventAdapter> clazz) {
var handlers = this.bindings.get(clazz);
return handlers == null ? new ArrayList<>() : new ArrayList<>(handlers);
}
public <T extends EventAdapter> T executeEvent(T event, int i) {
Collection<EventProcessor> handlers = this.bindings.get(event.getClass());
if (handlers == null) {
return event;
}
for (EventProcessor handler : handlers) {
if (i == PRE && handler.priority() >= 0) {
continue;
}
if (i == POST && handler.priority() < 0) {
continue;
}
handler.execute(event);
}
return event;
}
public <T extends EventAdapter> T executeEvent(T event) {
return this.executeEvent(event, ALL);
}
public synchronized void registerListener(final CloudListener listener) {
if (!this.registeredListeners.add(listener)) {
return;
}
Method[] methods = listener.getClass().getDeclaredMethods();
for (final Method method : methods) {
Subscribe annotation = method.getAnnotation(Subscribe.class);
if (annotation == null) {
continue;
}
Class<?>[] parameters = method.getParameterTypes();
if (parameters.length != 1) {
continue;
}
Class<?> param = parameters[0];
if (!method.getReturnType().equals(void.class)) {
continue;
}
if (!EventAdapter.class.isAssignableFrom(param)) {
return;
}
Class<? extends EventAdapter> realParam = param.asSubclass(EventAdapter.class);
var eventHandlersForEvent =
this.bindings.computeIfAbsent(
realParam,
ignored -> new java.util.concurrent.ConcurrentSkipListSet<>());
eventHandlersForEvent.add(createEventHandler(listener, method, annotation));
}
}
private EventProcessor createEventHandler(
final CloudListener listener, final Method method, final Subscribe annotation) {
return new EventProcessor(listener, method, annotation);
}
public synchronized void clearListeners() {
this.bindings.clear();
this.registeredListeners.clear();
}
public synchronized void removeListener(CloudListener listener) {
for (Map.Entry<Class<? extends EventAdapter>, Collection<EventProcessor>> entry :
this.bindings.entrySet()) {
entry.getValue().removeIf(curr -> curr.listener() == listener);
}
this.registeredListeners.remove(listener);
}
public Map<Class<? extends EventAdapter>, Collection<EventProcessor>> getBindings() {
return new HashMap<>(this.bindings);
}
public Set<CloudListener> getRegisteredListeners() {
return new HashSet<>(this.registeredListeners);
}
}