Use fluid bytes instead of enum + format.

This commit is contained in:
md_5 2013-09-09 14:36:48 +10:00
parent e3a7490bcd
commit 1342baed47
5 changed files with 24 additions and 62 deletions

View File

@ -17,7 +17,7 @@ import java.util.logging.Logger;
public class EventBus public class EventBus
{ {
private final Map<Class<?>, Map<EventPriority, Map<Object, Method[]>>> byListenerAndPriority = new HashMap<>(); private final Map<Class<?>, Map<Byte, Map<Object, Method[]>>> byListenerAndPriority = new HashMap<>();
private final Map<Class<?>, EventHandlerMethod[]> byEventBaked = new HashMap<>(); private final Map<Class<?>, EventHandlerMethod[]> byEventBaked = new HashMap<>();
private final ReadWriteLock lock = new ReentrantReadWriteLock(); private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Logger logger; private final Logger logger;
@ -63,9 +63,9 @@ public class EventBus
} }
} }
private Map<Class<?>, Map<EventPriority, Set<Method>>> findHandlers(Object listener) private Map<Class<?>, Map<Byte, Set<Method>>> findHandlers(Object listener)
{ {
Map<Class<?>, Map<EventPriority, Set<Method>>> handler = new HashMap<>(); Map<Class<?>, Map<Byte, Set<Method>>> handler = new HashMap<>();
for ( Method m : listener.getClass().getDeclaredMethods() ) for ( Method m : listener.getClass().getDeclaredMethods() )
{ {
EventHandler annotation = m.getAnnotation( EventHandler.class ); EventHandler annotation = m.getAnnotation( EventHandler.class );
@ -80,7 +80,7 @@ public class EventBus
} ); } );
continue; continue;
} }
Map<EventPriority, Set<Method>> prioritiesMap = handler.get( params[0] ); Map<Byte, Set<Method>> prioritiesMap = handler.get( params[0] );
if ( prioritiesMap == null ) if ( prioritiesMap == null )
{ {
prioritiesMap = new HashMap<>(); prioritiesMap = new HashMap<>();
@ -100,19 +100,19 @@ public class EventBus
public void register(Object listener) public void register(Object listener)
{ {
Map<Class<?>, Map<EventPriority, Set<Method>>> handler = findHandlers( listener ); Map<Class<?>, Map<Byte, Set<Method>>> handler = findHandlers( listener );
lock.writeLock().lock(); lock.writeLock().lock();
try try
{ {
for ( Map.Entry<Class<?>, Map<EventPriority, Set<Method>>> e : handler.entrySet() ) for ( Map.Entry<Class<?>, Map<Byte, Set<Method>>> e : handler.entrySet() )
{ {
Map<EventPriority, Map<Object, Method[]>> prioritiesMap = byListenerAndPriority.get( e.getKey() ); Map<Byte, Map<Object, Method[]>> prioritiesMap = byListenerAndPriority.get( e.getKey() );
if ( prioritiesMap == null ) if ( prioritiesMap == null )
{ {
prioritiesMap = new HashMap<>(); prioritiesMap = new HashMap<>();
byListenerAndPriority.put( e.getKey(), prioritiesMap ); byListenerAndPriority.put( e.getKey(), prioritiesMap );
} }
for ( Map.Entry<EventPriority, Set<Method>> entry : e.getValue().entrySet() ) for ( Map.Entry<Byte, Set<Method>> entry : e.getValue().entrySet() )
{ {
Map<Object, Method[]> currentPriorityMap = prioritiesMap.get( entry.getKey() ); Map<Object, Method[]> currentPriorityMap = prioritiesMap.get( entry.getKey() );
if ( currentPriorityMap == null ) if ( currentPriorityMap == null )
@ -133,16 +133,16 @@ public class EventBus
public void unregister(Object listener) public void unregister(Object listener)
{ {
Map<Class<?>, Map<EventPriority, Set<Method>>> handler = findHandlers( listener ); Map<Class<?>, Map<Byte, Set<Method>>> handler = findHandlers( listener );
lock.writeLock().lock(); lock.writeLock().lock();
try try
{ {
for ( Map.Entry<Class<?>, Map<EventPriority, Set<Method>>> e : handler.entrySet() ) for ( Map.Entry<Class<?>, Map<Byte, Set<Method>>> e : handler.entrySet() )
{ {
Map<EventPriority, Map<Object, Method[]>> prioritiesMap = byListenerAndPriority.get( e.getKey() ); Map<Byte, Map<Object, Method[]>> prioritiesMap = byListenerAndPriority.get( e.getKey() );
if ( prioritiesMap != null ) if ( prioritiesMap != null )
{ {
for ( EventPriority priority : e.getValue().keySet() ) for ( Byte priority : e.getValue().keySet() )
{ {
Map<Object, Method[]> currentPriority = prioritiesMap.get( priority ); Map<Object, Method[]> currentPriority = prioritiesMap.get( priority );
if ( currentPriority != null ) if ( currentPriority != null )
@ -174,11 +174,11 @@ public class EventBus
*/ */
private void bakeHandlers(Class<?> eventClass) private void bakeHandlers(Class<?> eventClass)
{ {
Map<EventPriority, Map<Object, Method[]>> handlersByPriority = byListenerAndPriority.get( eventClass ); Map<Byte, Map<Object, Method[]>> handlersByPriority = byListenerAndPriority.get( eventClass );
if ( handlersByPriority != null ) if ( handlersByPriority != null )
{ {
List<EventHandlerMethod> handlersList = new ArrayList<>( handlersByPriority.size() * 2 ); List<EventHandlerMethod> handlersList = new ArrayList<>( handlersByPriority.size() * 2 );
for ( EventPriority value : EventPriority.values() ) for ( byte value = Byte.MIN_VALUE; value < Byte.MAX_VALUE; value++ )
{ {
Map<Object, Method[]> handlersByListener = handlersByPriority.get( value ); Map<Object, Method[]> handlersByListener = handlersByPriority.get( value );
if ( handlersByListener != null ) if ( handlersByListener != null )

View File

@ -20,8 +20,7 @@ public @interface EventHandler
* <li>NORMAL</li> * <li>NORMAL</li>
* <li>HIGH</li> * <li>HIGH</li>
* <li>HIGHEST</li> * <li>HIGHEST</li>
* <li>MONITOR</li>
* </ol> * </ol>
*/ */
EventPriority priority() default EventPriority.NORMAL; byte priority() default EventPriority.NORMAL;
} }

View File

@ -5,10 +5,6 @@ import java.lang.reflect.Method;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
/**
*
* @author daboross
*/
@AllArgsConstructor @AllArgsConstructor
public class EventHandlerMethod public class EventHandlerMethod
{ {

View File

@ -1,45 +1,19 @@
package net.md_5.bungee.event; package net.md_5.bungee.event;
import lombok.AllArgsConstructor; import lombok.AccessLevel;
import lombok.Getter; import lombok.NoArgsConstructor;
/** /**
* Importance of the {@link EventHandler}. When executing an Event, the handlers * Importance of the {@link EventHandler}. When executing an Event, the handlers
* are called in order of their Priority. * are called in order of their Priority.
*/ */
@AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PRIVATE)
public enum EventPriority public class EventPriority
{ {
/** public static final byte LOWEST = -64;
* Lowest EventPriority. Use this priority to allow other plugins to further public static final byte LOW = -32;
* customize the outcome. public static final byte NORMAL = 0;
*/ public static final byte HIGH = 32;
LOWEST( 0 ), public static final byte HIGHEST = 64;
/**
* Higher than lowest, lower than normal.
*/
LOW( 1 ),
/**
* Default EventPriority
*/
NORMAL( 2 ),
/**
* High EventPriority. Use this priority to have more verdict on the
* outcome.
*/
HIGH( 3 ),
/**
* Most important EventPriorty for changes. Use this priority to have
* absolute verdict of the outcome of this event.
*/
HIGHEST( 4 ),
/**
* Logging/Monitor EventPriority. This priority is for <b>read only</b>
* event handlers. Do not change the outcome of the event in this priority.
* Intended for logging purposes.
*/
MONITOR( 5 );
@Getter
private final int priority;
} }

View File

@ -1,15 +1,8 @@
/*
* Copyright (C) 2013 Dabo Ross <www.daboross.net>
*/
package net.md_5.bungee.event; package net.md_5.bungee.event;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/**
*
* @author daboross
*/
public class UnregisteringListenerTest public class UnregisteringListenerTest
{ {