Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
enforce topic type declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Jul 30, 2022
1 parent 170ce5a commit 21baddc
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 197 deletions.
30 changes: 0 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,3 @@ class Listenable {
For more explanation, check
the [example](https://github.com/nothub/TinyEventBus/blob/master/src/test/java/lol/hub/tinyeventbus/example/Example.java)
.

---

###### reference lambdas

Reference lambdas invoking a function, being derived by an interface implemented by an extended superclass of the event
tend to cause type confusion.

The following example will not work as intended:

```java
abstract class CancelableEvent implements Cancelable {
...

class SomeEvent extends CancelableEvent {
...

Sub<SomeEvent> sub = Sub.of(SomeEvent::cancel);
```

To ensure the correctness of the event type, use one of the following patterns:

```java
Sub<SomeEvent> sub=Sub.of(e->e.cancel());
Sub<SomeEvent> sub=Sub.of(SomeEvent::cancel,SomeEvent.class);
```

For more information please
check [this example](https://github.com/nothub/TinyEventBus/blob/master/src/test/java/lol/hub/tinyeventbus/example/RefExample.java)
and [this explanation](https://github.com/jhalterman/typetools/issues/42).
4 changes: 2 additions & 2 deletions src/main/java/lol/hub/tinyeventbus/Bus.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void pub(Object event) {
*/
public void reg(Sub<?> sub) {
subs
.computeIfAbsent(sub.eventType, clazz -> new ConcurrentSkipListSet<>())
.computeIfAbsent(sub.topic, clazz -> new ConcurrentSkipListSet<>())
.add(sub);
}

Expand All @@ -101,7 +101,7 @@ public void reg(Object parent) {
* @see #unreg(Object)
*/
public void unreg(Sub<?> sub) {
final ConcurrentSkipListSet<Sub<?>> subs = this.subs.get(sub.eventType);
final ConcurrentSkipListSet<Sub<?>> subs = this.subs.get(sub.topic);
if (subs == null) return;
subs.remove(sub);
}
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/lol/hub/tinyeventbus/Sub.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,58 @@ public class Sub<T> implements Comparable<Sub<T>> {
*/
public final Consumer<T> consumer;

final Class<?> eventType;
final Class<?> topic;

/**
* Creates a {@link Sub} instance with manually defined event type.
*
* @param consumer Consumer to be invoked when an event is received.
* @param priority Priority of processing.
* @param eventType Manually defined type of event to be subscribed to.
* @see Sub#of(Consumer, int, Class)
* @param topic Manually defined type of event to be subscribed to.
* @see Sub#of(Class, Consumer, int)
*/
public Sub(Consumer<T> consumer, int priority, Class<?> eventType) {
public Sub(Class<?> topic, Consumer<T> consumer, int priority) {
this.priority = priority;
this.consumer = consumer;
this.eventType = eventType;
this.topic = topic;
}

/**
* Creates a {@link Sub} instance with manually defined event type.
*
* @param consumer Consumer to be invoked when an event is received.
* @param eventType Manually defined type of event to be subscribed to.
* @see Sub#of(Consumer, Class)
* @param topic Manually defined type of event to be subscribed to.
* @see Sub#of(Class, Consumer)
*/
public Sub(Consumer<T> consumer, Class<?> eventType) {
this(consumer, 0, eventType);
public Sub(Class<?> topic, Consumer<T> consumer) {
this(topic, consumer, 0);
}

/**
* Convenience method to create a {@link Sub} instance.
*
* @param consumer Consumer to be invoked when an event is received.
* @param priority Priority of processing.
* @param eventType Manually defined type of event to be subscribed to.
* @param topic Manually defined type of event to be subscribed to.
* @param <T> Generically defined type of event to be subscribed to.
* @return Created {@link Sub} instance.
* @see Sub#Sub(Consumer, int, Class)
* @see Sub#Sub(Class, Consumer, int)
*/
public static <T> Sub<T> of(Consumer<T> consumer, int priority, Class<?> eventType) {
return new Sub<>(consumer, priority, eventType);
public static <T> Sub<T> of(Class<?> topic, Consumer<T> consumer, int priority ) {
return new Sub<>(topic, consumer, priority );
}

/**
* Convenience method to create a {@link Sub} instance.
*
* @param consumer Consumer to be invoked when an event is received.
* @param eventType Manually defined type of event to be subscribed to.
* @param topic Manually defined type of event to be subscribed to.
* @param <T> Generically defined type of event to be subscribed to.
* @return Created {@link Sub} instance.
* @see Sub#Sub(Consumer, Class)
* @see Sub#Sub(Class, Consumer)
*/
public static <T> Sub<T> of(Consumer<T> consumer, Class<?> eventType) {
return new Sub<>(consumer, eventType);
public static <T> Sub<T> of(Class<?> topic, Consumer<T> consumer ) {
return new Sub<>(topic, consumer );
}

void accept(Object event) {
Expand Down
20 changes: 9 additions & 11 deletions src/test/java/lol/hub/tinyeventbus/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ class Example {
// create event consumer
final Consumer<Event> eventConsumer = e -> System.out.println(e.str);
// create subscriber
Sub<Event> sub = new Sub<>(eventConsumer);

Sub<Event> sub = new Sub<>(Event.class, eventConsumer);
// create subscriber with inline consumer
Sub<Event> prioSub = new Sub<>(e -> {
Sub<Event> prioSub = new Sub<>(Event.class, e -> {
if (e.str.equals("foobar")) e.canceled = true;
}, 50); // priority is defined as integer

// create subscriber with convenience method
Sub<Event> highPrioSub = Sub.of(e -> e.str = ":3", 100); // higher priority comes first
Sub<Event> highPrioSub = Sub.of(Event.class, e -> e.str = ":3", 100); // higher priority comes first

void run() {

Expand All @@ -42,12 +40,6 @@ void run() {

}

static class Main {
public static void main(String[] args) {
new Example().run();
}
}

static class Event implements Cancelable {

String str;
Expand All @@ -63,4 +55,10 @@ public boolean isCanceled() {
}

}

static class Main {
public static void main(String[] args) {
new Example().run();
}
}
}
69 changes: 0 additions & 69 deletions src/test/java/lol/hub/tinyeventbus/example/RefExample.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package lol.hub.tinyeventbus.example;

import lol.hub.tinyeventbus.Bus;
import lol.hub.tinyeventbus.Cancelable;
import lol.hub.tinyeventbus.Sub;

class ReferenceLambdaExample {

Sub<TestEvent> consumer = Sub.of(TestEvent.class, TestEvent::cancel);

void run() {

// create bus instance
Bus bus = new Bus();

bus.reg(consumer);
final TestEvent evB = new TestEvent();
bus.pub(evB);

System.out.println(evB.canceled);
// prints: true

}

abstract static class CancelableEvent implements Cancelable {

boolean canceled;

CancelableEvent() {
}

public void cancel() {
this.canceled = true;
}

@Override
public boolean isCanceled() {
return canceled;
}

}

public static class TestEvent extends CancelableEvent {
}

static class Main {
public static void main(String[] args) {
new ReferenceLambdaExample().run();
}
}
}
6 changes: 3 additions & 3 deletions src/test/java/lol/hub/tinyeventbus/tests/BenchmarkTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void reg(int subs) {

List<Sub<String>> listenerContainers = new ArrayList<>();

IntStream.range(0, subs).forEach(i -> listenerContainers.add(new Sub<>(s -> {
IntStream.range(0, subs).forEach(i -> listenerContainers.add(new Sub<>(String.class, s -> {
})));

final long start = System.nanoTime();
Expand All @@ -83,7 +83,7 @@ void del(int subs) {

List<Sub<String>> listenerContainers = new ArrayList<>();

IntStream.range(0, subs).forEach(i -> listenerContainers.add(new Sub<>(s -> {
IntStream.range(0, subs).forEach(i -> listenerContainers.add(new Sub<>(String.class, s -> {
})));

IntStream
Expand Down Expand Up @@ -113,7 +113,7 @@ void pub(int pubs, int subs) {

List<Sub<String>> listenerContainers = new ArrayList<>();

IntStream.range(0, subs).forEach(i -> listenerContainers.add(new Sub<>(s -> hits++)));
IntStream.range(0, subs).forEach(i -> listenerContainers.add(new Sub<>(String.class, s -> hits++)));

IntStream
.range(0, subs)
Expand Down
Loading

0 comments on commit 21baddc

Please sign in to comment.