I am writing a utility method which can check for empty and null string, or collection or an object or any general types -
public static boolean isEmpty(Object obj) {
if (obj == null)
return true;
if (obj instanceof Collection)
return ((Collection<?>) obj).size() == 0;
// is below line expensive?
final String s = String.valueOf(obj).trim();
return s.length() == 0 || s.equalsIgnoreCase("null");
}
How can I make my above method efficient, since above isEmpty
method will be called multiple times from the application which is very performance critical?
I am suspecting below line will be expensive because of heavy toString methods and it will create temporary garbage as well that might cause GC and slow down the performance?
final String s = String.valueOf(obj).trim();
Update:-
I have separated isEmpty method for each type now. Below is what I got after simplifying the above isEmpty method.
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
return false;
}
public static boolean isEmpty(Collection<?> value) {
if (value == null || value.isEmpty()) {
return true;
}
return false;
}
public static boolean isEmpty(String value) {
if (value == null || value.isEmpty()) {
return true;
}
return false;
}
Update 2:-
If I need to check for map null or empty, should I keep both collection isEmpty and Map isEmpty method both or Collection isEmpty method will be fine for that?
public static void main(String[] args) {
Map<String, String> hello = new HashMap<String, String>();
System.out.println(isEmpty(hello));
Map<String, HashMap<Integer, String>> primary = new HashMap<String, HashMap<Integer, String>>();
System.out.println(isEmpty(primary));
}
public static boolean isEmpty(Collection<?> value) {
return value == null || value.isEmpty();
}
public static boolean isEmpty(Map<?, ?> value) {
return value == null || value.isEmpty();
}
This sounds like a bad design to me. Null is null, empty is empty, if it's a string it's a string, and so on. Don't try to jam everything up in one method. It's bad for maintainability and readability.
if (str == null || str.isEmpty())
...
and
if (coll == null || coll.isEmpty())
are both perfectly fine.
Personally however, I try to never ever equate null
with an empty string or empty collection. I think it's a bad practice. A null
collection is no collection at all, an empty collection is in fact still a collection. You can avoid many if (coll == null)
checks by keeping a collection non-null. If you're worried about memory consumption, use use Collections.emptySet
et al.
That being said, if you still want to go in this direction, I'd suggest you use plain method overloading and create one isEmpty(Collection<?> coll)
and one isEmpty(String str)
to avoid instanceof and casting.
Regarding your edit:
Don't do for instance
if (value == null || value.isEmpty()) {
return true;
}
return false;
just do
return value == null || value.isEmpty();