Can someone help me with the below piece of code? I would like an equivalent using Optional functions.
public String getMyRequiredValue(Optional<String> value) {
if(value.isPresent()) {
Optional<String> optionVal = getAnotherValue(value.get());
if(optionVal.isPresent()) {
return optionVal.get();
} else {
return null;
}
} else {
return "Random";
}
}
public Optional<String> getAnotherValue(String value) { ... }
Just a note I tried this, but it does not work
return value.map(lang -> getAnotherValue(lang).orElse(null)).orElse("Random");
The thing that is not working is - when the value is present and getAnotherValue returns Optional.empty() I want the original function to return null. It is returning "Random" right now.
My assumption is since the map method returns null it gets replaced by "Random".
Note that the original code was written by someone else. Since, it has a lot of dependencies, I cannot change the input/output parameters. :(