Skip to content

Instantly share code, notes, and snippets.

@john77eipe
Last active April 16, 2017 13:51
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/207dbc03e3c2c70e0705a49cfddc61eb to your computer and use it in GitHub Desktop.
Save john77eipe/207dbc03e3c2c70e0705a49cfddc61eb to your computer and use it in GitHub Desktop.
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){
if(condition.check(i)){
if (i instanceof Integer) {
sum=sum.intValue()+i.intValue();
}
if(i instanceof Double) {
sum=sum.doubleValue()+i.doubleValue();
}
//so on
}
}
return sum;
}
}
//In client
ConditionCheck isEven = new ConditionCheck<Integer>(){
@Override
public boolean check(Integer i) {
return i%2==0;
}
};
int result = MathUtility.sum(listOfInts, isBig))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment