簡體   English   中英

Java + Lombok + Guava + 驗證

[英]Java + Lombok + Guava + Validation

我有這個 Pojo:

@Getter
@EqualsAndHashCode
public class Order {

    public enum OrderType {
        BUY, SELL
    }
    private Id id;
    private Quantity quantity;
    private Money price;
    private OrderType orderType;

    public Order(Id id, Quantity quantity, Money price, OrderType orderType) {

        Preconditions.checkNotNull(id, "id can't be null");
        Preconditions.checkNotNull(quantity, "quantity can't be null");
        Preconditions.checkNotNull(price, "price can't be null");
        Preconditions.checkNotNull(orderType, "orderType can't be null");

        this.id = id;
        this.quantity = quantity;
        this.price = price;
        this.orderType = orderType;
    }

我想做三件事:

  • 改用@AllArgsConstructor
  • 並刪除構造函數
  • 但當然要保持先決條件

這可能嗎?

我也喜歡使用 @Builder 模式,我可以將前提條件與這種方法結合起來嗎?

@lombok.NonNull標記所有字段並使用@RequiredArgsConstructor 應該這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM