Thursday, March 1, 2007

Get rid of your nested if operators!

Every one of you know how it is disappointing that you can not use
switch
operators with strings, doubles, your custom objects, etc. But anyway sometimes it is possible to get rid of your nested ifs if you will use static maps. Let us consider an example: here we need to set JavaBean properties of certain types:

protected static Object fillTestBean(Object bean) throws InvocationTargetException, IllegalArgumentException, IllegalAccessException {
Method[] methods = bean.getClass().getMethods();

for (Method method : methods) {
if (RTTIUtils.isSetter(method)) {
Class type = RTTIUtils.getSetterType(method);
if (type == String.class)
RTTIUtils.invokeSetter(bean, method, testStr);
else if (type == boolean.class)
RTTIUtils.invokeSetter(bean, method, testBoolean);
else if (type == byte.class)
RTTIUtils.invokeSetter(bean, method, testByte);
else if (type == char.class)
RTTIUtils.invokeSetter(bean, method, testChar);
else if (type == short.class)
RTTIUtils.invokeSetter(bean, method, testShort);
else if (type == int.class)
RTTIUtils.invokeSetter(bean, method, testInt);
else if (type == long.class)
RTTIUtils.invokeSetter(bean, method, testLong);
else if (type == float.class)
RTTIUtils.invokeSetter(bean, method, testFloat);
else if (type == double.class)
RTTIUtils.invokeSetter(bean, method, testDouble);
else if (type == Boolean.class)
RTTIUtils.invokeSetter(bean, method, testBooleanObj);
else if (type == Byte.class)
RTTIUtils.invokeSetter(bean, method, testByteObj);
else if (type == Character.class)
RTTIUtils.invokeSetter(bean, method, testCharacterObj);
else if (type == Short.class)
RTTIUtils.invokeSetter(bean, method, testShortObj);
else if (type == Integer.class)
RTTIUtils.invokeSetter(bean, method, testIntegerObj);
else if (type == Long.class)
RTTIUtils.invokeSetter(bean, method, testLongObj);
else if (type == Float.class)
RTTIUtils.invokeSetter(bean, method, testFloatObj);
else if (type == Double.class)
RTTIUtils.invokeSetter(bean, method, testDoubleObj);
else if (type == Date.class)
RTTIUtils.invokeSetter(bean, method, testDate);
}
}

return bean;
}

(here all variables starting with test are constants)
Seems really annoying to write all this ifs and, also what if we will need to check values? Another function will also need this nested ifs!
In this case I usually do the following:

private static final Map initObjects = new HashMap();
private static boolean isCosideredType(Class type) {
return initObjects.keySet().contains(type);
}
static {
initObjects.put(String.class, testStr);
initObjects.put(boolean.class, testBoolean);
initObjects.put(byte.class, testByte);
initObjects.put(char.class, testChar);
initObjects.put(short.class, testShort);
initObjects.put(int.class, testInt);
initObjects.put(long.class, testLong);
initObjects.put(float.class, testFloat);
initObjects.put(double.class, testDouble);
initObjects.put(Boolean.class, testBooleanObj);
initObjects.put(Byte.class, testByteObj);
initObjects.put(Character.class, testCharacterObj);
initObjects.put(Short.class, testShortObj);
initObjects.put(Integer.class, testIntegerObj);
initObjects.put(Long.class, testLongObj);
initObjects.put(Float.class, testFloatObj);
initObjects.put(Double.class, testDoubleObj);
initObjects.put(Date.class, testDate);
}

protected static Object fillTestBean(Object bean) throws InvocationTargetException, IllegalArgumentException, IllegalAccessException {
Method[] methods = bean.getClass().getMethods();

for (Method method : methods) {
if (RTTIUtils.isSetter(method)) {
Class type = RTTIUtils.getSetterType(method);
if (isCosideredType(type)) {
RTTIUtils.invokeSetter(bean, method, initObjects.get(type));
}
}
}

return bean;
}

4 comments:

Anonymous said...

If you want to increase your know-how just keep visiting this web site and be updated with the most up-to-date news update posted here.


Feel free to visit my weblog; make money from home free

Anonymous said...

It's a pity you don't have a donate button! I'd definitely donate to this superb blog! I guess for now i'll settle
for bookmarking and adding your RSS feed to my Google account.

I look forward to fresh updates and will talk about this website with my Facebook
group. Chat soon!

Here is my web-site: trading binary options

Anonymous said...

Hey there! I could have sworn I've been to this blog before but after reading through some of the post I realized it's new to
me. Nonetheless, I'm definitely delighted I found it and I'll be book-marking and checking back often!


Here is my blog ... kevin trudeau free money book

Anonymous said...

Saved as a favorite, I really like your blog!

My site; make Money Online free no scams