Russell Bateman April 2022
import lombok.Data; @Data public class Car { private String brand; private String model; private int year; }
import lombok.Builder; @Builder public class Car { private String brand; private String model; private int year; public static void main( String[] args ) { Car myCamaro = Car.builder() .brand( "Chevrolet" ) .model( "Camaro" ) .year( 2022 ) .build(); } }
...that throws an error if anything required is missing.
import lombok.Builder; import lombok.NonNull; @Builder public class DetailedCar { @NonNull private String brand; @NonNull private String model; @NonNull private int year; @NonNull private String engineCode; @NonNull private String engineType; private String requiredFuel; private String fuelSystem; private String maxHorsePower; private String maxTorque; private float fuelCapacity; public static void main( String[] args ) { Car myCamaro = Car.builder() .brand( "Chevrolet" ) .model( "Camaro" ) .year( 2022 ) .build(); } }
If, as above, engineCode and/or engineType are missing from the builder invocation, an Exception will be thrown:
Exception in thread "main" java.lang.NullPointerException: engineCode is marked non-null but is null
import lombok.Data; import lombok.ToString; import lombok.EqualsAndHashCode; @Data @ToString( callSuper = true ) @EqualsAndHashCode( callSuper = true ) public class Car extends MotorVehicle { private String brand; private String model; private int year; ... }