PDA

Click to See Complete Forum and Search --> : [RESOLVED] "Template" or "Multi Type" function


bergerkiller
Mar 8th, 2011, 09:36 AM
Hello everyone.

I have this HUGE class containing way too many functions. It could be shortened to 1/5 if I could only use multi typing in the functions.

For example, the following functions:
public static Object Choose(boolean Expression, Object TrueValue, Object FalseValue) {
if (Expression) return TrueValue;
return FalseValue;
}
public static int Choose(boolean Expression, int TrueValue, int FalseValue) {
if (Expression) return TrueValue;
return FalseValue;
}
public static int[] Choose(boolean Expression, int[] TrueValue, int[] FalseValue) {
if (Expression) return TrueValue;
return FalseValue;
}
public static Texture Choose(boolean Expression, Texture TrueValue, Texture FalseValue) {
if (Expression) return TrueValue;
return FalseValue;
}
public static String Choose(boolean Expression, String TrueValue, String FalseValue) {
if (Expression) return TrueValue;
return FalseValue;
}

I know there is a simple way of choosing between objects only, but it requires type conversion after the function. So, how could I shorten it to something like:
public static class Choose(Type T) {
T Choose(boolean Expression, T TrueValue, T FalseValue) {
if (Expression) return TrueValue; else return FalseValue;
}
}

Thanks in advance.

EDIT

I was pretty close to the resolution, found out that it was called "Generics" in the Java language:
public static <T> T choose(boolean Expression, T TrueValue, T FalseValue) {
if (Expression) return TrueValue; else return FalseValue;
}