package de.schlegel11.eventdispatcher;

import com.google.common.collect.Sets;

import java.util.EventListener;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * Created by schlegel11 on 20.10.14.
 */
final class EventDispatcherList {

    public static final EventDispatcherList PSEUDO_EMPTY_DISPATCHER_LIST = new EventDispatcherList();
    private final Set<EventListenerWrapper> listeners = Sets.newHashSet();

    public boolean addListener(EventListener listener, int numberOfCalls) {
        return listeners.add(new EventListenerWrapper(listener, numberOfCalls));
    }

    public boolean removeListener(EventListener listener) {
        return listeners.removeIf(elw -> elw.getListener() == listener);
    }

    public int getListenerCount() {
        return listeners.size();
    }

    public int getListenerMaxCalls(EventListener listener) {
        EventListenerWrapper elw = getElw(listener);
        return Objects.isNull(elw) ? 0 : elw.getMaxCalls();
    }

    public int getListenerCurrentCalls(EventListener listener) {
        EventListenerWrapper elw = getElw(listener);
        return Objects.isNull(elw) ? 0 : elw.getCurrentCalls();
    }

    public boolean isEmpty() {
        return listeners.isEmpty();
    }

    public void fireEvent(Consumer<EventListener> consumer) {
        for (EventListenerWrapper elw : listeners) {
            elw.addCurrentCalls(elw.getMaxCalls() == EventListenerWrapper.INFINITE_CALLS ? 0 : 1);
            consumer.accept(elw.getListener());
        }
        listeners.removeIf(elw -> elw.getMaxCalls() == elw.getCurrentCalls());
    }

    @Override
    public int hashCode() {
        return Objects.hash(listeners);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        final EventDispatcherList other = (EventDispatcherList) obj;
        return Objects.equals(this.listeners, other.listeners);
    }

    private EventListenerWrapper getElw(EventListener listener) {
        Stream<EventListenerWrapper> stream = listeners.stream().filter(elw -> elw.getListener() == listener);
        Optional<EventListenerWrapper> optional = stream.findFirst();
        return optional.isPresent() ? optional.get() : null;
    }
}

@@ -531,32 +531,38 @@
 ean addListener(
+final 
 EventListener li
@@ -568,16 +568,22 @@
 istener,
+ final
  int num
@@ -718,16 +718,22 @@
 istener(
+final 
 EventLis
@@ -937,24 +937,30 @@
 nerMaxCalls(
+final 
 EventListene
@@ -1044,34 +1044,8 @@
 turn
- Objects.isNull(elw) ? 0 :
  elw
@@ -1106,16 +1106,22 @@
 ntCalls(
+final 
 EventLis
@@ -1209,34 +1209,8 @@
 turn
- Objects.isNull(elw) ? 0 :
  elw
@@ -1336,16 +1336,22 @@
 reEvent(
+final 
 Consumer
@@ -1803,16 +1803,22 @@
  equals(
+final 
 Object o
@@ -2160,16 +2160,22 @@
  getElw(
+final 
 EventLis
@@ -2315,129 +2315,72 @@
     
-Optional%3CEventListenerWrapper%3E optional = stream.findFirst();%0A        return optional.isPresent() ? optional.get() : null
+return stream.findFirst().orElse(EventListenerWrapper.DUMMY_ELW)
 ;%0A  

Add final modifier to method arguments. Changed getElw method to not return null.