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]; | |
| if ("slack".equals(type) && isValidForSlack(message)) { | |
| Slack slack = new Slack(); | |
| slack.send(message); | |
| } else if ("skype".equals(type) && isValidForSkype(message)) { | |
| Skype skype = new Skype(); | |
| skype.send(message); | |
| } else if ("hangout".equals(type) && isValidForHangout(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]; | |
| Messenger messanger; | |
| if ("slack".equals(type)) { | |
| messanger = new Slack(); | |
| } else if ("skype".equals(type)) { | |
| messanger = new Skype(); | |
| } else if ("hangout".equals(type)) { | |
| messanger = new Hangout(); |
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 Slack implements Messenger { | |
| @Override | |
| public void send(final String message) { | |
| if (isValid(message)) { | |
| SlackClient client = new SlackClient(); | |
| client.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 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( |
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 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
| 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 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
| 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 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() { |
OlderNewer