com.github.edgar615.util.eventbus.HandlerRegistration Maven / Gradle / Ivy
package com.github.edgar615.util.eventbus;
import com.github.edgar615.util.event.Event;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
/**
* Created by Edgar on 2017/4/14.
*
* @author Edgar Date 2017/4/14
*/
public class HandlerRegistration {
private static final HandlerRegistration INSTANCE = new HandlerRegistration();
private final List bindings = new ArrayList<>();
private HandlerRegistration() {
}
public static HandlerRegistration instance() {
return INSTANCE;
}
public void registerHandler(BiPredicate predicate, EventHandler handler) {
HandlerBinding binding = new HandlerBinding(predicate, handler);
bindings.add(binding);
}
public List getHandlers(Event event) {
return bindings.stream()
.filter(b -> b.match(event))
.map(b -> b.eventHandler())
.collect(Collectors.toList());
}
}