これをもっと読みやすく、きれいにするために何ができるのだろうかと思っています。読みやすいということは、他の開発者にとって読みやすいという意味です。
同じコードを2回使用したくありません。いくつかの方法で短くできると思いますが、よくわかりません...
@Override
public void dispatchEvent(Event event) {
checkNotNull(event);
CancellableEvent cancellableEvent = null;
boolean cancellable;
if (cancellable = event instanceof CancellableEvent) {
cancellableEvent = (CancellableEvent) event;
checkArgument(cancellableEvent.isCancelled());
}
// Ignore-cancellation event handlers will run
for (EventPriority priority : EventPriority.values()) {
Map<Method, EventListener> internalMapping = getRegistry().getMethodMap(event.getClass(), priority, true);
if (internalMapping != null) {
for (Entry<Method, EventListener> entry : internalMapping.entrySet()) {
try {
entry.getKey().invoke(entry.getValue(), event);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
/*
* Delegate any exceptions that occur from
* the method to a runtime exception.
*/
throw new RuntimeException(e);
}
}
}
}
// Event handlers that consider cancellation will run
for (EventPriority priority : EventPriority.values()) {
Map<Method, EventListener> internalMapping = getRegistry().getMethodMap(event.getClass(), priority, false);
if (internalMapping != null) {
for (Entry<Method, EventListener> entry : internalMapping.entrySet()) {
try {
entry.getKey().invoke(entry.getValue(), event);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
/*
* Delegate any exceptions that occur from
* the method to a runtime exception.
*/
throw new RuntimeException(e);
}
// Immediately return in the case of the event being cancelled.
if (cancellable && cancellableEvent.isCancelled()) {
return;
}
}
}
}
}