Skip to content

Commit

Permalink
Don't resize array on call, tidy up things
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed May 20, 2024
1 parent 78defe9 commit a7fa5b9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,15 @@ public class Test implements ExampleListener {
````java
// There are multiple call methods which can be used depending on the situation:

// - callUnsafe() -> Calls the event without any error handling or
// internal resizing (DietrichEvents2#global() won't support more than
// 32 listeners with this method)
// - callUnsafe() -> Calls the event without any sanity

// - call() -> Calls the event with error handling and internal resizing (recommended)
// - callExceptionally() -> Calls the event without error handling

// - call() -> Calls the event with global error handling

// - callBreakable() -> Calls the event with error handling for every listener and supports
// BreakableException to be thrown (also does resize)

// - callExceptionally() -> Calls the event with internal resizing but without error handling

// There is also a post() method which uses the error handler (try catch)
DietrichEvents2.global().call(ExampleEvent.ID, new ExampleEvent("Hello World!"));
````

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public void setEventCapacity(final int eventCapacity) {
}
}

// ---------------------------------------------------------------------------

/**
* Subscribes a listener with all given IDs to the given class, can be called multiple times.
*
Expand Down Expand Up @@ -257,14 +259,16 @@ public void unsubscribeAll(final int id) {
priorities[id] = new int[0];
}

// ---------------------------------------------------------------------------

/**
* Calls an event and takes care of any exceptions that might be thrown by calling the {@link #errorHandler}
*
* @param id The id of the event.
* @param event The event to call.
*/
public void call(final int id, final AbstractEvent event) {
if (assertArraySize(id)) {
if (subscribers.length > id) {
try {
callUnsafe(id, event);
} catch (final Throwable t) {
Expand All @@ -273,17 +277,33 @@ public void call(final int id, final AbstractEvent event) {
}
}


/**
* Calls an event without taking care of error handling or capacity. This method should only be used in an environment
* where you know that everything is set up correctly, and you really care about performance.
*
* @param id The id of the event.
* @param event The event to call.
*/
public void callUnsafe(final int id, final AbstractEvent event) {
final Object[] subscriber = subscribers[id];

for (int i = 0; i < subscriber.length; i++) {
event.call(subscriber[i]);
}
}


/**
* Calls an event and handles the {@link BreakableException} by breaking the loop.
*
* @param id The id of the event.
* @param event The event to call.
*/
public void callBreakable(final int id, final AbstractEvent event) {
if (!assertArraySize(id)) {
if (subscribers.length <= id) {
return;
}

final Object[] subscriber = subscribers[id];
for (Object o : subscriber) {
try {
Expand All @@ -304,35 +324,20 @@ public void callBreakable(final int id, final AbstractEvent event) {
* @param event The event to call.
*/
public void callExceptionally(final int id, final AbstractEvent event) {
if (assertArraySize(id)) {
if (subscribers.length > id) {
callUnsafe(id, event);
}
}

/**
* Calls an event without taking care of error handling or capacity resizing. This method should not be used normally.
*
* @param id The id of the event.
* @param event The event to call.
*/
public void callUnsafe(final int id, final AbstractEvent event) {
final Object[] subscriber = subscribers[id];
// ---------------------------------------------------------------------------
// Deprecated methods

for (int i = 0; i < subscriber.length; i++) {
event.call(subscriber[i]);
}
}

private boolean assertArraySize(final int id) {
@Deprecated
public void post(final int id, final AbstractEvent event) {
if (subscribers.length <= id) {
setEventCapacity(id + 1);
return false;
return;
}
return true;
}

@Deprecated
public void post(final int id, final AbstractEvent event) {
call(id, event);
}

Expand Down

0 comments on commit a7fa5b9

Please sign in to comment.