0

I'd like to create a method that checks every Object's value is empty.If the input object is null then return true;.If input is type of array, check it's length. Below is my method to implement this logic

public static boolean isEmpty(Object input) {
 if (input == null) {
 return true;
 }
 if (input instanceof Collection) {
 if (((Collection<?>) input).size() == 0) {
 return true;
 }
 }
 if (input instanceof String) {
 if (((String) input).trim().length() == 0) {
 return true;
 }
 }
 if (input instanceof Object[]) {
 if (((Object[]) input).length == 0) {
 return true;
 }
 }
 return false;
}

But the problem is while I testing as like this

int[] a = {};
float[] b = {};
Integer[] c = {};
Float[] d = {};
System.out.println(Validator.isEmpty(a));
System.out.println(Validator.isEmpty(b));
System.out.println(Validator.isEmpty(c));
System.out.println(Validator.isEmpty(d));

I have no idea why a and b are false. Can somebody explain me ?

asked Nov 6, 2016 at 13:51
6
  • 2
    "I have no idea why a and b are false." Have you checked what you get when you do new float[0] instanceof Object[]? Commented Nov 6, 2016 at 13:55
  • 1
    Is there a specific reason for this method? Have you considered using three different isEmpty methods which take different type arguments? Commented Nov 6, 2016 at 13:57
  • @T.J.Crowder Oops ! I got Incompatible conditional operand types float[] and Object[] Commented Nov 6, 2016 at 13:57
  • 1
    You can overload the isEmpty method for all primitive array types Commented Nov 6, 2016 at 14:01
  • By the way, you probably want to check if it's a CharSequence instead of just String. This will catch not only String but also StringBuilder and many other string-like objects. Commented Nov 6, 2016 at 14:07

1 Answer 1

5

float[] is not instanceof Object[]. If you want to check for all kinds of arrays, you probably want to get the class from the object and check its isArray method. Then you can use Array.getLength to get its length (since, bizarrely, you can't use Class#getField to get the length field):

import java.lang.reflect.*;
class Validator {
 public static void main(String args[]) throws Exception {
 int[] a = {};
 float[] b = {};
 Integer[] c = {};
 Float[] d = {};
 System.out.println(Validator.isEmpty(a)); // true
 System.out.println(Validator.isEmpty(b)); // true
 System.out.println(Validator.isEmpty(c)); // true
 System.out.println(Validator.isEmpty(d)); // true
 System.out.println(Validator.isEmpty(new float[3])); // false (just double-checking)
 }
 public static boolean isEmpty(Object input) {
 if (input == null) {
 return true;
 }
 if (input instanceof String) {
 if (((String) input).trim().length() == 0) {
 return true;
 }
 }
 if (input.getClass().isArray()) {
 return Array.getLength(input) == 0;
 }
 return false;
 }
}
answered Nov 6, 2016 at 13:57
2
  • Thanks alot for code example. I'm still trouble about how to change my method? before this. Commented Nov 6, 2016 at 14:08
  • @Cataclysm: Yeah, it turned out to be more of a pain than I was expecting... I wasn't expecting Class#getField to refuse to give me the length field of an array (although it's in the documentation, as it turns out). Commented Nov 6, 2016 at 14:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.