-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add listenAll method to EventManager.
- Loading branch information
1 parent
f8263b2
commit 44cf105
Showing
7 changed files
with
340 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
libraries/event/src/main/java/dev/yumi/commons/event/ListenerPhase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2023 Yumi Project | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.yumi.commons.event; | ||
|
||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Annotates a specific callback in a listener a specific phase to listen. | ||
* | ||
* @see Event#addPhaseOrdering(Comparable, Comparable) | ||
* @see Event#register(Comparable, Object) | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@Repeatable(ListenerPhases.class) | ||
public @interface ListenerPhase { | ||
/** | ||
* {@return the targeted callback interface} | ||
*/ | ||
Class<?> callbackTarget(); | ||
|
||
/** | ||
* {@return the identifier of the phase to listen} | ||
*/ | ||
String value(); | ||
} |
24 changes: 24 additions & 0 deletions
24
libraries/event/src/main/java/dev/yumi/commons/event/ListenerPhases.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2023 Yumi Project | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.yumi.commons.event; | ||
|
||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Represents the container type of {@link ListenerPhase} to make it repeatable. | ||
* | ||
* @see ListenerPhase | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface ListenerPhases { | ||
ListenerPhase[] value(); | ||
} |
183 changes: 183 additions & 0 deletions
183
libraries/event/src/test/java/dev/yumi/commons/event/test/EventListenAllTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* Copyright 2023 Yumi Project | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package dev.yumi.commons.event.test; | ||
|
||
import dev.yumi.commons.collections.YumiCollections; | ||
import dev.yumi.commons.event.EventManager; | ||
import dev.yumi.commons.event.ListenerPhase; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class EventListenAllTest { | ||
@Test | ||
public void testListenAllRejectNoEvents() { | ||
var events = new EventManager<>("default", Function.identity()); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> { | ||
events.listenAll(new Listener(new ExecutionTester())); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testListenAllRejectMissingEvents() { | ||
var events = new EventManager<>("default", Function.identity()); | ||
var testEvent = events.create(TestCallback.class); | ||
var testFilterEvent = events.create(FilterTestCallback.class); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> { | ||
events.listenAll(new Object(), testEvent, testFilterEvent); | ||
}); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void testListenAllRejectGenerics() { | ||
var events = new EventManager<>("default", Function.identity()); | ||
var testGenericEvent = events.create(Generic.class, listeners -> arg -> { | ||
for (var listener : listeners) { | ||
listener.invoke(arg); | ||
} | ||
}); | ||
var generic = new Generic<String>() { | ||
@Override | ||
public void invoke(String arg) { | ||
} | ||
}; | ||
|
||
assertThrows(IllegalArgumentException.class, () -> { | ||
events.listenAll(generic, testGenericEvent); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testListenAll() { | ||
var events = new EventManager<>("default", Function.identity()); | ||
var testEvent = events.create(TestCallback.class); | ||
var testFilterEvent = events.create(FilterTestCallback.class); | ||
var tester = new ExecutionTester(); | ||
|
||
events.listenAll(new Listener(tester), testEvent, testFilterEvent); | ||
|
||
testEvent.invoker().call("Test"); | ||
tester.assertCalled(1); | ||
|
||
testFilterEvent.invoker().filter("Yippee"); | ||
tester.assertCalled(2); | ||
} | ||
|
||
@Test | ||
public void testListenAllPhases() { | ||
var events = new EventManager<>("default", Function.identity()); | ||
var tester = new ExecutionTester(); | ||
|
||
YumiCollections.forAllPermutations( | ||
List.of( | ||
List.of(new PhaseListenerEarly(tester)), | ||
List.of(new PhaseListenerDefault(tester), new PhaseListenerExplicitDefault(tester)), | ||
List.of(new PhaseListenerLate(tester)) | ||
), | ||
listeners -> { | ||
var testEvent = events.createWithPhases(TestCallback.class, "early", "default", "late"); | ||
var filterEvent = events.createWithPhases(FilterTestCallback.class, "early", "default", "late"); | ||
|
||
for (var phaseListeners : listeners) { | ||
for (var listener : phaseListeners) { | ||
events.listenAll(listener, testEvent, filterEvent); | ||
} | ||
} | ||
|
||
tester.reset(); | ||
testEvent.invoker().call("Test"); | ||
tester.assertCalled(4); | ||
tester.reset(); | ||
assertTrue(filterEvent.invoker().filter("Yippee")); | ||
tester.assertCalled(3); | ||
}); | ||
} | ||
|
||
interface Generic<T> { | ||
void invoke(T arg); | ||
} | ||
|
||
@ListenerPhase(callbackTarget = TestCallback.class, value = "early") | ||
@ListenerPhase(callbackTarget = FilterTestCallback.class, value = "late") | ||
record PhaseListenerEarly(ExecutionTester tester) implements TestCallback, FilterTestCallback { | ||
@Override | ||
public void call(String text) { | ||
this.tester.assertOrder(0); | ||
} | ||
|
||
@Override | ||
public boolean filter(String text) { | ||
throw new IllegalStateException("The filter callback should have already returned a value."); | ||
} | ||
} | ||
|
||
record PhaseListenerDefault(ExecutionTester tester) implements TestCallback, FilterTestCallback { | ||
@Override | ||
public void call(String text) { | ||
this.tester.assertOrder(1); | ||
} | ||
|
||
@Override | ||
public boolean filter(String text) { | ||
this.tester.assertOrder(1); | ||
return false; | ||
} | ||
} | ||
|
||
@ListenerPhase(callbackTarget = TestCallback.class, value = "default") | ||
@ListenerPhase(callbackTarget = FilterTestCallback.class, value = "default") | ||
record PhaseListenerExplicitDefault(ExecutionTester tester) implements TestCallback, FilterTestCallback { | ||
@Override | ||
public void call(String text) { | ||
this.tester.assertOrder(2); | ||
} | ||
|
||
@Override | ||
public boolean filter(String text) { | ||
this.tester.assertOrder(2); | ||
return true; | ||
} | ||
} | ||
|
||
@ListenerPhase(callbackTarget = TestCallback.class, value = "late") | ||
@ListenerPhase(callbackTarget = FilterTestCallback.class, value = "early") | ||
record PhaseListenerLate(ExecutionTester tester) implements TestCallback, FilterTestCallback { | ||
@Override | ||
public void call(String text) { | ||
this.tester.assertOrder(3); | ||
} | ||
|
||
@Override | ||
public boolean filter(String text) { | ||
this.tester.assertOrder(0); | ||
return false; | ||
} | ||
} | ||
|
||
record Listener(ExecutionTester tester) implements TestCallback, FilterTestCallback { | ||
|
||
@Override | ||
public void call(String text) { | ||
this.tester.assertOrder(0); | ||
} | ||
|
||
@Override | ||
public boolean filter(String text) { | ||
this.tester.assertOrder(1); | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.