Last active
July 30, 2017 18:27
-
-
Save nachivpn/93bb680a091d62023f39a0614b8614bb to your computer and use it in GitHub Desktop.
getComponent.java
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
Map<String,Component> components; | |
public interface Component{ | |
String prettyResult(); | |
String getId(); | |
} | |
//returns component if available, exception otherwise | |
public Either<Exception, Component> getComponent(String componentName){ | |
Optional<Component> optionalComponent = Optional.ofNullable(components.get(componentName)); | |
return optionalComponent | |
//component available, return it using Right | |
.<Either<Exception,Component>>map(c -> new Right<>(c)) | |
//component not avilable, return exception using Left | |
.orElse(new Left<>(new Exception("Component not found: " + componentName))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment