Last active
April 16, 2017 13:51
-
-
Save john77eipe/207dbc03e3c2c70e0705a49cfddc61eb 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
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