BAEL-5639: added code samples for the article

This commit is contained in:
emanueltrandafir1993 2022-07-02 17:55:17 +02:00
parent 48b451fd70
commit c3d805e5c0
8 changed files with 128 additions and 0 deletions

View File

@ -14,6 +14,15 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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) {
}

View File

@ -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");
}
}