Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@afcastano
Created August 14, 2016 13:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afcastano/1b3ee98f7bae8b1547f9d149d5508d5b to your computer and use it in GitHub Desktop.
Save afcastano/1b3ee98f7bae8b1547f9d149d5508d5b to your computer and use it in GitHub Desktop.
public abstract Result<Integer> readIntFromFile(String file);
public Result<Integer> adjustValue(Integer value) {
if (value > 5) {
Result.error("Value " + value + " should no be greater than 5");
}
return Result.ok(5 - value);
}
public Double calculateAverage(Integer val1, Integer val2) {
return (val1 + val2)/2d;
}
/**
* Read ints from two files, Adjust the first one and then calculate average.
* Returns a Result wrapping the positive outcome or any error.
*/
public Result<Double> businessOperation(String fileName, String fileName2) {
Result<Integer> adjustedValue = readIntFromFile(fileName)
.flatMap(this::adjustValue)
return adjustedValue.flatMap( val1 ->
readIntFromFile(fileName2).flatMap( val2 ->
Result.ok(calculateAverage(val1, val2))
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment