1

I have an object that contain some fields, I want to check if some fields are not null and not empty. Is there an good way to do that in java 8 or apache utilities...

I don't want do something like

if(myObj.getMyField1 != null || myObj.getMyField1 != "" || myObj.getMyField2 != null || myObj.getMyField2 != "" || myObj.getMyField3 != null || myObj.getMyField3 != "") {} 

this is myObj

@Data // lombok for generating getters and setters
public class Myobj {
   private String myField1;
   private String myField2;
   private String myField3;
   private String myField4;
   private String myField5;

   private AnotherObj myField6;

}

Would you have any suggestions ?

holi-java
  • 29,655
  • 7
  • 72
  • 83
Victor
  • 385
  • 2
  • 12
  • 26
  • 1
    Is `getMyField1` supposed to be a getter (actually `getMyField1()`) or a field access (actually `myField1`)? – Holger May 23 '17 at 15:50
  • @Holger by looking at the code of myObj, it would need to be getter – eis May 24 '17 at 04:37

3 Answers3

5

You can using java-8 Stream#anyMatch to checking the strings is whether null or empty. for example:

boolean hasNullOrEmptyString = Stream.of(myField1,myField2,...,myFieldN)
                                     .anyMatch(it-> it==null || it.isEmpty());

OR you can replace lambda expression with method reference expression which the method can be reused later:

boolean hasNullOrEmptyString = Stream.of(myField1,myField2,...,myFieldN)
                                     .anyMatch(this::isNullOrEmptyString);

boolean isNullOrEmptyString(String it){
  return it==null || it.isEmpty();
}

OR as I see your question that marked as spring, and spring already has a utility method StringUtils#isEmpty for check a String whether is null or empty:

boolean hasNullOrEmptyString = Stream.of(myField1,myField2,...,myFieldN)
                                     .anyMatch(StringUtils::isEmpty);
holi-java
  • 29,655
  • 7
  • 72
  • 83
  • Thanks @holi-java But when a fields is null the application throw **java.lang.NullPointerException** – Victor May 24 '17 at 15:05
  • @Victor Hi, you can see this [How to fix NullPointerException](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it/24100776#24100776) for more details. – holi-java May 24 '17 at 15:11
  • Thanks @holi-java. Concerning NullpointerException i check if the object is null and then applying **Stream.of...** in each fields of that object – Victor May 26 '17 at 10:23
  • @Victor Not at all. I only could give you some suggestion since you didn't put your example code in question. – holi-java May 26 '17 at 10:28
  • This the first verification: `boolean addressValidation = (cInfo.getAddress() != null)? Stream.of(cInfo.getAddress().getZip(), cInfo.getAddress().getCity()).anyMatch(StringUtils::isEmpty): false;` – Victor May 26 '17 at 14:03
  • This the second one `boolean proValidation = addressValidation && Stream.of(cInfo.getCompanyName(), cInfo.getCountry(), cInfo.getLegalForm()).anyMatch(StringUtils::isEmpty);` – Victor May 26 '17 at 14:04
2

I would suggest Apache StringUtils class : https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isAnyEmpty-java.lang.CharSequence...-

milcaepsilon
  • 272
  • 3
  • 16
2

You've tagged this as spring-boot, so I'm assuming you might be using controllers and validating their parameters. If that is the case, just do

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Data
public class Myobj {
   @NotNull
   @Size(min = 1)
   private String myField1;
   @NotNull
   @Size(min = 1)
   private String myField2;
   /* etc */
}

and

@RequestMapping(value = "/some/url", method = POST)
public void yourMethod(@RequestBody @Valid YourObject yourObject) {
    // 
}

Then your object will be validated on instantiation.

eis
  • 51,991
  • 13
  • 150
  • 199
  • Thanks @eis how I can catch "@NotNull" if there is an notNullException in my code – Victor May 23 '17 at 15:00
  • @Victor via an [exception handler or controller advice](https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc). – eis May 23 '17 at 15:04