简体   繁体   中英

Java + Lombok + Guava + Validation

I have this 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;
    }

I wish to do three things:

  • use @AllArgsConstructor instead
  • and remove the constructor
  • but of course keep the Preconditions

Is this possible?

I also like to use @Builder pattern, can I incorporate Preconditions with this approach?

Mark all fields with @lombok.NonNull and use @RequiredArgsConstructor ; that should do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM