IntegerUtils and DoubleUtils in Apache Commons package

15,756

I wish they had a utilities class for numbers as useful as the one for strings. NumberUtils class is all about converting numbers to/from strings.

You can use ObjectUtils to do null-safe Integer operations though.

Instead of:

foo(Integer arg) {
  if(arg != null && arg == 1)
    doSomething();
}

You can do:

foo(Integer arg) {
  if(ObjectUtils.defaultIfNull(arg, 0) == 1)
    doSomething();
}

In the case where the Integer you are comparing is, say, a function call that returns an Integer, this will allow you to only call the function once without creating a throwaway variable.

Share:
15,756
Venk K
Author by

Venk K

A developer next door...

Updated on July 08, 2022

Comments

  • Venk K
    Venk K almost 2 years

    I use the Apache Commons package extensively, especially the StringUtils, BooleanUtils, ObjectUtils, MapUtils classes and find them extremely helpful. I am wondering if there are classes such as IntegerUtils, DoubleUtils that provide a similar functionality for their respective wrapper classes (I do not find such classes in the Apache Commons package).

    Thanks,

    Venkat