Added event bus implementation.

Dieser Commit ist enthalten in:
sk89q 2014-04-02 18:53:06 -07:00
Ursprung b78b086f2e
Commit a7d83958ac
6 geänderte Dateien mit 601 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,69 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.eventbus;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import java.lang.reflect.Method;
/**
* A {@link SubscriberFindingStrategy} for collecting all event handler methods
* that are marked with the {@link Subscribe} annotation.
* </p>
* Original for Guava, licensed under the Apache License, Version 2.0.
*/
class AnnotatedSubscriberFinder implements SubscriberFindingStrategy {
/**
* {@inheritDoc}
*
* This implementation finds all methods marked with a {@link Subscribe}
* annotation.
*/
@Override
public Multimap<Class<?>, EventHandler> findAllSubscribers(Object listener) {
Multimap<Class<?>, EventHandler> methodsInListener = HashMultimap.create();
Class<?> clazz = listener.getClass();
while (clazz != null) {
for (Method method : clazz.getMethods()) {
Subscribe annotation = method.getAnnotation(Subscribe.class);
method.setAccessible(true);
if (annotation != null) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length != 1) {
throw new IllegalArgumentException(
"Method " + method + " has @Subscribe annotation, but requires " +
parameterTypes.length + " arguments. Event handler methods " +
"must require a single argument.");
}
Class<?> eventType = parameterTypes[0];
EventHandler handler = new MethodEventHandler(annotation.priority(), listener, method);
methodsInListener.put(eventType, handler);
}
}
clazz = clazz.getSuperclass();
}
return methodsInListener;
}
}

Datei anzeigen

