Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to do an empty check in Java? [duplicate]

Tags:

This might look like a primitive question or a this could be done by a simple utility library method that I don't know about.

The goal is to check the value of a boolean field that is nested under two objects.

private boolean sourceWebsite(Registration registration) {     Application application = registration.getApplication();     if (application == null) {         return true;     }      Metadata metadata = application.getMetadata();     if (metadata == null) {         return true;     }      Boolean source = metadata.getSource();     if (source == null) {         return true;     }      return !source; } 

I know this could be done in a single if(). I have added multiple ifs here for the sake of readability.

Is there a way that we could simplify the above if statements and have a simple utility class that returns the value of Boolean source if the parent objects or not null?

like image 424
codeMan Avatar asked Apr 11 '19 10:04

codeMan


People also ask

Does Java isEmpty check for null?

isEmpty(< string >)​ Checks if the <string> value is an empty string containing no characters or whitespace. Returns true if the string is null or empty.

How do you pass an empty string in Java?

All you need to do is to call equals() method on empty String literal and pass the object you are testing as shown below : String nullString = null; String empty = new String(); boolean test = "". equals(empty); // true System.

How do you check a string is empty or not in Java?

Java String isEmpty() Method The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.

How do I check if a string array is not empty?

To check if an array is empty or not, you can use the . length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not.

IS null check in Java?

In order to check whether a Java object is Null or not, we can either use the isNull() method of the Objects class or comparison operator.

How do I check if a string is empty or null in Java 8?

Using the isEmpty() Method The isEmpty() method returns true or false depending on whether or not our string contains any text. It's easily chainable with a string == null check, and can even differentiate between blank and empty strings: String string = "Hello there"; if (string == null || string. isEmpty() || string.


1 Answers

You can use java.util.Optional in this way:

private boolean sourceWebsite(Registration registration) {     return Optional.of(registration)         .map(Registration::getApplication)         .map(Application::getMetadata)         .map(Metadata::getSource)         .map(source -> !source)         .orElse(Boolean.TRUE); } 

In short, this will return true if any of the getters returns null, and !Metadata.source otherwise.

like image 147
ernest_k Avatar answered Sep 21 '22 13:09

ernest_k