https://github.com/osiris-team/easy-java-events
EJE provides accessible methods for handling events/actions/listeners.
https://github.com/osiris-team/easy-java-events
Last synced: over 1 year ago
JSON representation
EJE provides accessible methods for handling events/actions/listeners.
- Host: GitHub
- URL: https://github.com/osiris-team/easy-java-events
- Owner: Osiris-Team
- License: mit
- Created: 2022-03-17T15:50:38.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-10-06T12:00:42.000Z (almost 3 years ago)
- Last Synced: 2025-02-28T13:31:14.574Z (over 1 year ago)
- Language: Java
- Homepage:
- Size: 72.3 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Easy-Java-Events
EJE provides accessible methods for handling events/actions/listeners.
Add this as dependency to your project via
[Maven/Gradle/Sbt/Leinigen](https://jitpack.io/#Osiris-Team/Easy-Java-Events/LATEST) (requires Java 8 or higher).
#### Learn the basics in 10 seconds:
```java
Event onValueChanged = new Event<>();
onValueChanged.addAction(value -> {
System.out.println("New value: "+value);
});
onValueChanged.execute(10); // Prints out "New value: 10"
onValueChanged.execute(5); // Prints out "New value: 5"
```
#### Learn everything in 5 minutes:
```java
class EventTest {
@Test
void example() {
Event onValueChanged = new Event<>();
Action myAction = onValueChanged.addAction(value -> { // Stays in memory and gets executed every time.
System.out.println("New value: "+value);
// You can throw exceptions in here
}, Exception::printStackTrace); // and catch them here.
onValueChanged.execute(10); // Prints out "New value: 10"
onValueChanged.execute(5); // Prints out "New value: 5"
// One time actions that only get executed once, are also supported:
onValueChanged.addOneTimeAction(value -> {
System.out.println("New value: "+value+" bye!");
}, Exception::printStackTrace);
onValueChanged.execute(7); // Prints out "New value: 7" and "New value: 7 bye!"
// If more and more actions get added over time
// you must remove unused actions. There are some util methods for this case.
// First we create a new action with additional params: isOneTime=false and object=null.
Action actionToRemove = onValueChanged.addAction((action, value) -> {
System.out.println("New value: "+value+", but I will be gone soon :/");
}, Exception::printStackTrace, false, null);
// Then we initialise the cleaner thread for this event, which checks
// its actions list every 100ms for actions that
// fulfill the condition "object != null" and removes those.
onValueChanged.initCleaner(100, object -> object != null, Exception::printStackTrace);
// Once we want to remove the action, we simply give it an object that is not null.
// The cleaner then removes it in the next check.
actionToRemove.object = new Object(); // Note that you can store any type of object here.
}
}