Last active
July 16, 2016 05:44
-
-
Save afcastano/92a35d4ddea5f8ad7fd346daa3f76886 to your computer and use it in GitHub Desktop.
Operation with nested optionals
This file contains 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
private abstract class Counter { | |
abstract Optional<Integer> colourCount(); | |
} | |
private abstract Optional<Counter> fetchThisMonthCounter(); | |
private abstract Optional<Counter> fetchPreviousMonthCounter(); | |
// ************ blah blah blah | |
// ... | |
public Optional<Integer> totalColourCount() { | |
Optional<Counter> thisMonth = fetchThisMonthCounter(); | |
Optional<Counter> previousMonth = fetchPreviousMonthCounter(); | |
if(thisMonth.isPresent() && thisMonth.get().colourCount().isPresent() | |
&& previousMonth.isPresent() && previousMonth.get().colourCount().isPresent()) { | |
Integer colour1 = thisMonth.get().colourCount().get(); | |
Integer colour2 = previousMonth.get().colourCount().get(); | |
return Optional.of(colour1 + colour2); | |
} | |
return Optional.empty(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment