Java Lombok 生成器检查非空和非空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44855981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:46:04  来源:igfitidea点击:

Lombok builder to check non null and not empty

javabuilderlombok

提问by user1692342

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 @NonNullbut 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:

我有一个带有变量的类,我不希望它为空或为空。有没有办法使用 Lombok builder 来设置属性?我可以使用,@NonNull但我无法验证它是否为空。显然,另一种选择是编写我自己的构建器来完成所有这些检查。例如:

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
        }
    }
}

采纳答案by Kedar Prabhu

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:

我以前遇到过同样的问题,我意识到除了使用 Lombok 的 @NonNull 和 @Builder 之外,还可以使用私有访问修饰符重载构造函数,您可以在其中执行验证。像这样的东西:

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.

此外,当 String 具有空白、空或空值时,抛出 IllegalArgumentException 更有意义(而不是 NPE)。

回答by Maxim Kirilov

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.

在这种情况下,空验证将在对象构造期间进行。

回答by Suryavel TR

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

回答by tyler2cr

Have you tried "@NotEmpty"? It's in the javax.validation.constraints package

你试过“@NotEmpty”吗?它在 javax.validation.constraints 包中

https://javaee.github.io/javaee-spec/javadocs/javax/validation/constraints/NotEmpty.html

https://javaee.github.io/javaee-spec/javadocs/javax/validation/constraints/NotEmpty.html