├── .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 | 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 VALUES = new ArrayList<>(); 30 | 31 | private static final TestListener ADDING_EVENT = event -> { 32 | System.out.println("TestEvent: " + event.something); 33 | VALUES.add(event.something); 34 | }; 35 | 36 | @BeforeAll 37 | static void setUp() { 38 | DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, ADDING_EVENT); 39 | } 40 | 41 | @Test 42 | void hasFunctions() { 43 | final DietrichEvents2 d = DietrichEvents2.global(); 44 | 45 | Assertions.assertTrue(d.hasSubscriber(TestListener.TestEvent.ID)); 46 | Assertions.assertNotEquals(0, d.getSubscribers(TestListener.TestEvent.ID).length); 47 | Assertions.assertTrue(d.isSubscriber(TestListener.TestEvent.ID, ADDING_EVENT)); 48 | } 49 | 50 | @Test 51 | void fire() { 52 | DietrichEvents2.global().callUnsafe(TestListener.TestEvent.ID, new TestListener.TestEvent("Hello World")); 53 | DietrichEvents2.global().callUnsafe(TestListener.TestEvent.ID, new TestListener.TestEvent(10)); 54 | 55 | Assertions.assertTrue(VALUES.contains("Hello World")); 56 | Assertions.assertTrue(VALUES.contains(10)); 57 | Assertions.assertFalse(VALUES.contains(20)); 58 | } 59 | 60 | @Test 61 | void unsubscribe() { 62 | final DietrichEvents2 d = DietrichEvents2.global(); 63 | 64 | d.unsubscribe(TestListener.TestEvent.ID, ADDING_EVENT); 65 | Assertions.assertFalse(d.isSubscriber(TestListener.TestEvent.ID, ADDING_EVENT)); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/test/java/de/florianmichael/dietrichevents2/PriorityTest.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 | import java.util.LinkedList; 24 | import java.util.List; 25 | 26 | public class PriorityTest { 27 | 28 | private static final List VALUES = new LinkedList<>(); 29 | 30 | @Test 31 | void fire() { 32 | DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test3"), Priorities.NORMAL); 33 | DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test2"), Priorities.HIGH); 34 | DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test1"), Priorities.LOWEST); 35 | DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test5"), Priorities.HIGHEST); 36 | DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test4"), Priorities.LOW); 37 | DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test6"), Priorities.FALLBACK); 38 | DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test7"), Priorities.MONITOR); 39 | DietrichEvents2.global().callUnsafe(TestListener.TestEvent.ID, new TestListener.TestEvent(null)); 40 | 41 | Assertions.assertEquals(VALUES.get(0), "Test7"); 42 | Assertions.assertEquals(VALUES.get(1), "Test5"); 43 | Assertions.assertEquals(VALUES.get(2), "Test2"); 44 | Assertions.assertEquals(VALUES.get(3), "Test3"); 45 | Assertions.assertEquals(VALUES.get(4), "Test4"); 46 | Assertions.assertEquals(VALUES.get(5), "Test1"); 47 | Assertions.assertEquals(VALUES.get(6), "Test6"); 48 | } 49 | 50 | private static TestListener createListener(final String name) { 51 | return event -> VALUES.add(name); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | @rem SPDX-License-Identifier: Apache-2.0 17 | @rem 18 | 19 | @if "%DEBUG%"=="" @echo off 20 | @rem ########################################################################## 21 | @rem 22 | @rem Gradle startup script for Windows 23 | @rem 24 | @rem ########################################################################## 25 | 26 | @rem Set local scope for the variables with windows NT shell 27 | if "%OS%"=="Windows_NT" setlocal 28 | 29 | set DIRNAME=%~dp0 30 | if "%DIRNAME%"=="" set DIRNAME=. 31 | @rem This is normally unused 32 | set APP_BASE_NAME=%~n0 33 | set APP_HOME=%DIRNAME% 34 | 35 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 36 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 37 | 38 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 39 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 40 | 41 | @rem Find java.exe 42 | if defined JAVA_HOME goto findJavaFromJavaHome 43 | 44 | set JAVA_EXE=java.exe 45 | %JAVA_EXE% -version >NUL 2>&1 46 | if %ERRORLEVEL% equ 0 goto execute 47 | 48 | echo. 1>&2 49 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 50 | echo. 1>&2 51 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 52 | echo location of your Java installation. 1>&2 53 | 54 | goto fail 55 | 56 | :findJavaFromJavaHome 57 | set JAVA_HOME=%JAVA_HOME:"=% 58 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 59 | 60 | if exist "%JAVA_EXE%" goto execute 61 | 62 | echo. 1>&2 63 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 64 | echo. 1>&2 65 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 66 | echo location of your Java installation. 1>&2 67 | 68 | goto fail 69 | 70 | :execute 71 | @rem Setup the command line 72 | 73 | 74 | 75 | @rem Execute Gradle 76 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* 77 | 78 | :end 79 | @rem End local scope for the variables with windows NT shell 80 | if %ERRORLEVEL% equ 0 goto mainEnd 81 | 82 | :fail 83 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 84 | rem the _cmd.exe /c_ return code! 85 | set EXIT_CODE=%ERRORLEVEL% 86 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 87 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 88 | exit /b %EXIT_CODE% 89 | 90 | :mainEnd 91 | if "%OS%"=="Windows_NT" endlocal 92 | 93 | :omega 94 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | # SPDX-License-Identifier: Apache-2.0 19 | # 20 | 21 | ############################################################################## 22 | # 23 | # Gradle start up script for POSIX generated by Gradle. 24 | # 25 | # Important for running: 26 | # 27 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 28 | # noncompliant, but you have some other compliant shell such as ksh or 29 | # bash, then to run this script, type that shell name before the whole 30 | # command line, like: 31 | # 32 | # ksh Gradle 33 | # 34 | # Busybox and similar reduced shells will NOT work, because this script 35 | # requires all of these POSIX shell features: 36 | # * functions; 37 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 38 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 39 | # * compound commands having a testable exit status, especially «case»; 40 | # * various built-in commands including «command», «set», and «ulimit». 41 | # 42 | # Important for patching: 43 | # 44 | # (2) This script targets any POSIX shell, so it avoids extensions provided 45 | # by Bash, Ksh, etc; in particular arrays are avoided. 46 | # 47 | # The "traditional" practice of packing multiple parameters into a 48 | # space-separated string is a well documented source of bugs and security 49 | # problems, so this is (mostly) avoided, by progressively accumulating 50 | # options in "$@", and eventually passing that to Java. 51 | # 52 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 53 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 54 | # see the in-line comments for details. 55 | # 56 | # There are tweaks for specific operating systems such as AIX, CygWin, 57 | # Darwin, MinGW, and NonStop. 58 | # 59 | # (3) This script is generated from the Groovy template 60 | # https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 61 | # within the Gradle project. 62 | # 63 | # You can find Gradle at https://github.com/gradle/gradle/. 64 | # 65 | ############################################################################## 66 | 67 | # Attempt to set APP_HOME 68 | 69 | # Resolve links: $0 may be a link 70 | app_path=$0 71 | 72 | # Need this for daisy-chained symlinks. 73 | while 74 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 75 | [ -h "$app_path" ] 76 | do 77 | ls=$( ls -ld "$app_path" ) 78 | link=${ls#*' -> '} 79 | case $link in #( 80 | /*) app_path=$link ;; #( 81 | *) app_path=$APP_HOME$link ;; 82 | esac 83 | done 84 | 85 | # This is normally unused 86 | # shellcheck disable=SC2034 87 | APP_BASE_NAME=${0##*/} 88 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 89 | APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | 118 | 119 | # Determine the Java command to use to start the JVM. 120 | if [ -n "$JAVA_HOME" ] ; then 121 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 122 | # IBM's JDK on AIX uses strange locations for the executables 123 | JAVACMD=$JAVA_HOME/jre/sh/java 124 | else 125 | JAVACMD=$JAVA_HOME/bin/java 126 | fi 127 | if [ ! -x "$JAVACMD" ] ; then 128 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 129 | 130 | Please set the JAVA_HOME variable in your environment to match the 131 | location of your Java installation." 132 | fi 133 | else 134 | JAVACMD=java 135 | if ! command -v java >/dev/null 2>&1 136 | then 137 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 138 | 139 | Please set the JAVA_HOME variable in your environment to match the 140 | location of your Java installation." 141 | fi 142 | fi 143 | 144 | # Increase the maximum file descriptors if we can. 145 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 146 | case $MAX_FD in #( 147 | max*) 148 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 149 | # shellcheck disable=SC2039,SC3045 150 | MAX_FD=$( ulimit -H -n ) || 151 | warn "Could not query maximum file descriptor limit" 152 | esac 153 | case $MAX_FD in #( 154 | '' | soft) :;; #( 155 | *) 156 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 157 | # shellcheck disable=SC2039,SC3045 158 | ulimit -n "$MAX_FD" || 159 | warn "Could not set maximum file descriptor limit to $MAX_FD" 160 | esac 161 | fi 162 | 163 | # Collect all arguments for the java command, stacking in reverse order: 164 | # * args from the command line 165 | # * the main class name 166 | # * -classpath 167 | # * -D...appname settings 168 | # * --module-path (only if needed) 169 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 170 | 171 | # For Cygwin or MSYS, switch paths to Windows format before running java 172 | if "$cygwin" || "$msys" ; then 173 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 174 | 175 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 176 | 177 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 178 | for arg do 179 | if 180 | case $arg in #( 181 | -*) false ;; # don't mess with options #( 182 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 183 | [ -e "$t" ] ;; #( 184 | *) false ;; 185 | esac 186 | then 187 | arg=$( cygpath --path --ignore --mixed "$arg" ) 188 | fi 189 | # Roll the args list around exactly as many times as the number of 190 | # args, so each arg winds up back in the position where it started, but 191 | # possibly modified. 192 | # 193 | # NB: a `for` loop captures its iteration list before it begins, so 194 | # changing the positional parameters here affects neither the number of 195 | # iterations, nor the values presented in `arg`. 196 | shift # remove old arg 197 | set -- "$@" "$arg" # push replacement arg 198 | done 199 | fi 200 | 201 | 202 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 203 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 204 | 205 | # Collect all arguments for the java command: 206 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 207 | # and any embedded shellness will be escaped. 208 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 209 | # treated as '${Hostname}' itself on the command line. 210 | 211 | set -- \ 212 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 213 | -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ 214 | "$@" 215 | 216 | # Stop when "xargs" is not available. 217 | if ! command -v xargs >/dev/null 2>&1 218 | then 219 | die "xargs is not available" 220 | fi 221 | 222 | # Use "xargs" to parse quoted args. 223 | # 224 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 225 | # 226 | # In Bash we could simply go: 227 | # 228 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 229 | # set -- "${ARGS[@]}" "$@" 230 | # 231 | # but POSIX shell has neither arrays nor command substitution, so instead we 232 | # post-process each arg (as a line of input to sed) to backslash-escape any 233 | # character that might be a shell metacharacter, then use eval to reverse 234 | # that process (while maintaining the separation between arguments), and wrap 235 | # the whole thing up as a single "set" statement. 236 | # 237 | # This will of course break if any of these variables contains a newline or 238 | # an unmatched quote. 239 | # 240 | 241 | eval "set -- $( 242 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 243 | xargs -n1 | 244 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 245 | tr '\n' ' ' 246 | )" '"$@"' 247 | 248 | exec "$JAVACMD" "$@" 249 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DietrichEvents2 2 | One of the fastest Java event systems in the world using compiler optimizations, which still has a lot of features 3 | 4 | ## Contact 5 | If you encounter any issues, please report them on the 6 | [issue tracker](https://github.com/FlorianMichael/DietrichEvents2/issues). 7 | If you just want to talk or need help with DietrichEvents2 feel free to join my 8 | [Discord](https://florianmichael.de/discord). 9 | 10 | ## How to add this to your project 11 | ### Gradle/Maven 12 | 13 | To use DietrichEvents2 with Gradle/Maven you can 14 | use [the Maven Central repository](https://mvnrepository.com/artifact/de.florianmichael/DietrichEvents2) 15 | or [my own repository](https://maven.florianmichael.de/#/releases/de/florianmichael/DietrichEvents2). 16 | You can also find instructions how to implement it into your build script there. 17 | 18 | ### Jar File 19 | 20 | If you just want the latest jar file you can download it 21 | from [my build server](https://build.florianmichael.de/job/DietrichEvents2), [GitHub Actions](https://github.com/FlorianMichael/DietrichEvents2/actions) 22 | or use the [releases tab](https://github.com/FlorianMichael/DietrichEvents2/releases). 23 | 24 | ## Example usage 25 | ### Create instance 26 | You can use either **new DietrichEvents2(exception -> {});** or **DietrichEvents2.global()** to access an instance of 27 | the EventSystem, 28 | 29 | ### Create an Event 30 | ```java 31 | public interface ExampleListener { 32 | 33 | void onTest(final String example); 34 | 35 | class ExampleEvent extends AbstractEvent { 36 | 37 | /** 38 | * The ID has to be incremented for every new Event 39 | */ 40 | public static final int ID = 0; 41 | public final String example; 42 | 43 | public ExampleEvent(final String example) { 44 | this.example = example; 45 | } 46 | 47 | @Override 48 | public void call(ExampleListener listener) { 49 | listener.onTest(example); 50 | } 51 | } 52 | } 53 | ``` 54 | 55 | ### Register Listener 56 | ```java 57 | public class Test implements ExampleListener { 58 | 59 | public void begin() { 60 | DietrichEvents2.global().subscribe(ExampleEvent.ID, this); 61 | } 62 | 63 | @Override 64 | public void onTest(String example) { 65 | System.out.println("Executed once!"); 66 | DietrichEvents2.global().unsubscribe(ExampleEvent.ID, this); 67 | } 68 | } 69 | ``` 70 | 71 | ### Calling an Event 72 | ````java 73 | // There are multiple call methods which can be used depending on the situation: 74 | 75 | // - callUnsafe() -> Calls the event without any sanity 76 | 77 | // - callExceptionally() -> Calls the event without error handling 78 | 79 | // - call() -> Calls the event with global error handling 80 | 81 | // - callBreakable() -> Calls the event with error handling for every listener and supports 82 | // BreakableException to be thrown (also does resize) 83 | 84 | DietrichEvents2.global().call(ExampleEvent.ID, new ExampleEvent("Hello World!")); 85 | ```` 86 | 87 | ## JMH Benchmark 88 | The Benchmark shows the average time it takes to call an event 100.000 times. 89 | All Benchmarks are run with the same code (see **src/jmh/java**), but different Java versions. If an event system does 90 | not appear in every list, it does not exist for the particular Java version, or would not work without modifying 91 | it.
92 | 93 | ### Hardware specification: 94 | - CPU: Intel(R) Core(TM) i9-10900K CPU @ 3.70GHz 95 | - RAM: 48,0GB DDR4 96 | - GPU: NVIDIA GeForce RTX 3070 Ti 97 | - OS: Windows 11 Home 22H2 (Build 22621.1992) 98 | 99 | If you want to have another event system in the list, or want to have the source code to generate the tables, you can 100 | write me on Discord, look for it at "Contact" above.
101 | 102 | ### Java 17 103 | | Benchmark | Mode | Cnt | Score | Error | Units | 104 | |-------------------------------------------------------------------------------------------------------------------------|------|-----|--------------|------------|-------| 105 | | [DietrichEvents2](https://github.com/FlorianMichael/DietrichEvents2) | avgt | 4 | 310318,125 | 124933,800 | ns/op | 106 | | [ASMEvents](https://github.com/Lenni0451/ASMEvents) | avgt | 4 | 546376,840 | 18561,729 | ns/op | 107 | | [ChimeraEventBus](https://github.com/FelixH2012/ChimeraEventBus) | avgt | 4 | 581672,678 | 71045,952 | ns/op | 108 | | [norbit](https://github.com/CrosbyDev/norbit) | avgt | 4 | 604412,122 | 28715,740 | ns/op | 109 | | [DietrichEvents](https://github.com/FlorianMichael/DietrichEvents) | avgt | 4 | 627457,818 | 12842,704 | ns/op | 110 | | [EventAPI](https://github.com/Lenni0451/EventAPI) (MinimalEventManager) | avgt | 4 | 769492,673 | 18975,650 | ns/op | 111 | | [DarkMagician6](https://bitbucket.org/DarkMagician6/eventapi/src/master/) | avgt | 4 | 1020463,350 | 70117,174 | ns/op | 112 | | [LambdaEvents](https://github.com/Lenni0451/LambdaEvents) (LambdaMetaFactory) | avgt | 4 | 1134071,045 | 41718,145 | ns/op | 113 | | [LambdaEvents](https://github.com/Lenni0451/LambdaEvents) (Method Handles) | avgt | 4 | 1593772,392 | 66639,866 | ns/op | 114 | | [LambdaEvents](https://github.com/Lenni0451/LambdaEvents) (Reflection) | avgt | 4 | 2164632,230 | 49576,755 | ns/op | 115 | | [Cydhra](https://github.com/Cydhra/EventSystem/tree/master) (Event System) | avgt | 4 | 5169086,080 | 58597,729 | ns/op | 116 | | [EventAPI](https://github.com/Lenni0451/EventAPI) (EventManager) | avgt | 4 | 6735240,280 | 221306,805 | ns/op | 117 | | [Guava](https://github.com/google/guava) | avgt | 4 | 15337145,465 | 247949,530 | ns/op | 118 | 119 | ### Java 11 120 | | Benchmark | Mode | Cnt | Score | Error | Units | 121 | |-------------------------------------------------------------------------------------------------------------------------|------|-----|--------------|------------|-------| 122 | | [DietrichEvents2](https://github.com/FlorianMichael/DietrichEvents2) | avgt | 4 | 415349,165 | 7314,048 | ns/op | 123 | | [ASMEvents](https://github.com/Lenni0451/ASMEvents) | avgt | 4 | 663676,846 | 16997,570 | ns/op | 124 | | [ChimeraEventBus](https://github.com/FelixH2012/ChimeraEventBus) | avgt | 4 | 710557,890 | 72090,826 | ns/op | 125 | | [DietrichEvents](https://github.com/FlorianMichael/DietrichEvents) | avgt | 4 | 743307,467 | 18786,064 | ns/op | 126 | | [DarkMagician6](https://bitbucket.org/DarkMagician6/eventapi/src/master/) | avgt | 4 | 753029,999 | 16373,710 | ns/op | 127 | | [EventAPI](https://github.com/Lenni0451/EventAPI) (MinimalEventManager) | avgt | 4 | 770001,532 | 24463,218 | ns/op | 128 | | [EventAPI](https://github.com/Lenni0451/EventAPI) (ASMEventManager) | avgt | 4 | 771670,427 | 13801,284 | ns/op | 129 | | [LambdaEvents](https://github.com/Lenni0451/LambdaEvents) (LambdaMetaFactory) | avgt | 4 | 1130460,687 | 30560,981 | ns/op | 130 | | [LambdaEvents](https://github.com/Lenni0451/LambdaEvents) (Method Handles) | avgt | 4 | 1504976,295 | 69021,509 | ns/op | 131 | | [LambdaEvents](https://github.com/Lenni0451/LambdaEvents) (Reflection) | avgt | 4 | 2205444,424 | 337975,180 | ns/op | 132 | | [EventAPI](https://github.com/Lenni0451/EventAPI) (InjectionEventManager) | avgt | 4 | 3015519,829 | 44426,039 | ns/op | 133 | | [EventAPI](https://github.com/Lenni0451/EventAPI) (EventManager) | avgt | 4 | 5504772,250 | 46947,848 | ns/op | 134 | | [Cydhra](https://github.com/Cydhra/EventSystem/tree/master) (Event System) | avgt | 4 | 5794477,294 | 113790,263 | ns/op | 135 | | [Guava](https://github.com/google/guava) | avgt | 4 | 11656575,419 | 533926,166 | ns/op | 136 | 137 | ### Java 8 138 | | Benchmark | Mode | Cnt | Score | Error | Units | 139 | |-------------------------------------------------------------------------------------------------------------------------|------|-----|--------------|------------|-------| 140 | | [DietrichEvents2](https://github.com/FlorianMichael/DietrichEvents2) | avgt | 4 | 635392,941 | 25647,033 | ns/op | 141 | | [ASMEvents](https://github.com/Lenni0451/ASMEvents) | avgt | 4 | 813149,931 | 31763,759 | ns/op | 142 | | [LambdaEvents](https://github.com/Lenni0451/LambdaEvents) (LambdaMetaFactory) | avgt | 4 | 1129216,906 | 16383,663 | ns/op | 143 | | [EventAPI](https://github.com/Lenni0451/EventAPI) (MinimalEventManager) | avgt | 4 | 1327251,543 | 55359,321 | ns/op | 144 | | [EventAPI](https://github.com/Lenni0451/EventAPI) (ASMEventManager) | avgt | 4 | 1399196,527 | 55170,229 | ns/op | 145 | | [LambdaEvents](https://github.com/Lenni0451/LambdaEvents) (Method Handles) | avgt | 4 | 1495215,777 | 6336,098 | ns/op | 146 | | [DietrichEvents](https://github.com/FlorianMichael/DietrichEvents) | avgt | 4 | 1497398,449 | 128637,113 | ns/op | 147 | | [DarkMagician6](https://bitbucket.org/DarkMagician6/eventapi/src/master/) | avgt | 4 | 1870394,664 | 59848,353 | ns/op | 148 | | [LambdaEvents](https://github.com/Lenni0451/LambdaEvents) (Reflection) | avgt | 4 | 2169950,957 | 50804,811 | ns/op | 149 | | [EventAPI](https://github.com/Lenni0451/EventAPI) (InjectionEventManager) | avgt | 4 | 3832627,003 | 73982,528 | ns/op | 150 | | [EventAPI](https://github.com/Lenni0451/EventAPI) (EventManager) | avgt | 4 | 5963389,296 | 956568,797 | ns/op | 151 | | [Cydhra](https://github.com/Cydhra/EventSystem/tree/master) (Event System) | avgt | 4 | 7357398,698 | 92774,097 | ns/op | 152 | | [Guava](https://github.com/google/guava) | avgt | 4 | 11945301,707 | 239846,530 | ns/op | 153 | 154 | Date of the benchmark: 2023-07-17 155 | 156 | *This table is not meant to put others down, but simply to support the main message of this event system.* 157 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /src/main/java/de/florianmichael/dietrichevents2/DietrichEvents2.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 java.util.function.Consumer; 21 | 22 | /** 23 | * This class is the main class of DietrichEvents2. 24 | */ 25 | public class DietrichEvents2 { 26 | 27 | /** 28 | * The global instance of DietrichEvents2. 29 | */ 30 | private static final DietrichEvents2 GLOBAL = new DietrichEvents2(32, Throwable::printStackTrace); 31 | 32 | public static DietrichEvents2 global() { 33 | return GLOBAL; 34 | } 35 | 36 | /** 37 | * The subscribers of the event. 38 | */ 39 | private Object[][] subscribers; 40 | private int[][] priorities; 41 | 42 | /** 43 | * The errorHandler consumer will be called when an exception is thrown in a subscriber. 44 | */ 45 | public Consumer errorHandler; 46 | 47 | /** 48 | * Creates a new instance of DietrichEvents2. The eventCapacity parameter is the default size of the array that stores all subscribers. 49 | * 50 | * @param eventCapacity The default size of the array that stores all subscribers. 51 | */ 52 | public DietrichEvents2(final int eventCapacity) { 53 | this(eventCapacity, Throwable::printStackTrace); 54 | } 55 | 56 | /** 57 | * Creates a new instance of DietrichEvents2. The maxEvents parameter is the default size of the array that stores all subscribers. 58 | * 59 | * @param eventCapacity The default size of the array that stores all subscribers. 60 | * @param errorHandler The errorHandler consumer will be called when an exception is thrown in a subscriber. 61 | * @throws IllegalArgumentException If the eventCapacity is less than 1 or the errorHandler is null. 62 | */ 63 | public DietrichEvents2(final int eventCapacity, final Consumer errorHandler) { 64 | if (eventCapacity < 1) { 65 | throw new IllegalArgumentException("Event capacity must be at least 1"); 66 | } 67 | this.subscribers = new Object[eventCapacity][0]; 68 | this.priorities = new int[eventCapacity][0]; 69 | 70 | if (errorHandler == null) { 71 | throw new IllegalArgumentException("Error handler must not be null"); 72 | } 73 | this.errorHandler = errorHandler; 74 | } 75 | 76 | /** 77 | * @param id The id of the event. 78 | * @return Whether the event has subscribers. 79 | */ 80 | public boolean hasSubscriber(final int id) { 81 | return subscribers[id].length > 0; 82 | } 83 | 84 | /** 85 | * @param id The id of the event. 86 | * @return The subscribers of the event, if there are no subscribers null, will be returned. 87 | */ 88 | public Object[] getSubscribers(final int id) { 89 | if (!hasSubscriber(id)) { 90 | return null; 91 | } 92 | return subscribers[id]; 93 | } 94 | 95 | /** 96 | * @param id The id of the event. 97 | * @param object The object to check. 98 | * @return Whether the object is a subscriber of the event. 99 | */ 100 | public boolean isSubscriber(final int id, final Object object) { 101 | final Object[] subscriberArr = subscribers[id]; 102 | for (Object o : subscriberArr) { 103 | if (o == object) { 104 | return true; 105 | } 106 | } 107 | return false; 108 | } 109 | 110 | /** 111 | * Internal method that automatically resizes the array with all subscribers, 112 | * this method should never be called simply because the event system calls it itself. 113 | * 114 | * @param eventCapacity The new maximum length of the array. 115 | * @throws IllegalArgumentException If the eventCapacity is less than 1. 116 | */ 117 | public void setEventCapacity(final int eventCapacity) { 118 | if (eventCapacity < 1) { 119 | throw new IllegalArgumentException("Event capacity must be at least 1"); 120 | } 121 | final Object[][] subscribers = this.subscribers; 122 | final int[][] priorities = this.priorities; 123 | 124 | // Create new arrays 125 | this.subscribers = new Object[eventCapacity][0]; 126 | this.priorities = new int[eventCapacity][0]; 127 | 128 | // Fill old arrays into new arrays 129 | for (int i = 0; i < subscribers.length; i++) { 130 | this.subscribers[i] = subscribers[i]; 131 | this.priorities[i] = priorities[i]; 132 | } 133 | } 134 | 135 | // --------------------------------------------------------------------------- 136 | 137 | /** 138 | * Subscribes a listener with all given IDs to the given class, can be called multiple times. 139 | * 140 | * @param object The object to subscribe. 141 | * @param ids The ids of the events. 142 | */ 143 | public void subscribe(final Object object, final int... ids) { 144 | subscribe(object, 0, ids); 145 | } 146 | 147 | /** 148 | * Subscribes a listener with all given IDs to the given class, can be called multiple times. 149 | * 150 | * @param object The object to subscribe. 151 | * @param priority The priority of the subscriber. 152 | * @param ids The ids of the events. 153 | */ 154 | public void subscribe(final Object object, final int priority, final int[] ids) { 155 | for (int id : ids) { 156 | subscribe(id, object, priority); 157 | } 158 | } 159 | 160 | /** 161 | * Subscribes a listener with the given ID to the given class, can be called multiple times. 162 | * 163 | * @param id The id of the event. 164 | * @param object The object to subscribe. 165 | */ 166 | public void subscribe(final int id, final Object object) { 167 | subscribe(id, object, 0); 168 | } 169 | 170 | /** 171 | * Subscribes a listener with the given ID to the given class, can be called multiple times. 172 | * For priorities see {@link Priorities}. 173 | * The higher a priority is, the earlier an event is called. 174 | * 175 | * @param id The id of the event. 176 | * @param object The object to subscribe. 177 | * @param priority The priority of the subscriber. 178 | */ 179 | public void subscribe(final int id, final Object object, final int priority) { 180 | if (subscribers.length <= id) setEventCapacity(id + 1); // Resize event capacity if needed 181 | 182 | final Object[] subscriberArr = subscribers[id]; 183 | final int[] priorityArr = priorities[id]; 184 | 185 | int insertionIndex = subscriberArr.length; 186 | for (int i = 0; i < subscriberArr.length; i++) { 187 | if (priorityArr[i] > priority) { 188 | insertionIndex = i; 189 | break; 190 | } 191 | } 192 | 193 | final Object[] newSubscriberArr = new Object[subscriberArr.length + 1]; 194 | final int[] newPriorityArr = new int[subscriberArr.length + 1]; 195 | 196 | // before index 197 | System.arraycopy(priorityArr, 0, newPriorityArr, 0, insertionIndex); 198 | System.arraycopy(subscriberArr, 0, newSubscriberArr, 0, insertionIndex); 199 | 200 | // index 201 | newPriorityArr[insertionIndex] = priority; 202 | newSubscriberArr[insertionIndex] = object; 203 | 204 | // after index 205 | System.arraycopy(priorityArr, insertionIndex, newPriorityArr, insertionIndex + 1, priorityArr.length - insertionIndex); 206 | System.arraycopy(subscriberArr, insertionIndex, newSubscriberArr, insertionIndex + 1, subscriberArr.length - insertionIndex); 207 | 208 | subscribers[id] = newSubscriberArr; 209 | priorities[id] = newPriorityArr; 210 | } 211 | 212 | public void unsubscribe(final Object object, final int... ids) { 213 | for (int id : ids) { 214 | unsubscribe(id, object); 215 | } 216 | } 217 | 218 | /** 219 | * Unsubscribes a listener with the given ID from the given class, can be called multiple times. 220 | * 221 | * @param id The id of the event. 222 | * @param object The object to unsubscribe. 223 | */ 224 | public void unsubscribe(final int id, final Object object) { 225 | Object[] subscriberArr = subscribers[id]; 226 | int[] priorityArr = priorities[id]; 227 | 228 | int removeIndex = -1; 229 | for (int i = 0; i < subscriberArr.length; i++) { 230 | if (subscriberArr[i] == object) { 231 | removeIndex = i; 232 | break; 233 | } 234 | } 235 | 236 | if (removeIndex == -1) return; 237 | 238 | Object[] newSubscriberArr = new Object[subscriberArr.length - 1]; 239 | int[] newPriorityArr = new int[subscriberArr.length - 1]; 240 | 241 | if (removeIndex > 0) { 242 | System.arraycopy(subscriberArr, 0, newSubscriberArr, 0, removeIndex); 243 | System.arraycopy(priorityArr, 0, newPriorityArr, 0, removeIndex); 244 | } 245 | System.arraycopy(subscriberArr, removeIndex + 1, newSubscriberArr, removeIndex, subscriberArr.length - removeIndex - 1); 246 | System.arraycopy(priorityArr, removeIndex + 1, newPriorityArr, removeIndex, subscriberArr.length - removeIndex - 1); 247 | 248 | subscribers[id] = newSubscriberArr; 249 | priorities[id] = newPriorityArr; 250 | } 251 | 252 | /** 253 | * Unsubscribes all listeners from the given type. 254 | * 255 | * @param id The id of the event. 256 | */ 257 | public void unsubscribeAll(final int id) { 258 | subscribers[id] = new Object[0]; 259 | priorities[id] = new int[0]; 260 | } 261 | 262 | // --------------------------------------------------------------------------- 263 | 264 | /** 265 | * Calls an event and takes care of any exceptions that might be thrown by calling the {@link #errorHandler} 266 | * 267 | * @param id The id of the event. 268 | * @param event The event to call. 269 | */ 270 | public void call(final int id, final AbstractEvent event) { 271 | if (subscribers.length > id) { 272 | try { 273 | callUnsafe(id, event); 274 | } catch (final Throwable t) { 275 | this.errorHandler.accept(t); 276 | } 277 | } 278 | } 279 | 280 | 281 | /** 282 | * Calls an event without taking care of error handling or capacity. This method should only be used in an environment 283 | * where you know that everything is set up correctly, and you really care about performance. 284 | * 285 | * @param id The id of the event. 286 | * @param event The event to call. 287 | */ 288 | public void callUnsafe(final int id, final AbstractEvent event) { 289 | final Object[] subscriber = subscribers[id]; 290 | 291 | for (Object o : subscriber) { 292 | event.call(o); 293 | } 294 | } 295 | 296 | 297 | /** 298 | * Calls an event and handles the {@link BreakableException} by breaking the loop. 299 | * 300 | * @param id The id of the event. 301 | * @param event The event to call. 302 | */ 303 | public void callBreakable(final int id, final AbstractEvent event) { 304 | if (subscribers.length <= id) { 305 | return; 306 | } 307 | final Object[] subscriber = subscribers[id]; 308 | for (Object o : subscriber) { 309 | try { 310 | event.call(o); 311 | } catch (final Throwable t) { 312 | if (t instanceof BreakableException) { 313 | break; 314 | } 315 | this.errorHandler.accept(t); 316 | } 317 | } 318 | } 319 | 320 | /** 321 | * Calls an event and passes any exceptions to the caller. 322 | * 323 | * @param id The id of the event. 324 | * @param event The event to call. 325 | */ 326 | public void callExceptionally(final int id, final AbstractEvent event) { 327 | if (subscribers.length > id) { 328 | callUnsafe(id, event); 329 | } 330 | } 331 | 332 | // --------------------------------------------------------------------------- 333 | // Deprecated methods 334 | 335 | @Deprecated 336 | public void post(final int id, final AbstractEvent event) { 337 | if (subscribers.length <= id) { 338 | setEventCapacity(id + 1); 339 | return; 340 | } 341 | call(id, event); 342 | } 343 | 344 | @Deprecated 345 | public void postInternal(final int id, final AbstractEvent event) { 346 | callUnsafe(id, event); 347 | } 348 | 349 | } 350 | --------------------------------------------------------------------------------