Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.

Commit e706749

Browse files
committed
BAEL-5639: added code samples for the article
1 parent 9b64970 commit e706749

8 files changed

Lines changed: 128 additions & 0 deletions

File tree

core-java-modules/core-java-14/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
<version>1.0.0-SNAPSHOT</version>
1515
</parent>
1616

17+
<dependencies>
18+
<dependency>
19+
<groupId>org.projectlombok</groupId>
20+
<artifactId>lombok</artifactId>
21+
<version>1.18.24</version>
22+
<scope>provided</scope>
23+
</dependency>
24+
</dependencies>
25+
1726
<build>
1827
<plugins>
1928
<plugin>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.java14.recordvslombok;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
8+
public class ColorData {
9+
10+
private int red;
11+
private int green;
12+
private int blue;
13+
14+
public String getHexString() {
15+
return String.format("#%02X%02X%02X", red, green, blue);
16+
}
17+
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.java14.recordvslombok;
2+
3+
public record ColorRecord(int red, int green, int blue) {
4+
5+
public String getHexString() {
6+
return String.format("#%02X%02X%02X", red, green, blue);
7+
}
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.java14.recordvslombok;
2+
3+
import lombok.AccessLevel;
4+
import lombok.Getter;
5+
import lombok.Value;
6+
7+
@Value
8+
@Getter(AccessLevel.NONE)
9+
public class ColorValueObject {
10+
int red;
11+
int green;
12+
int blue;
13+
14+
public String getHexString() {
15+
return String.format("#%02X%02X%02X", red, green, blue);
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.java14.recordvslombok;
2+
3+
import lombok.Value;
4+
5+
@Value
6+
public class MonochromeColor extends ColorData {
7+
8+
public MonochromeColor(int grayScale) {
9+
super(grayScale, grayScale, grayScale);
10+
}
11+
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.java14.recordvslombok;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
6+
@Getter
7+
@Builder
8+
public class StudentBuilder {
9+
private String firstName;
10+
private String lastName;
11+
private Long studentId;
12+
private String email;
13+
private String phoneNumber;
14+
private String address;
15+
private String country;
16+
private int age;
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.java14.recordvslombok;
2+
3+
public record StudentRecord(String firstName, String lastName, Long studentId, String email, String phoneNumber, String address, String country, int age) {
4+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.java14.recordvslombok;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class RecordVsLombokUntTest {
8+
9+
@Test
10+
public void givenAColorRecord_hexStringIsCorrect() {
11+
var red = new ColorRecord(255, 0, 0);
12+
13+
assertThat(red.getHexString()).isEqualTo("#FF0000");
14+
}
15+
16+
@Test
17+
public void givenAColorValueObject_hexStringIsCorrect() {
18+
var red = new ColorValueObject(255, 0, 0);
19+
20+
assertThat(red.getHexString()).isEqualTo("#FF0000");
21+
}
22+
23+
@Test
24+
public void givenRecordWithManyAttributes_firstNameShouldBeJohn() {
25+
StudentRecord john = new StudentRecord("John", "Doe", null, "john@doe.com", null, null, "England", 20);
26+
27+
assertThat(john.firstName()).isEqualTo("John");
28+
}
29+
30+
@Test
31+
public void givenBuilderWithManyAttributes_firstNameShouldBeJohn() {
32+
StudentBuilder john = StudentBuilder.builder()
33+
.firstName("John")
34+
.lastName("Doe")
35+
.email("john@doe.com")
36+
.country("England")
37+
.age(20)
38+
.build();
39+
40+
assertThat(john.getFirstName()).isEqualTo("John");
41+
}
42+
43+
}

0 commit comments

Comments
 (0)