Skip to content

Instantly share code, notes, and snippets.

@nachivpn
Last active July 30, 2017 18:27
Show Gist options
  • Save nachivpn/93bb680a091d62023f39a0614b8614bb to your computer and use it in GitHub Desktop.
Save nachivpn/93bb680a091d62023f39a0614b8614bb to your computer and use it in GitHub Desktop.
getComponent.java
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