@Builder is a helpful mechanism for using the Builder pattern without writing boilerplate code. We can apply this annotation to a Class or a constructor or a method.@Builder needs an @AllArgsConstructor@Data @AllArgsConstructor @NoArgsConstructor @ToString @Builder
public class Employee{
private String name;
private int age;
}
public class EmployeeData{
public static void main(String[] args){
Employee
.builder()
.name("deepthi")
.age(24)
.build();
Employee e = Employee
.builder()
.build()
System.out.println(e.getName() + " " + e.getAge())
}
}
@Data @AllArgsConstructor @NoArgsConstructor @ToString @Builder
public class Chocolate{
private String name;
private Double price;
private Boolean isAvailable;
// creating constructor for class Chocolate for name and price variables
@Builder
public Chocolate(String name, Double price){
this.name = name;
this.price = price;
}
}
public class ChocolateData{
public static void main(String[] args){
Chocolate c = Chocolate
.builder()
.name("Kitkat")
.price(10.0)
.build();
System.out.println(c.getName() + " " + c.getPrice() + " " + c.getIsAvailable())
}
}
@Data @AllArgsConstructor @NoArgsConstructor @ToString @Builder
public class Books{
private String author;
private Integer pages;
private Boolean isAvailable;
// creating constructor for class Books for author, pages and isAvailable variables
public Books(String author, Integer pages, Boolean isAvailable){
this.author = author;
this.pages = pages;
this.isAvailable = isAvailable;
}
// creating a instance method BooksInstance(object of this class)
@Builder
private static Books BooksInstance(String author, Integer pages){
return new Books(author, pages, true); // creating object of Books class
}
public class BooksData{
public static void main(String[] args){
Books c = Books
.builder()
.author("Dr Joseph Murphy")
.pages(270)
.build();
System.out.println(c.getAuthor() + " " + c.getPages() + " " + c.getIsAvailable())
}
}
@Data @AllArgsConstructor @NoArgsConstructor @ToString @Builder
public class FluidBloodPressure{
private List<Sections> sections;
}
@Data @AllArgsConstructor @NoArgsConstructor @ToString @Builder
public Sections{
private String patient;
private List<Treatment> treatment;
}
public Interface report{
getReport(fluidBloodPressure);
}
private Interface getReport(FluidBloodPressure fluidBloodPressure){
FluidBloodPressure fbp = FluidBloodPressure
.builder()
.sections(fluidBloodPressure.getSections()
.stream()
.map(listItem -> FluidBloodPressure.builder()
.patient(listItem.getPatient())
.treatment(listItem.getTreatment())
.build())
.collect(Collections.toList()))
.build();
return fbp;
}
@ToString @Data @AllArgsConstructor @NoArgsConstructor
@Builder
public class BuilderWithoutDefaultValue{
private Long id;
private String name = "Deepthi";
private Boolean merit;
}
public class Test{
public static void main (String[] args){
BuilderWithoutDefaultValue result = BuilderWithoutDefaultValue.builder().build();
System.out.println(result);
}
}
@ToString @Data @AllArgsConstructor @NoArgsConstructor
@Builder
public class BuilderWithDefaultValue{
private Long id;
@Default
private String name = "Deepthi";
@Builder.Default
private Boolean merit = true;
}
public class Test{
public static void main (String[] args){
BuilderWithDefaultValue result = BuilderWithDefaultValue.builder().build();
System.out.println(result);
}
}