1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
import java.util.function.BiFunction;
public class HasCodeTest {
/**
* Enum privée de test
*/
private enum EnumString implements HasCode<String>{
STRING_VALUE1,
STRING_VALUE2,
STRING_VALUE3;
@Override
public String getCode() {
return name();
}
}
/**
* Enum privée de test
*/
private enum EnumInteger implements HasCode<Integer>{
INTEGER_VALUE1,
INTEGER_VALUE2,
INTEGER_VALUE3;
@Override
public Integer getCode() {
return ordinal();
}
}
/**
* Enum privée de test
*/
private enum EnumRaw implements HasCode{
RAW_VALUE1,
RAW_VALUE2,
RAW_VALUE3;
@Override
public String getCode() {
return name();
}
}
public static <T, U, R> R checkExpected(BiFunction<T, U, R> funct, T param, U secondParam, R expectedResult){
R result = funct.apply(param, secondParam);
if (result != null && result.equals(expectedResult)){
System.out.println("expected result " + result );
} else {
System.out.println("Result not found for parameter " + param );
}
return result;
}
public static void main(String[] args){
checkExpected(HasCode::fromCode, "RAW_VALUE2", EnumRaw.class, EnumRaw.RAW_VALUE2); // IJ me met une erreur sur le HasCode::fromCode. Mais il s'exécute sans problème... Et la compil maven marche aussi
checkExpected(HasCode::fromCode, 1, EnumInteger.class, EnumInteger.INTEGER_VALUE2);
checkExpected(HasCode::fromCode, "STRING_VALUE2", EnumString.class, EnumString.STRING_VALUE2);
}
} |
Partager