#3221: Use computeIfAbsent method in EventBus

This commit is contained in:
Janmm14 2021-12-10 12:50:24 +01:00 committed by md_5
parent 51eb1ac623
commit 2479fab632
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11

View File

@ -88,18 +88,8 @@ public class EventBus
} ); } );
continue; continue;
} }
Map<Byte, Set<Method>> prioritiesMap = handler.get( params[0] ); Map<Byte, Set<Method>> prioritiesMap = handler.computeIfAbsent( params[0], k -> new HashMap<>() );
if ( prioritiesMap == null ) Set<Method> priority = prioritiesMap.computeIfAbsent( annotation.priority(), k -> new HashSet<>() );
{
prioritiesMap = new HashMap<>();
handler.put( params[0], prioritiesMap );
}
Set<Method> priority = prioritiesMap.get( annotation.priority() );
if ( priority == null )
{
priority = new HashSet<>();
prioritiesMap.put( annotation.priority(), priority );
}
priority.add( m ); priority.add( m );
} }
} }
@ -114,20 +104,10 @@ public class EventBus
{ {
for ( Map.Entry<Class<?>, Map<Byte, Set<Method>>> e : handler.entrySet() ) for ( Map.Entry<Class<?>, Map<Byte, Set<Method>>> e : handler.entrySet() )
{ {
Map<Byte, Map<Object, Method[]>> prioritiesMap = byListenerAndPriority.get( e.getKey() ); Map<Byte, Map<Object, Method[]>> prioritiesMap = byListenerAndPriority.computeIfAbsent( e.getKey(), k -> new HashMap<>() );
if ( prioritiesMap == null )
{
prioritiesMap = new HashMap<>();
byListenerAndPriority.put( e.getKey(), prioritiesMap );
}
for ( Map.Entry<Byte, 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.computeIfAbsent( entry.getKey(), k -> new HashMap<>() );
if ( currentPriorityMap == null )
{
currentPriorityMap = new HashMap<>();
prioritiesMap.put( entry.getKey(), currentPriorityMap );
}
currentPriorityMap.put( listener, entry.getValue().toArray( new Method[ 0 ] ) ); currentPriorityMap.put( listener, entry.getValue().toArray( new Method[ 0 ] ) );
} }
bakeHandlers( e.getKey() ); bakeHandlers( e.getKey() );