Created
August 14, 2016 13:20
-
-
Save afcastano/1b3ee98f7bae8b1547f9d149d5508d5b to your computer and use it in GitHub Desktop.
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
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