@ -0,0 +1,261 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.eventbus;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.*;
import com.google.common.eventbus.DeadEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Dispatches events to listeners, and provides ways for listeners to register
* themselves.
* </p>
* This class is based on Guava's {@link EventBus} but priority is supported
* and events are dispatched at the time of call, rather than being queued up.
* This does allow dispatching during an in-progress dispatch.
* </p>
* This implementation utilizes naive synchronization on all getter and setter
* methods. Dispatch does not occur when a lock has been acquired, however.
*/
public class EventBus {
private final Logger logger = Logger.getLogger(EventBus.class.getCanonicalName());
private final SetMultimap<Class<?>, EventHandler> handlersByType =
Multimaps.newSetMultimap(new HashMap<Class<?>, Collection<EventHandler>>(),
new Supplier<Set<EventHandler>>() {
@Override
public Set<EventHandler> get() {
return newHandlerSet();
}
});
/**
* Strategy for finding handler methods in registered objects. Currently,
* only the {@link AnnotatedSubscriberFinder} is supported, but this is
* encapsulated for future expansion.
*/
private final SubscriberFindingStrategy finder = new AnnotatedSubscriberFinder();
/**
* A thread-safe cache for flattenHierarchy(). The Class class is immutable.
*/
private Cache<Class<?>, Set<Class<?>>> flattenHierarchyCache =
CacheBuilder.newBuilder()
.weakKeys()
.build(new CacheLoader<Class<?>, Set<Class<?>>>() {
@Override
public Set<Class<?>> load(Class<?> concreteClass) throws Exception {
List<Class<?>> parents = Lists.newLinkedList();
Set<Class<?>> classes = Sets.newHashSet();
parents.add(concreteClass);
while (!parents.isEmpty()) {
Class<?> clazz = parents.remove(0);
classes.add(clazz);
Class<?> parent = clazz.getSuperclass();
if (parent != null) {
parents.add(parent);
}
Collections.addAll(parents, clazz.getInterfaces());
}
return classes;
}
});
/**
* Registers the given handler for the given class to receive events.
*
* @param clazz the event class to register
* @param handler the handler to register
*/
public synchronized void subscribe(Class<?> clazz, EventHandler handler) {
checkNotNull(clazz);
checkNotNull(handler);
handlersByType.put(clazz, handler);
}
/**
* Registers the given handler for the given class to receive events.
*
* @param handlers a map of handlers
*/
public synchronized void subscribeAll(Multimap<Class<?>, EventHandler> handlers) {
checkNotNull(handlers);
handlersByType.putAll(handlers);
}
/**
* Unregisters the given handler for the given class.
*
* @param clazz the class
* @param handler the handler
*/
public synchronized void unsubscribe(Class<?> clazz, EventHandler handler) {
checkNotNull(clazz);
checkNotNull(handler);
handlersByType.remove(clazz, handler);
}
/**
* Unregisters the given handlers.
*
* @param handlers a map of handlers
*/
public synchronized void unsubscribeAll(Multimap<Class<?>, EventHandler> handlers) {
checkNotNull(handlers);
for (Map.Entry<Class<?>, Collection<EventHandler>> entry : handlers.asMap().entrySet()) {
Set<EventHandler> currentHandlers = getHandlersForEventType(entry.getKey());
Collection<EventHandler> eventMethodsInListener = entry.getValue();
if (currentHandlers != null &&!currentHandlers.containsAll(entry.getValue())) {
currentHandlers.removeAll(eventMethodsInListener);
}
}
}
/**
* Registers all handler methods on {@code object} to receive events.
* Handler methods are selected and classified using this EventBus's
* {@link SubscriberFindingStrategy}; the default strategy is the
* {@link AnnotatedSubscriberFinder}.
*
* @param object object whose handler methods should be registered.
*/
public void register(Object object) {
subscribeAll(finder.findAllSubscribers(object));
}
/**
* Unregisters all handler methods on a registered {@code object}.
*
* @param object object whose handler methods should be unregistered.
* @throws IllegalArgumentException if the object was not previously registered.
*/
public void unregister(Object object) {
unsubscribeAll(finder.findAllSubscribers(object));
}
/**
* Posts an event to all registered handlers. This method will return
* successfully after the event has been posted to all handlers, and
* regardless of any exceptions thrown by handlers.
*
* <p>If no handlers have been subscribed for {@code event}'s class, and
* {@code event} is not already a {@link DeadEvent}, it will be wrapped in a
* DeadEvent and reposted.
*
* @param event event to post.
*/
public void post(Object event) {
List<EventHandler> dispatching = new ArrayList<EventHandler>();
synchronized (this) {
Set<Class<?>> dispatchTypes = flattenHierarchy(event.getClass());
for (Class<?> eventType : dispatchTypes) {
Set<EventHandler> wrappers = getHandlersForEventType(eventType);
if (wrappers != null && !wrappers.isEmpty()) {
dispatching.addAll(wrappers);
}
}
}
Collections.sort(dispatching);
for (EventHandler handler : dispatching) {
dispatch(event, handler);
}
}
/**
* Dispatches {@code event} to the handler in {@code handler}. This method
* is an appropriate override point for subclasses that wish to make
* event delivery asynchronous.
*
* @param event event to dispatch.
* @param handler handler that will call the handler.
*/
protected void dispatch(Object event, EventHandler handler) {
try {
handler.handleEvent(event);
} catch (InvocationTargetException e) {
logger.log(Level.SEVERE,
"Could not dispatch event: " + event + " to handler " + handler, e);
}
}
/**
* Retrieves a mutable set of the currently registered handlers for
* {@code type}. If no handlers are currently registered for {@code type},
* this method may either return {@code null} or an empty set.
*
* @param type type of handlers to retrieve.
* @return currently registered handlers, or {@code null}.
*/
synchronized Set<EventHandler> getHandlersForEventType(Class<?> type) {
return handlersByType.get(type);
}
/**
* Creates a new Set for insertion into the handler map. This is provided
* as an override point for subclasses. The returned set should support
* concurrent access.
*
* @return a new, mutable set for handlers.
*/
protected synchronized Set<EventHandler> newHandlerSet() {
return new HashSet<EventHandler>();
}
/**
* Flattens a class's type hierarchy into a set of Class objects. The set
* will include all superclasses (transitively), and all interfaces
* implemented by these superclasses.
*
* @param concreteClass class whose type hierarchy will be retrieved.
* @return {@code clazz}'s complete type hierarchy, flattened and uniqued.
*/
Set<Class<?>> flattenHierarchy(Class<?> concreteClass) {
try {
return flattenHierarchyCache.get(concreteClass);
} catch (ExecutionException e) {
throw Throwables.propagate(e.getCause());
}
}
}

