Skip to content

Instantly share code, notes, and snippets.

@john77eipe
Created April 16, 2017 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john77eipe/da5212b420da0ee7642f8ef6e410a7cc to your computer and use it in GitHub Desktop.
Save john77eipe/da5212b420da0ee7642f8ef6e410a7cc to your computer and use it in GitHub Desktop.
//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