Lombok 构建器检查非空且不为空
Lombok builder to check non null and not empty
我有一个 class 变量,我不希望它为 null 或空。有没有办法使用 Lombok 构建器来设置 属性?我可以使用 @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
}
}
}
构建器注释应该可以解决您的问题:
@Builder
class Person {
@NonNull
private String firstName;
@NonNull
private String lastName;
}
生成的代码是:
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 + ")";
}
}
}
在这种情况下,null 验证将在对象构造期间进行。
Maxim Kirilov 的回答不完整。它不检查 blank/empty 个字符串。
我以前遇到过同样的问题,我意识到除了使用来自 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;
}
此外,当 String 具有空白、空值或 null 值时,抛出 IllegalArgumentException 更有意义(而不是 NPE)。
我做了这样的事情,
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");
}
}
}
你试过“@NotEmpty”吗?它在 javax.validation.constraints 包
中
https://javaee.github.io/javaee-spec/javadocs/javax/validation/constraints/NotEmpty.html
我有一个 class 变量,我不希望它为 null 或空。有没有办法使用 Lombok 构建器来设置 属性?我可以使用 @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
}
}
}
构建器注释应该可以解决您的问题:
@Builder
class Person {
@NonNull
private String firstName;
@NonNull
private String lastName;
}
生成的代码是:
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 + ")";
}
}
}
在这种情况下,null 验证将在对象构造期间进行。
Maxim Kirilov 的回答不完整。它不检查 blank/empty 个字符串。
我以前遇到过同样的问题,我意识到除了使用来自 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;
}
此外,当 String 具有空白、空值或 null 值时,抛出 IllegalArgumentException 更有意义(而不是 NPE)。
我做了这样的事情,
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");
}
}
}
你试过“@NotEmpty”吗?它在 javax.validation.constraints 包
中https://javaee.github.io/javaee-spec/javadocs/javax/validation/constraints/NotEmpty.html