Skip to content

Instantly share code, notes, and snippets.

View john77eipe's full-sized avatar
💥

John Eipe john77eipe

💥
View GitHub Profile
//Vendor code
static class IntegerAddition implements BinaryOperator<Integer> {
static private IntegerAddition instance = new IntegerAddition() ;
private IntegerAddition() {}
@Override
public Integer apply(Integer t, Integer u) {
return t + u;
//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
//no changes to vendor
//in client just this line of code ;-)
int result = MathUtility.sum(listOfInts, (Integer i)->{return (i<3);});
//still shorter way of doing it
int result = MathUtility.sum(listOfInts, i ->i<3);
interface ConditionCheck<T> {
public boolean check(T i);
}
class MathUtility<T extends Number> {
public static <T extends Number> Number sum(List<T> list, ConditionCheck<T> condition){
//ability to add is only here
Number sum = 0;
for(T i: list){
//Vendor's code
interface ConditionCheck {
public boolean check(Integer i);
}
class MathUtility {
public static int sum(List<Integer> list, ConditionCheck condition){
//ability to add is only here
int sum=0;
//client creates list of Ints
List<Integer> listOfInts = new ArrayList<Integer>();
listOfInts.add(2);
listOfInts.add(3);
listOfInts.add(4);
//client does the filtering based on his logic
//let's say he wants to filter out even numbers
Iterator<Integer> itr = listOfInts.iterator();
while (itr.hasNext()) {
//before
Runnable r1 = new Runnable() {
@Override
public void run() {
System.out.println("My Runnable");
}
};
//now
Runnable r2 = () -> System.out.println("My Runnable");
package async;
import java.io.IOException;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
package async;
import java.io.IOException;
import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
package async;
import java.io.IOException;
import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;