mmm-event

Java module providing the event pattern as generic reusable API and implementation.

License

License

GroupId

GroupId

io.github.m-m-m
ArtifactId

ArtifactId

mmm-event
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

mmm-event
Java module providing the event pattern as generic reusable API and implementation.
Project URL

Project URL

https://m-m-m.github.io/
Project Organization

Project Organization

mmm-team
Source Code Management

Source Code Management

https://github.com/m-m-m/event/tree/master

Download mmm-event

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.m-m-m/mmm-event/ -->
<dependency>
    <groupId>io.github.m-m-m</groupId>
    <artifactId>mmm-event</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.m-m-m/mmm-event/
implementation 'io.github.m-m-m:mmm-event:0.2.0'
// https://jarcasting.com/artifacts/io.github.m-m-m/mmm-event/
implementation ("io.github.m-m-m:mmm-event:0.2.0")
'io.github.m-m-m:mmm-event:jar:0.2.0'
<dependency org="io.github.m-m-m" name="mmm-event" rev="0.2.0">
  <artifact name="mmm-event" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.m-m-m', module='mmm-event', version='0.2.0')
)
libraryDependencies += "io.github.m-m-m" % "mmm-event" % "0.2.0"
[io.github.m-m-m/mmm-event "0.2.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

logo

Apache License, Version 2.0 build-status

mmm-event

Maven Central base JavaDoc

The module io.github.mmm.event (artifactId mmm-event) provides a generic event infrastructure. Sending and receiving events is a very common things. This module implements eventing as a generic pattern and provides a simple but powerful API and base implementation that saves you from a lot of work. It is designed minimalistic and has no external dependencies.

Features

This library offers the following features:

  • Highly optimized

    e.g. if you want to create many objects that can have event listeners. Maybe lots of them do not have any listener registered. Hence, the shall not allocate expensive List instances and sending events should be a no-op.

  • Highly customizable

    Any object can be an event. You are free to create your own custom event or send existing types (event String) as event. You create your own custom listener event and simply derive it from the EventListener<E> interface from this library, where you bind <E> to your custom event. See examples below for further details.

  • Prevent Memory Leaks

    You may create objects that have an event listener registered to some event source. Now, the event source typically holds a strong reference to that listener preventing it from being garbage collected. So if you do not remove the listener when the owning object is disposed, you quickly end up with memory leaks. This library allows to add an event listener such that internally a WeakReference is used preventing memory leaks without additional programming effort.

  • Single and Multi-Threaded

    The library provides implementations optimized for different usage scenarios. You may have a simple single-threaded use-case where you want the best performance with no overhead or you may need concurrency and thread-safeness.

  • Event Bus

    For eventing between loosely coupled components you may want to use the EventBus that acts as a central hub where listeners are registered and events are send. This way a component only needs to subscribe for a particular type of event without knowing who the sender(s) of such events actually are.

Usage

Maven Dependency:

<dependency>
  <groupId>io.github.m-m-m</groupId>
  <artifactId>mmm-event</artifactId>
</dependency>

Module Dependency:

  requires transitive io.github.mmm.event;

Example

Create your event as interface, class, or enum:

public class MyEvent {

  private final String message;

  public MyEvent(String message) {
    this.message = message;
  }

  public String getMessage() {
    return this.message;
  }
}

Create your event listener interface:

@FunctionalInterface
public interface MyEventListener extends EventListener<MyEvent> {
}

Create your component capable to send events:

public interface MyComponent extends EventSource<MyEvent, MyEventListener> {
   void doSomething();
}
public class MyComponentImpl extends AbstractEventSource<MyEvent, MyEventListener> implements MyComponent {
  public void doSomething() {
    fireEvent(new MyEvent("Hello World"));
  }
}

Putting it all together:

public class MyDemo {

  public static void main(String[] args) {
    MyComponent component = new MyComponentImpl();
    component.addListener((e) -> System.out.println(e.getMessage()));
    component.doSomething();
  }
}
io.github.m-m-m

Marvelous-Micro-Modules

Versions

Version
0.2.0
0.1.1
0.1.0