From c3d805e5c0781b61eaafc4bc439c304076a33bb1 Mon Sep 17 00:00:00 2001 From: emanueltrandafir1993 Date: Sat, 2 Jul 2022 17:55:17 +0200 Subject: [PATCH] BAEL-5639: added code samples for the article --- core-java-modules/core-java-14/pom.xml | 9 ++++ .../java14/recordvslombok/ColorData.java | 18 ++++++++ .../java14/recordvslombok/ColorRecord.java | 8 ++++ .../recordvslombok/ColorValueObject.java | 17 ++++++++ .../recordvslombok/MonochromeColor.java | 12 ++++++ .../java14/recordvslombok/StudentBuilder.java | 17 ++++++++ .../java14/recordvslombok/StudentRecord.java | 4 ++ .../recordvslombok/RecordVsLombokUntTest.java | 43 +++++++++++++++++++ 8 files changed, 128 insertions(+) create mode 100644 core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/ColorData.java create mode 100644 core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/ColorRecord.java create mode 100644 core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/ColorValueObject.java create mode 100644 core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/MonochromeColor.java create mode 100644 core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/StudentBuilder.java create mode 100644 core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/StudentRecord.java create mode 100644 core-java-modules/core-java-14/src/test/java/com/baeldung/java14/recordvslombok/RecordVsLombokUntTest.java diff --git a/core-java-modules/core-java-14/pom.xml b/core-java-modules/core-java-14/pom.xml index 35ea0bd2d0..f78edd622a 100644 --- a/core-java-modules/core-java-14/pom.xml +++ b/core-java-modules/core-java-14/pom.xml @@ -14,6 +14,15 @@ 1.0.0-SNAPSHOT + + + org.projectlombok + lombok + 1.18.24 + provided + + + diff --git a/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/ColorData.java b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/ColorData.java new file mode 100644 index 0000000000..6d50c63ba5 --- /dev/null +++ b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/ColorData.java @@ -0,0 +1,18 @@ +package com.baeldung.java14.recordvslombok; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class ColorData { + + private int red; + private int green; + private int blue; + + public String getHexString() { + return String.format("#%02X%02X%02X", red, green, blue); + } + +} diff --git a/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/ColorRecord.java b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/ColorRecord.java new file mode 100644 index 0000000000..03d1f5c264 --- /dev/null +++ b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/ColorRecord.java @@ -0,0 +1,8 @@ +package com.baeldung.java14.recordvslombok; + +public record ColorRecord(int red, int green, int blue) { + + public String getHexString() { + return String.format("#%02X%02X%02X", red, green, blue); + } +} diff --git a/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/ColorValueObject.java b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/ColorValueObject.java new file mode 100644 index 0000000000..eca33662c2 --- /dev/null +++ b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/ColorValueObject.java @@ -0,0 +1,17 @@ +package com.baeldung.java14.recordvslombok; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Value; + +@Value +@Getter(AccessLevel.NONE) +public class ColorValueObject { + int red; + int green; + int blue; + + public String getHexString() { + return String.format("#%02X%02X%02X", red, green, blue); + } +} diff --git a/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/MonochromeColor.java b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/MonochromeColor.java new file mode 100644 index 0000000000..6672cb49ea --- /dev/null +++ b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/MonochromeColor.java @@ -0,0 +1,12 @@ +package com.baeldung.java14.recordvslombok; + +import lombok.Value; + +@Value +public class MonochromeColor extends ColorData { + + public MonochromeColor(int grayScale) { + super(grayScale, grayScale, grayScale); + } + +} diff --git a/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/StudentBuilder.java b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/StudentBuilder.java new file mode 100644 index 0000000000..318634788e --- /dev/null +++ b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/StudentBuilder.java @@ -0,0 +1,17 @@ +package com.baeldung.java14.recordvslombok; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class StudentBuilder { + private String firstName; + private String lastName; + private Long studentId; + private String email; + private String phoneNumber; + private String address; + private String country; + private int age; +} \ No newline at end of file diff --git a/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/StudentRecord.java b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/StudentRecord.java new file mode 100644 index 0000000000..2d51fadd12 --- /dev/null +++ b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/recordvslombok/StudentRecord.java @@ -0,0 +1,4 @@ +package com.baeldung.java14.recordvslombok; + +public record StudentRecord(String firstName, String lastName, Long studentId, String email, String phoneNumber, String address, String country, int age) { +} diff --git a/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/recordvslombok/RecordVsLombokUntTest.java b/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/recordvslombok/RecordVsLombokUntTest.java new file mode 100644 index 0000000000..1cca13133f --- /dev/null +++ b/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/recordvslombok/RecordVsLombokUntTest.java @@ -0,0 +1,43 @@ +package com.baeldung.java14.recordvslombok; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +public class RecordVsLombokUntTest { + + @Test + public void givenAColorRecord_hexStringIsCorrect() { + var red = new ColorRecord(255, 0, 0); + + assertThat(red.getHexString()).isEqualTo("#FF0000"); + } + + @Test + public void givenAColorValueObject_hexStringIsCorrect() { + var red = new ColorValueObject(255, 0, 0); + + assertThat(red.getHexString()).isEqualTo("#FF0000"); + } + + @Test + public void givenRecordWithManyAttributes_firstNameShouldBeJohn() { + StudentRecord john = new StudentRecord("John", "Doe", null, "john@doe.com", null, null, "England", 20); + + assertThat(john.firstName()).isEqualTo("John"); + } + + @Test + public void givenBuilderWithManyAttributes_firstNameShouldBeJohn() { + StudentBuilder john = StudentBuilder.builder() + .firstName("John") + .lastName("Doe") + .email("john@doe.com") + .country("England") + .age(20) + .build(); + + assertThat(john.getFirstName()).isEqualTo("John"); + } + +} \ No newline at end of file