Refactor multiple If' statements in Java-8

前端 未结 7 1319
轮回少年
轮回少年 2021-01-06 20:05

I need to validate mandatory fields in my class

For example, 9 fields must not be null.

I need to check if they are all null but I

7条回答
  •  伪装坚强ぢ
    2021-01-06 20:48

    Basically, there are two ways here:

    • As suggested by the comment, NonNull as offered by Project Lombok for example
    • Java bean validation

    I would heavily recommend to look into bean validation:

    Define your classes that carry information as beans. And then use the wide range of annotations to mark the corresponding fields. And then use some existing framework to do the validation for you. You can even define your own annotations there, that run your own code.

提交回复
热议问题