Created
April 16, 2017 14:01
-
-
Save john77eipe/da5212b420da0ee7642f8ef6e410a7cc 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
//Vendor makes this change to utilize java 8 features but he faces this issue | |
public static <T extends Number> Optional<T> sum(List<T> list, Predicate<T> condition){ | |
//ability to add is only here | |
return list.parallelStream() | |
.filter(condition) | |
.map(i -> i) | |
.reduce((a,b)->a+b); //Compile error: Operator + is undefined for type T, T | |
} | |
//client | |
int result = MathUtility.sum(listOfInts, i->i<4).get(); | |
//one solution is to move the addition logic back to client | |
int result = MathUtility.sum(listOfInts, i->i<4, (a,b)->a+b).get(); | |
//vendor code | |
public static <T extends Number> Optional<T> sum(List<T> list, Predicate<T> condition, BinaryOperator<T> operation){ | |
//ability to add is only here | |
return list.parallelStream() | |
.filter(condition) | |
.map(i -> i) | |
.reduce(operation); | |
} | |
//But we don't want to do this!!! As our inital assumption is that client doesn't or don't want to do the arithmetic operation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment