Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lombok builder to check non null and not empty

I have a class with variables I don't want it to be null or empty. Is there a way to use Lombok builder to set the property? I can use @NonNull but I won't be able to verify if it is empty or not. Obviously the other option is to write my own builder which does all these checks. For example:

class Person {
    @NonNull
    private String firstName;
    @NonNull
    private String lastName;

    public static class PersonBuilder() {
        // .
        // .
        // .
        public Person build() {
            //do checks for empty etc and return object
        }
    }
}
like image 207
user1692342 Avatar asked Jun 30 '17 23:06

user1692342


People also ask

What is the use of @builder annotation?

The @Builder annotation produces complex builder APIs for your classes. @Builder lets you automatically produce the code required to have your class be instantiable with code such as: Person. builder()

What does the @NonNull annotation do?

Denotes that a parameter, field or method return value can never be null. This is a marker annotation and it has no specific attributes.

What is @builder annotation in Lombok?

Lombok's @Builder annotation is a useful technique to implement the builder pattern that aims to reduce the boilerplate code. In this tutorial, we will learn to apply @Builder to a class and other useful features. Ensure you have included Lombok in the project and installed Lombok support in the IDE.

Where can you use NonNull?

Overview. You can use @NonNull on a record component, or a parameter of a method or constructor. This will cause to lombok generate a null-check statement for you.


3 Answers

Maxim Kirilov's answer is incomplete. It doesn't check for blank/empty Strings.

I've faced the same issue before, and I realized that in addition to using @NonNull and @Builder from Lombok, overload the constructor with a private access modifier, where you can perform the validations. Something like this:

private Person(final String firstName, final String lastName) {
    if(StringUtils.isBlank(firstName)) {
        throw new IllegalArgumentException("First name can't be blank/empty/null"); 
    }
    if(StringUtils.isBlank(lastName)) {
        throw new IllegalArgumentException("Last name can't be blank/empty/null"); 
    }
    this.firstName = firstName;
    this.lastName = lastName;
}

Also, throwing IllegalArgumentException makes more sense (instead of NPE) when String has blank, empty or null values.

like image 137
Kedar Prabhu Avatar answered Oct 10 '22 15:10

Kedar Prabhu


The builder annotation should solve your issue:

@Builder
class Person {
    @NonNull
    private String firstName;
    @NonNull
    private String lastName;
}

The generated code is:

class Person {
    @NonNull
    private String firstName;
    @NonNull
    private String lastName;

    @ConstructorProperties({"firstName", "lastName"})
    Person(@NonNull String firstName, @NonNull String lastName) {
        if(firstName == null) {
            throw new NullPointerException("firstName");
        } else if(lastName == null) {
            throw new NullPointerException("lastName");
        } else {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }

    public static Person.PersonBuilder builder() {
        return new Person.PersonBuilder();
    }

    public static class PersonBuilder {
        private String firstName;
        private String lastName;

        PersonBuilder() {
        }

        public Person.PersonBuilder firstName(String firstName) {
            this.firstName = firstName;
            return this;
        }

        public Person.PersonBuilder lastName(String lastName) {
            this.lastName = lastName;
            return this;
        }

        public Person build() {
            return new Person(this.firstName, this.lastName);
        }

        public String toString() {
            return "Person.PersonBuilder(firstName=" + this.firstName + ", lastName=" + this.lastName + ")";
        }
    }
}

In this case the null validation will take place during object construction.

like image 40
Maxim Kirilov Avatar answered Oct 10 '22 14:10

Maxim Kirilov


I did something like this,

class Person {
    private String mFristName;
    private String mSecondName;

    @Builder
    Person(String firstName, String secondName) {
        mFristName = PreCondition.checkNotNullOrEmpty(firstName);
        mSecondName = PreCondition.checkNotNullOrEmpty(secondName);
    }
}

class PreCondition {

    static <T> T checkNotNullOrEmpty(T instance) {
        if (instance == null || (instance instanceof String && ((String) instance).isEmpty())) {
            throw new NullOrEmptyException();
        }
        return instance;
    }

    static class NullOrEmptyException extends RuntimeException {
        NullOrEmptyException() {
            super("Null or Empty");
        }
    }
}
like image 3
Suryavel TR Avatar answered Oct 10 '22 14:10

Suryavel TR