Datei anzeigen

@ -0,0 +1,105 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.eventbus;
import java.lang.reflect.InvocationTargetException;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Event handler object for {@link EventBus} that is able to dispatch
* an event.
* </p>
* Original for Guava, licensed under the Apache License, Version 2.0.
*/
public abstract class EventHandler implements Comparable<EventHandler> {
public enum Priority {
VERY_EARLY,
EARLY,
NORMAL,
LATE,
VERY_LATE
}
private final Priority priority;
/**
* Create a new event handler.
*
* @param priority the priority
*/
protected EventHandler(Priority priority) {
checkNotNull(priority);
this.priority = priority;
}
/**
* Get the priority.
*
* @return the priority
*/
public Priority getPriority() {
return priority;
}
/**
* Dispatch the given event.
* </p>
* Subclasses should override {@link #dispatch(Object)}.
*
* @param event the event
* @throws InvocationTargetException thrown if an exception is thrown during dispatch
*/
public final void handleEvent(Object event) throws InvocationTargetException {
try {
dispatch(event);
} catch (Throwable t) {
throw new InvocationTargetException(t);
}
}
/**
* Dispatch the event.
*
* @param event the event object
* @throws Exception an exception that may be thrown
*/
public abstract void dispatch(Object event) throws Exception;
@Override
public int compareTo(EventHandler o) {
return getPriority().ordinal() - o.getPriority().ordinal();
}
@Override
public abstract int hashCode();
@Override
public abstract boolean equals(Object obj);
@Override
public String toString() {
return "EventHandler{" +
"priority=" + priority +
'}';
}
}

Datei anzeigen

@ -0,0 +1,81 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.eventbus;
import java.lang.reflect.Method;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Invokes a {@link Method} to dispatch an event.
*/
public class MethodEventHandler extends EventHandler {
private final Object object;
private final Method method;
/**
* Create a new event handler.
*
* @param priority the priority
* @param method the method
*/
public MethodEventHandler(Priority priority, Object object, Method method) {
super(priority);
checkNotNull(method);
this.object = object;
this.method = method;
}
/**
* Get the method.
*
* @return the method
*/
public Method getMethod() {
return method;
}
@Override
public void dispatch(Object event) throws Exception {
method.invoke(object, event);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MethodEventHandler that = (MethodEventHandler) o;
if (!method.equals(that.method)) return false;
if (object != null ? !object.equals(that.object) : that.object != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = object != null ? object.hashCode() : 0;
result = 31 * result + method.hashCode();
return result;
}
}

Datei anzeigen

@ -0,0 +1,42 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.eventbus;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
/**
* Used to mark methods as event handlers.
*/
@Retention(RUNTIME)
@Target({METHOD})
public @interface Subscribe {
/**
* The priority as far as order of dispatching is concerned.
*
* @return the priority
*/
EventHandler.Priority priority() default EventHandler.Priority.NORMAL;
}

Datei anzeigen

@ -0,0 +1,43 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.eventbus;
import com.google.common.collect.Multimap;
/**
* A method for finding event handler methods in objects, for use by
* {@link EventBus}.
*/
interface SubscriberFindingStrategy {
/**
* Finds all suitable event handler methods in {@code source}, organizes them
* by the type of event they handle, and wraps them in {@link EventHandler}s.
*
* @param source object whose handlers are desired.
* @return EventHandler objects for each handler method, organized by event
* type.
*
* @throws IllegalArgumentException if {@code source} is not appropriate for
* this strategy (in ways that this interface does not define).
*/
Multimap<Class<?>, EventHandler> findAllSubscribers(Object source);
}