diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Address.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Address.java new file mode 100644 index 0000000000..b654ffe2c5 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Address.java @@ -0,0 +1,40 @@ +package com.baeldung.java8to17; + +public class Address { + + private String street; + private String city; + private String pin; + + public Address(String street, String city, String pin) { + super(); + this.street = street; + this.city = city; + this.pin = pin; + } + + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getPin() { + return pin; + } + + public void setPin(String pin) { + this.pin = pin; + } + +} diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Circle.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Circle.java new file mode 100644 index 0000000000..1e346653cf --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Circle.java @@ -0,0 +1,4 @@ +package com.baeldung.java8to17; + +public record Circle(double radius) implements Shape { +} \ No newline at end of file diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Person.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Person.java new file mode 100644 index 0000000000..a6c9f50fc8 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Person.java @@ -0,0 +1,30 @@ +package com.baeldung.java8to17; + +public class Person { + + private String name; + private Address address; + + public Person(String name, Address address) { + super(); + this.name = name; + this.address = address; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + +} diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Rectangle.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Rectangle.java new file mode 100644 index 0000000000..79d2f0358b --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Rectangle.java @@ -0,0 +1,4 @@ +package com.baeldung.java8to17; + +public record Rectangle(double length, double width) implements Shape { +} \ No newline at end of file diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Shape.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Shape.java new file mode 100644 index 0000000000..60b5193468 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Shape.java @@ -0,0 +1,5 @@ +package com.baeldung.java8to17; + +public interface Shape { + +} diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Student.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Student.java new file mode 100644 index 0000000000..056bc4b7d5 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/java8to17/Student.java @@ -0,0 +1,5 @@ +package com.baeldung.java8to17; + +public record Student(int rollNo, String name) { + +} diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/java17/Java8to17ExampleUnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/java17/Java8to17ExampleUnitTest.java new file mode 100644 index 0000000000..950fc70988 --- /dev/null +++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/java17/Java8to17ExampleUnitTest.java @@ -0,0 +1,87 @@ +package com.baeldung.java17; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertThrows; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +import com.baeldung.java8to17.Address; +import com.baeldung.java8to17.Circle; +import com.baeldung.java8to17.Person; +import com.baeldung.java8to17.Rectangle; +import com.baeldung.java8to17.Student; + +public class Java8to17ExampleUnitTest { + + @Test + void givenMultiLineText_whenUsingTextBlock_thenStringIsReturned() { + String value = """ + This is a + Multi-line + Text + """; + + assertThat(value).isEqualTo("This is a\nMulti-line\nText\n"); + } + + @Test + void givenString_whenUsingUtilFunctions_thenReturnsExpectedResult() { + assertThat(" ".isBlank()); + assertThat("Twinkle ".repeat(2)).isEqualTo("Twinkle Twinkle "); + assertThat("Format Line".indent(4)).isEqualTo(" Format Line\n"); + assertThat("Line 1 \n Line2".lines()).asList().size().isEqualTo(2); + assertThat(" Text with white spaces ".strip()).isEqualTo("Text with white spaces"); + assertThat("Car, Bus, Train".transform(s1 -> Arrays.asList(s1.split(","))).get(0)).isEqualTo("Car"); + } + + @Test + void givenDataModel_whenUsingRecordType_thenBehavesLikeDTO() { + Student student = new Student(10, "Priya"); + Student student2 = new Student(10, "Priya"); + + assertThat(student.rollNo()).isEqualTo(10); + assertThat(student.name()).isEqualTo("Priya"); + assertThat(student.equals(student2)); + assertThat(student.hashCode()).isEqualTo(student2.hashCode()); + } + + @Test + void givenObject_whenThrowingNPE_thenReturnsHelpfulMessage() { + Person student = new Person("Lakshmi", new Address("35, West Street", null, null)); + + Exception exception = assertThrows(NullPointerException.class, () -> { + student.getAddress().getCity().toLowerCase(); + }); + + assertThat(exception.getMessage()).isEqualTo( + "Cannot invoke \"String.toLowerCase()\" because the return value of \"com.baeldung.java8to17.Address.getCity()\" is null"); + } + + @Test + void givenGenericObject_whenUsingPatternMatching_thenReturnsTargetType() { + String city = null; + Object obj = new Address("35, West Street", "Chennai", "6000041"); + + if (obj instanceof Address address) { + city = address.getCity(); + } + + assertThat(city).isEqualTo("Chennai"); + } + + @Test + void givenGenericObject_whenUsingSwitchExpression_thenPatternMatchesRightObject() { + Object shape = new Rectangle(10, 20); + + double circumference = switch (shape) { + case Rectangle r -> 2 * r.length() + 2 * r.width(); + case Circle c -> 2 * c.radius() * Math.PI; + default -> throw new IllegalArgumentException("Unknown shape"); + }; + + assertThat(circumference).isEqualTo(60); + } + +}