java Lombok 我们可以在一个类上同时使用 @Builder 和 @Value 吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31992342/
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
Lombok Can we use @Builder and @Value together on a single class
提问by Anil
First of all thanks to Lombok, our java code is now much sleeker and cleaner. My use case is I want to create an immutable class. For that I would use @Value annotation. Also I want to use builder capabilities, for that I would use @Builder annotation. My question is if we can use both @Builder and @Value together on a single class. Is it a good practice recommended by Lombok users/developers.
首先感谢 Lombok,我们的 Java 代码现在更加流畅和干净。我的用例是我想创建一个不可变的类。为此,我将使用 @Value 注释。此外,我想使用构建器功能,为此我将使用 @Builder 注释。我的问题是我们是否可以在一个类中同时使用 @Builder 和 @Value 。这是 Lombok 用户/开发人员推荐的好习惯吗?
采纳答案by Justin
Of course you can. To check, simply delombokyour code and see what it generates. Take this example:
当然可以。要检查,只需对您的代码进行delombok并查看它生成的内容。拿这个例子:
@Builder
@Value
public class Pair {
private Object left;
private Object right;
}
After delombokification, this produces:
在 delombokification 之后,这会产生:
public class Pair {
private Object left;
private Object right;
@java.beans.ConstructorProperties({ "left", "right" })
Pair(Object left, Object right) {
this.left = left;
this.right = right;
}
public static PairBuilder builder() {
return new PairBuilder();
}
public Object getLeft() {
return this.left;
}
public Object getRight() {
return this.right;
}
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Pair)) return false;
final Pair other = (Pair) o;
final Object this$left = this.left;
final Object other$left = other.left;
if (this$left == null ? other$left != null : !this$left.equals(other$left)) return false;
final Object this$right = this.right;
final Object other$right = other.right;
if (this$right == null ? other$right != null : !this$right.equals(other$right)) return false;
return true;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $left = this.left;
result = result * PRIME + ($left == null ? 0 : $left.hashCode());
final Object $right = this.right;
result = result * PRIME + ($right == null ? 0 : $right.hashCode());
return result;
}
public String toString() {
return "Pair(left=" + this.left + ", right=" + this.right + ")";
}
public static class PairBuilder {
private Object left;
private Object right;
PairBuilder() {
}
public Pair.PairBuilder left(Object left) {
this.left = left;
return this;
}
public Pair.PairBuilder right(Object right) {
this.right = right;
return this;
}
public Pair build() {
return new Pair(left, right);
}
public String toString() {
return "Pair.PairBuilder(left=" + this.left + ", right=" + this.right + ")";
}
}
}
So you can clearly use both @Value
and @Builder
所以你可以清楚地同时使用@Value
和@Builder
回答by Tom Andersen
As of version 1.16.10, the constructor is notpublic when you use both.
从版本 1.16.10 开始,当您同时使用两者时,构造函数不是公共的。
You can add @AllArgsConstructor to compensate for this.
您可以添加 @AllArgsConstructor 来弥补这一点。
回答by vikas
Short Answer: Yes, you can always use @Value and @Builder together. I have used this in my production code and it works fine.
简短回答:是的,您始终可以同时使用 @Value 和 @Builder。我在我的生产代码中使用了它,它工作正常。
Long Answer: You can use them together. The one thing you want to keep in mind is that the AllArgsConstructor provided by @Value will be private to package since lombok v1.16.0. So, if you want that to be public you will have to access control that additionally by @AllArgsConstructor. In my case though, private constructor is what I really wanted. My aim of using @Builder was to really allow the the object instantiation only using the builder and not use constructor or setter methods later.
长答案:您可以一起使用它们。您要记住的一件事是,从 lombok v1.16.0 开始,@Value 提供的 AllArgsConstructor 将是私有的包。因此,如果您希望将其公开,则必须另外通过@AllArgsConstructor 访问控制。不过就我而言,私有构造函数才是我真正想要的。我使用@Builder 的目的是真正允许仅使用构建器进行对象实例化,而不再使用构造函数或 setter 方法。
Here is my code:
这是我的代码:
@Value
@Builder
@ApiModel(description = "Model to represent annotation")
public class Annotation {
@NonNull
@ApiModelProperty(value = "Unique identifier")
private String id;
}