Created
September 21, 2015 19:43
-
-
Save bassemZohdy/dda6f46da282646c1bfb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.function.Consumer; | |
import java.util.function.Predicate; | |
public class ConsumingRouter<T> implements Consumer<T> { | |
private Map<Predicate<T>, Consumer<T>> map = new ConcurrentHashMap<>(); | |
public void sub(Predicate<T> p, Consumer<T> c) { | |
map.put(p, c); | |
} | |
@Override | |
public void accept(T t) { | |
map.entrySet().forEach(e -> { | |
if (e.getKey().test(t)) | |
e.getValue().accept(t); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment