Lombok builder to check non null and not empty

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.


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.


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");
        }
    }
}