├── .github
├── FUNDING.yml
├── dependabot.yml
└── workflows
│ └── build.yml
├── .idea
└── copyright
│ ├── profiles_settings.xml
│ └── Apache_2_0.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── settings.gradle.kts
├── .editorconfig
├── gradle.properties
├── src
├── main
│ └── java
│ │ └── de
│ │ └── florianmichael
│ │ └── dietrichevents2
│ │ ├── StateTypes.java
│ │ ├── BreakableException.java
│ │ ├── AbstractEvent.java
│ │ ├── Priorities.java
│ │ ├── CancellableEvent.java
│ │ ├── BreakableEvent.java
│ │ └── DietrichEvents2.java
├── test
│ └── java
│ │ └── de
│ │ └── florianmichael
│ │ └── dietrichevents2
│ │ ├── BreakableTestListener.java
│ │ ├── CancellableTestListener.java
│ │ ├── TestListener.java
│ │ ├── CancellableEventTest.java
│ │ ├── UnsubscribeAllTest.java
│ │ ├── BreakableEventTest.java
│ │ ├── ExceptionTest.java
│ │ ├── GlobalTest.java
│ │ └── PriorityTest.java
└── jmh
│ └── java
│ └── de
│ └── florianmichael
│ └── dietrichevents2
│ ├── BenchmarkListener.java
│ └── BenchmarkCaller.java
├── gradlew.bat
├── gradlew
├── README.md
└── LICENSE
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | github: FlorianMichael
2 | custom: [ "https://florianmichael.de/donate" ]
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FlorianMichael/DietrichEvents2/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: "gradle"
4 | cooldown:
5 | default-days: 7
6 | directory: "/"
7 | schedule:
8 | interval: "daily"
9 | - package-ecosystem: "github-actions"
10 | cooldown:
11 | default-days: 7
12 | directory: "/"
13 | schedule:
14 | interval: "weekly"
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Gradle
2 |
3 | .gradle/
4 | build/
5 | out/
6 | classes/
7 | .kotlin/
8 |
9 | # Eclipse
10 |
11 | *.launch
12 |
13 | # Idea
14 |
15 | .idea/
16 | !.idea/copyright/*
17 | !.idea/scopes/*
18 | *.iml
19 | *.ipr
20 | *.iws
21 |
22 | # VSCode
23 |
24 | .settings/
25 | .vscode/
26 | bin/
27 | .classpath
28 | .project
29 |
30 | # macOS
31 |
32 | DS_Store
33 |
34 | # Misc
35 |
36 | run/
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionSha256Sum=df67a32e86e3276d011735facb1535f64d0d88df84fa87521e90becc2d735444
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.0-bin.zip
5 | networkTimeout=10000
6 | validateDistributionUrl=true
7 | zipStoreBase=GRADLE_USER_HOME
8 | zipStorePath=wrapper/dists
9 |
--------------------------------------------------------------------------------
/settings.gradle.kts:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | mavenCentral()
4 | gradlePluginPortal()
5 | }
6 |
7 | plugins {
8 | id("de.florianmichael.baseproject.BaseProject") version "1.2.8"
9 | }
10 | }
11 |
12 | plugins {
13 | id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0"
14 | }
15 |
16 | rootProject.name = "DietrichEvents2"
17 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | [*]
2 | charset = utf-8
3 | indent_size = 4
4 | indent_style = space
5 | insert_final_newline = true
6 | tab_width = 4
7 |
8 | [*.java]
9 | ij_java_class_count_to_use_import_on_demand = 999999
10 | ij_java_names_count_to_use_import_on_demand = 999999
11 | ij_java_imports_layout = *, |, $*
12 | ij_java_generate_final_locals = true
13 | ij_java_generate_final_parameters = true
14 |
15 | [{*.json,*.yml}]
16 | indent_size = 2
17 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Gradle Properties
2 | org.gradle.jvmargs=-Xmx8G
3 | org.gradle.parallel=true
4 | org.gradle.configuration-cache=true
5 |
6 | # Project Details
7 | project_jvm_version=8
8 |
9 | project_group=de.florianmichael
10 | project_name=DietrichEvents2
11 | project_version=1.2.2-SNAPSHOT
12 | project_description=One of the fastest Java event systems in the world using compiler optimizations, which still has a lot of features
13 |
14 | publishing_gh_account=FlorianMichael
15 | publishing_dev_name=EnZaXD
16 | publishing_dev_mail=florian.michael07@gmail.com
17 |
--------------------------------------------------------------------------------
/.idea/copyright/Apache_2_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: Build
2 | on: [pull_request, push, workflow_dispatch]
3 | permissions:
4 | contents: read
5 |
6 | jobs:
7 | build:
8 | runs-on: ubuntu-24.04-arm
9 | steps:
10 | - name: Checkout Repository
11 | uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # 6.0.1
12 | with:
13 | persist-credentials: false
14 | - name: Set up Gradle
15 | uses: gradle/actions/setup-gradle@4d9f0ba0025fe599b4ebab900eb7f3a1d93ef4c2 # 5.0.0
16 | - name: Set up JDK 17
17 | uses: actions/setup-java@f2beeb24e141e01a676f977032f5a29d81c9e27e # 5.1.0
18 | with:
19 | distribution: 'temurin'
20 | java-version: 17
21 | check-latest: true
22 | - name: Build with Gradle
23 | run: ./gradlew build
24 | - name: Upload Artifacts to GitHub
25 | uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # 5.0.0
26 | with:
27 | name: Artifacts
28 | path: build/libs/
--------------------------------------------------------------------------------
/src/main/java/de/florianmichael/dietrichevents2/StateTypes.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | /**
21 | * This class is optional and does not have to be used
22 | */
23 | public enum StateTypes {
24 |
25 | FIRST,
26 | PRE,
27 | INTRA,
28 | POST,
29 | LAST
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/de/florianmichael/dietrichevents2/BreakableException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | /**
21 | * Alternative to {@link BreakableEvent} which supports dynamic events and can be used in any context. Requires the
22 | * usage of {@link DietrichEvents2#callBreakable(int, AbstractEvent)} to call the event.
23 | */
24 | public class BreakableException extends RuntimeException {
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/de/florianmichael/dietrichevents2/AbstractEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | /**
21 | * This class represents an event.
22 | *
23 | * @param The type of the listener.
24 | */
25 | public interface AbstractEvent {
26 |
27 | /**
28 | * Calls the listener.
29 | *
30 | * @param listener The listener to call.
31 | */
32 | void call(final T listener);
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/test/java/de/florianmichael/dietrichevents2/BreakableTestListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | public interface BreakableTestListener {
21 |
22 | void onTest(final BreakableTestEvent event);
23 |
24 | class BreakableTestEvent extends BreakableEvent {
25 |
26 | public static final int ID = 1;
27 |
28 | @Override
29 | public void call0(BreakableTestListener listener) {
30 | listener.onTest(this);
31 | }
32 |
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/test/java/de/florianmichael/dietrichevents2/CancellableTestListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | public interface CancellableTestListener {
21 |
22 | void onTest(final CancellableTestEvent event);
23 |
24 | class CancellableTestEvent extends CancellableEvent {
25 |
26 | public static final int ID = 2;
27 |
28 | @Override
29 | public void call(CancellableTestListener listener) {
30 | listener.onTest(this);
31 | }
32 |
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/test/java/de/florianmichael/dietrichevents2/TestListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | public interface TestListener {
21 |
22 | void onTest(final TestEvent event);
23 |
24 | class TestEvent implements AbstractEvent {
25 |
26 | public static final int ID = 0;
27 |
28 | public final Object something;
29 |
30 | public TestEvent(Object something) {
31 | this.something = something;
32 | }
33 |
34 | @Override
35 | public void call(TestListener listener) {
36 | listener.onTest(this);
37 | }
38 |
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/de/florianmichael/dietrichevents2/Priorities.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | /**
21 | * This class is optional and does not have to be used. It shows how priorities can be used. Priorities are sorted
22 | * in ascending order, so the highest priority is the lowest number.
23 | */
24 | public class Priorities {
25 |
26 | public static final int FALLBACK = Integer.MAX_VALUE;
27 | public static final int LOWEST = 2;
28 | public static final int LOW = 1;
29 | public static final int NORMAL = 0; // Default priority
30 | public static final int HIGH = -1;
31 | public static final int HIGHEST = -2;
32 | public static final int MONITOR = Integer.MIN_VALUE;
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/jmh/java/de/florianmichael/dietrichevents2/BenchmarkListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | import org.openjdk.jmh.infra.Blackhole;
21 |
22 | public interface BenchmarkListener {
23 |
24 | void onBenchmark(final Blackhole blackhole);
25 |
26 | class BenchmarkEvent implements AbstractEvent {
27 |
28 | public static final int ID = 0;
29 |
30 | private final Blackhole blackhole;
31 |
32 | public BenchmarkEvent(final Blackhole blackhole) {
33 | this.blackhole = blackhole;
34 | }
35 |
36 | @Override
37 | public void call(BenchmarkListener listener) {
38 | listener.onBenchmark(this.blackhole);
39 | }
40 |
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/test/java/de/florianmichael/dietrichevents2/CancellableEventTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | import org.junit.jupiter.api.Assertions;
21 | import org.junit.jupiter.api.Test;
22 |
23 | public class CancellableEventTest {
24 |
25 | @Test
26 | void fire() {
27 | final DietrichEvents2 d = DietrichEvents2.global();
28 | d.subscribe(CancellableTestListener.CancellableTestEvent.ID, (CancellableTestListener) CancellableEvent::cancel);
29 |
30 | CancellableTestListener.CancellableTestEvent event = new CancellableTestListener.CancellableTestEvent();
31 | d.callUnsafe(CancellableTestListener.CancellableTestEvent.ID, event);
32 | Assertions.assertTrue(event.isCancelled());
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/de/florianmichael/dietrichevents2/CancellableEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | /**
21 | * This class represents an event that can be cancelled.
22 | *
23 | * @param The type of the listener.
24 | */
25 | public abstract class CancellableEvent implements AbstractEvent {
26 |
27 | /**
28 | * Whether the event is cancelled.
29 | */
30 | private boolean cancelled;
31 |
32 | /**
33 | * Cancels the event.
34 | */
35 | public void cancel() {
36 | setCancelled(true);
37 | }
38 |
39 | public boolean isCancelled() {
40 | return cancelled;
41 | }
42 |
43 | public void setCancelled(boolean cancelled) {
44 | this.cancelled = cancelled;
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/src/test/java/de/florianmichael/dietrichevents2/UnsubscribeAllTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | import org.junit.jupiter.api.Assertions;
21 | import org.junit.jupiter.api.BeforeAll;
22 | import org.junit.jupiter.api.Test;
23 |
24 | public class UnsubscribeAllTest {
25 |
26 | private static final TestListener event = event -> System.out.println("TestEvent: " + event.something);
27 |
28 | @BeforeAll
29 | static void setUp() {
30 | DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, event);
31 | }
32 |
33 | @Test
34 | void unsubscribeAll() {
35 | final DietrichEvents2 d = DietrichEvents2.global();
36 |
37 | d.unsubscribeAll(TestListener.TestEvent.ID);
38 | Assertions.assertFalse(d.hasSubscriber(TestListener.TestEvent.ID));
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/test/java/de/florianmichael/dietrichevents2/BreakableEventTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | import org.junit.jupiter.api.Assertions;
21 | import org.junit.jupiter.api.Test;
22 |
23 | public class BreakableEventTest {
24 |
25 | private static int executions;
26 |
27 | @Test
28 | void fire() {
29 | final BreakableTestListener.BreakableTestEvent event = new BreakableTestListener.BreakableTestEvent();
30 |
31 | final DietrichEvents2 d = DietrichEvents2.global();
32 | d.subscribe(BreakableTestListener.BreakableTestEvent.ID, (BreakableTestListener) e -> {
33 | executions++;
34 | e.stopHandling();
35 | });
36 | for (int i = 0; i < 10; i++) {
37 | d.callUnsafe(BreakableTestListener.BreakableTestEvent.ID, event);
38 | }
39 |
40 | Assertions.assertEquals(1, executions);
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/test/java/de/florianmichael/dietrichevents2/ExceptionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | import org.junit.jupiter.api.Assertions;
21 | import org.junit.jupiter.api.BeforeAll;
22 | import org.junit.jupiter.api.Test;
23 |
24 | public class ExceptionTest {
25 |
26 | private static final TestListener event = event -> System.out.println("TestEvent: " + event.something.getClass());
27 |
28 | private static DietrichEvents2 instance;
29 |
30 | private static String message;
31 |
32 | @BeforeAll
33 | static void setUp() {
34 | instance = new DietrichEvents2(1, throwable -> message = "Custom message: " + throwable.getMessage());
35 | instance.subscribe(TestListener.TestEvent.ID, event);
36 | }
37 |
38 | @Test
39 | void fireException() {
40 | instance.call(TestListener.TestEvent.ID, new TestListener.TestEvent(null));
41 | Assertions.assertTrue(message.contains("Custom message"));
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/jmh/java/de/florianmichael/dietrichevents2/BenchmarkCaller.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | import org.openjdk.jmh.annotations.*;
21 | import org.openjdk.jmh.infra.Blackhole;
22 |
23 | import java.util.concurrent.TimeUnit;
24 |
25 | @State(Scope.Benchmark)
26 | @OutputTimeUnit(TimeUnit.NANOSECONDS)
27 | @Warmup(iterations = 4, time = 5)
28 | @Measurement(iterations = 4, time = 5)
29 | public class BenchmarkCaller implements BenchmarkListener {
30 |
31 | private static final int ITERATIONS = 100_000;
32 |
33 | @Setup
34 | public void setup() {
35 | DietrichEvents2.global().subscribe(BenchmarkEvent.ID, this);
36 | }
37 |
38 | @Benchmark
39 | @BenchmarkMode(Mode.AverageTime)
40 | @Fork(value = 1, warmups = 1)
41 | public void callBenchmarkListener(Blackhole blackhole) {
42 | for (int i = 0; i < ITERATIONS; i++) {
43 | DietrichEvents2.global().call(BenchmarkEvent.ID, new BenchmarkListener.BenchmarkEvent(blackhole));
44 | }
45 | }
46 |
47 | @Override
48 | public void onBenchmark(Blackhole blackhole) {
49 | blackhole.consume(Integer.bitCount(Integer.parseInt("123")));
50 | }
51 |
52 | }
--------------------------------------------------------------------------------
/src/main/java/de/florianmichael/dietrichevents2/BreakableEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | /**
21 | * This class represents an event that can be aborted. Event types that extend this class have to be static in order to
22 | * work. Listeners can call {@link #stopHandling()} to abort the event.
23 | *
24 | * @param The type of the listener.
25 | */
26 | public abstract class BreakableEvent implements AbstractEvent {
27 |
28 | /**
29 | * Whether the event is cancelled.
30 | */
31 | private boolean abort;
32 |
33 | @Override
34 | public final void call(T listener) {
35 | if (!isAbort()) {
36 | call0(listener);
37 | }
38 | }
39 |
40 | /**
41 | * Calls the listener.
42 | *
43 | * @param listener The listener to call.
44 | */
45 | public abstract void call0(final T listener);
46 |
47 | /**
48 | * Cancels the event.
49 | */
50 | public void stopHandling() {
51 | setAbort(true);
52 | }
53 |
54 | public boolean isAbort() {
55 | return abort;
56 | }
57 |
58 | public void setAbort(boolean abort) {
59 | this.abort = abort;
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/src/test/java/de/florianmichael/dietrichevents2/GlobalTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
3 | * Copyright (C) 2023-2025 FlorianMichael/EnZaXD and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package de.florianmichael.dietrichevents2;
19 |
20 | import org.junit.jupiter.api.Assertions;
21 | import org.junit.jupiter.api.BeforeAll;
22 | import org.junit.jupiter.api.Test;
23 |
24 | import java.util.ArrayList;
25 | import java.util.List;
26 |
27 | public class GlobalTest {
28 |
29 | private static final List