This file contains hidden or 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
| public class Messengers implements ForwardingStream<Rule> { | |
| private final List<Rule> messengerList; | |
| public Messengers(List<Rule> messengers) { | |
| this.messengerList = messengers; | |
| } | |
| @Override | |
| public Stream<Rule> getStream() { | |
| return this.messengerList.stream(); |
This file contains hidden or 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
| public interface Rule { | |
| OptionalMessenger matches(final String type, final String message); | |
| String getType(); | |
| } | |
| public interface Action { | |
| void send(String message); | |
| } |
This file contains hidden or 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
| public interface Messenger { | |
| void send(String message); | |
| boolean matches(final String type, final String message); | |
| String getType(); | |
| } |
This file contains hidden or 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
| public class MessengerClient { | |
| public static void main(String[] args) { | |
| String type = args[0]; | |
| String message = args[1]; | |
| getMessengers().findMatchingMessenger(type, message).send(type, message); | |
| } | |
| private static Messengers getMessengers() { |
This file contains hidden or 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
| public class OptionalMessenger { | |
| private final Optional<Messenger> messenger; | |
| private OptionalMessenger(Optional<Messenger> messenger) { | |
| this.messenger = messenger; | |
| } | |
| public static OptionalMessenger of(Messenger messenger) { | |
| return new OptionalMessenger(Optional.of(messenger)); | |
| } |
This file contains hidden or 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
| public class Messengers implements ForwardingStream<Messenger> { | |
| private final List<Messenger> messengerList; | |
| public Messengers(List<Messenger> messengers) { | |
| this.messengerList = messengers; | |
| } | |
| @Override | |
| public Stream<Messenger> getStream() { | |
| return this.messengerList.stream(); |
This file contains hidden or 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
| getMessengers().stream() | |
| .filter(messenger -> messenger.matches(type, message)) | |
| .findFirst() | |
| .orElseThrow( | |
| () -> | |
| new MessageSendFailed( | |
| String.format( | |
| "Your message has invalid text or messenger '%s' or not supported.", type))) | |
| .send(message); |
This file contains hidden or 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
| public class Admin implements Messenger { | |
| @Override | |
| public boolean matches(final String type, final String message) { | |
| return getType().equalsIgnoreCase(type) && isValid(message); | |
| } | |
| @Override | |
| public String getType() { | |
| return "admin"; |
This file contains hidden or 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
| public interface Messenger { | |
| void send(String message); | |
| boolean matches(final String type,final String message); | |
| } | |
| public class Hangout implements Messenger { | |
| @Override | |
| public boolean matches(final String type, final String message) { |
This file contains hidden or 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
| public static void main(String[] args) { | |
| String type = args[0]; | |
| String message = args[1]; | |
| getMessengers().stream() | |
| .filter(messenger -> messenger.matches(type, message)) | |
| .findFirst() | |
| .orElseThrow( | |
| () -> | |
| new MessageSendFailed( |