I need a final review and some tips about a function I wrote.
public static Integer max(Integer... pNumber) {
Integer lResult = null;
for (Integer lNumber : pNumber) {
if (lResult == null || (null != lNumber && lNumber > lResult)) {
lResult = lNumber;
}
}
return lResult;
}
I am not realy sure if my code is elegant.
null parameters as well as a null result is possible.
However… I have the feeling that I solved something in the code to complicated.
The function is part of an utility class which is used by the company.
using third party librarys is not allowed.
Here the corresponding test:
@Test
public void testMaxWithInteger() {
assertEquals(new Integer(9), NumberUtils.max(4, 2, -4, null, 9, 5));
}
Thanks in advance
IMHO I wouldn’t use
nullobjects asInteger[]can use 6x as much memory as usingint[]Instead of
Integer[]I would useint[]and leave0orInteger.MIN_VALUEas my “un-initialised” value.