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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With