Skip to content

Commit

Permalink
event factory fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Mar 12, 2024
1 parent 54b76ec commit 902cc01
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.base.Preconditions;
import dev.upcraft.sparkweave.api.event.Event;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Function;
Expand All @@ -16,12 +17,13 @@ public class EventFactoryImpl<T> implements Event<T> {

private final Class<T> type;
private final Function<T[], T> invokerFactory;
private Object[] listeners = new Object[0];
private T[] listeners;
private T invoker;

private EventFactoryImpl(Class<T> type, Function<T[], T> invokerFactory) {
this.type = type;
this.invokerFactory = invokerFactory;
this.listeners = makeArray(0);
setupInvoker();
}

Expand Down Expand Up @@ -53,14 +55,18 @@ public void unregister(T listener) {
}
}

listeners = Arrays.stream(listeners).filter(Objects::nonNull).toArray();
listeners = Arrays.stream(listeners).filter(Objects::nonNull).toArray(this::makeArray);

setupInvoker();
}

@SuppressWarnings("unchecked")
private void setupInvoker() {
invoker = invokerFactory.apply((T[]) listeners);
invoker = invokerFactory.apply(listeners);
}

@SuppressWarnings("unchecked")
private T[] makeArray(int size) {
return (T[]) Array.newInstance(type, size);
}

@Override
Expand Down

0 comments on commit 902cc01

Please sign in to comment.