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 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; |
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 |
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
//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); |
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
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){ |
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'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; |
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
//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()) { |
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
//before | |
Runnable r1 = new Runnable() { | |
@Override | |
public void run() { | |
System.out.println("My Runnable"); | |
} | |
}; | |
//now | |
Runnable r2 = () -> System.out.println("My Runnable"); |
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
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; |
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
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; |
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
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; |
NewerOlder