Implement usage of EventPriority in EventBus

This commit is contained in:
Dabo Ross 2013-09-05 01:55:44 -07:00 committed by md_5
parent d3c1acce83
commit 0f06b2c4e0

View File

@ -15,7 +15,7 @@ import java.util.logging.Logger;
public class EventBus
{
private final Map<Class<?>, Map<Object, Method[]>> eventToHandler = new HashMap<>();
private final Map<Class<?>, Map<EventPriority, Map<Object, Method[]>>> eventToHandler = new HashMap<>();
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Logger logger;
@ -34,7 +34,10 @@ public class EventBus
lock.readLock().lock();
try
{
Map<Object, Method[]> handlers = eventToHandler.get( event.getClass() );
Map<EventPriority, Map<Object, Method[]>> handlersByPriority = eventToHandler.get( event.getClass() );
for ( EventPriority value : EventPriority.values() )
{
Map<Object, Method[]> handlers = handlersByPriority.get( value );
if ( handlers != null )
{
for ( Map.Entry<Object, Method[]> handler : handlers.entrySet() )
@ -57,15 +60,16 @@ public class EventBus
}
}
}
}
} finally
{
lock.readLock().unlock();
}
}
private Map<Class<?>, Set<Method>> findHandlers(Object listener)
private Map<Class<?>, Map<EventPriority, Set<Method>>> findHandlers(Object listener)
{
Map<Class<?>, Set<Method>> handler = new HashMap<>();
Map<Class<?>, Map<EventPriority, Set<Method>>> handler = new HashMap<>();
for ( Method m : listener.getClass().getDeclaredMethods() )
{
EventHandler annotation = m.getAnnotation( EventHandler.class );
@ -80,14 +84,19 @@ public class EventBus
} );
continue;
}
Set<Method> existing = handler.get( params[0] );
if ( existing == null )
Map<EventPriority, Set<Method>> prioritiesMap = handler.get( params[0] );
if ( prioritiesMap == null )
{
existing = new HashSet<>();
handler.put( params[0], existing );
prioritiesMap = new HashMap<>();
handler.put( params[0], prioritiesMap );
}
existing.add( m );
Set<Method> priority = prioritiesMap.get( annotation.priority() );
if ( priority == null )
{
priority = new HashSet<>();
prioritiesMap.put( annotation.priority(), priority );
}
priority.add( m );
}
}
return handler;
@ -95,20 +104,29 @@ public class EventBus
public void register(Object listener)
{
Map<Class<?>, Set<Method>> handler = findHandlers( listener );
Map<Class<?>, Map<EventPriority, Set<Method>>> handler = findHandlers( listener );
lock.writeLock().lock();
try
{
for ( Map.Entry<Class<?>, Set<Method>> e : handler.entrySet() )
for ( Map.Entry<Class<?>, Map<EventPriority, Set<Method>>> e : handler.entrySet() )
{
Map<Object, Method[]> a = eventToHandler.get( e.getKey() );
if ( a == null )
Map<EventPriority, Map<Object, Method[]>> prioritiesMap = eventToHandler.get( e.getKey() );
if ( prioritiesMap == null )
{
a = new HashMap<>();
eventToHandler.put( e.getKey(), a );
prioritiesMap = new HashMap<>();
eventToHandler.put( e.getKey(), prioritiesMap );
}
for ( Map.Entry<EventPriority, Set<Method>> entry : e.getValue().entrySet() )
{
Map<Object, Method[]> currentPriorityMap = prioritiesMap.get( entry.getKey() );
if ( currentPriorityMap == null )
{
currentPriorityMap = new HashMap<>();
prioritiesMap.put( entry.getKey(), currentPriorityMap );
}
Method[] baked = new Method[ entry.getValue().size() ];
currentPriorityMap.put( listener, entry.getValue().toArray( baked ) );
}
Method[] baked = new Method[ e.getValue().size() ];
a.put( listener, e.getValue().toArray( baked ) );
}
} finally
{
@ -118,17 +136,28 @@ public class EventBus
public void unregister(Object listener)
{
Map<Class<?>, Set<Method>> handler = findHandlers( listener );
Map<Class<?>, Map<EventPriority, Set<Method>>> handler = findHandlers( listener );
lock.writeLock().lock();
try
{
for ( Map.Entry<Class<?>, Set<Method>> e : handler.entrySet() )
for ( Map.Entry<Class<?>, Map<EventPriority, Set<Method>>> e : handler.entrySet() )
{
Map<Object, Method[]> a = eventToHandler.get( e.getKey() );
if ( a != null )
Map<EventPriority, Map<Object, Method[]>> prioritiesMap = eventToHandler.get( e.getKey() );
if ( prioritiesMap != null )
{
a.remove( listener );
if ( a.isEmpty() )
for ( EventPriority priority : e.getValue().keySet() )
{
Map<Object, Method[]> currentPriority = prioritiesMap.get( priority );
if ( currentPriority != null )
{
currentPriority.remove( listener );
if ( currentPriority.isEmpty() )
{
prioritiesMap.remove( priority );
}
}
}
if ( prioritiesMap.isEmpty() )
{
eventToHandler.remove( e.getKey() );
}