diff --git a/core-java-modules/core-java-19/pom.xml b/core-java-modules/core-java-19/pom.xml new file mode 100644 index 0000000000..096b13e679 --- /dev/null +++ b/core-java-modules/core-java-19/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + core-java-19 + + + 19 + 19 + UTF-8 + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 19 + 19 + --enable-preview + + + + + + \ No newline at end of file diff --git a/core-java-modules/core-java-19/src/main/java/com/baeldung/features/records/GPSPoint.java b/core-java-modules/core-java-19/src/main/java/com/baeldung/features/records/GPSPoint.java new file mode 100644 index 0000000000..961575c88d --- /dev/null +++ b/core-java-modules/core-java-19/src/main/java/com/baeldung/features/records/GPSPoint.java @@ -0,0 +1,3 @@ +package com.baeldung.features.records; + +public record GPSPoint (double lat, double lng) { } \ No newline at end of file diff --git a/core-java-modules/core-java-19/src/main/java/com/baeldung/features/records/ILocation.java b/core-java-modules/core-java-19/src/main/java/com/baeldung/features/records/ILocation.java new file mode 100644 index 0000000000..00851c8d5e --- /dev/null +++ b/core-java-modules/core-java-19/src/main/java/com/baeldung/features/records/ILocation.java @@ -0,0 +1,9 @@ +package com.baeldung.features.records; + +public sealed interface ILocation permits Location { + default String getName() { + return switch (this) { + case Location(var name, var ignored) -> name; + }; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-19/src/main/java/com/baeldung/features/records/Location.java b/core-java-modules/core-java-19/src/main/java/com/baeldung/features/records/Location.java new file mode 100644 index 0000000000..fb50fbe645 --- /dev/null +++ b/core-java-modules/core-java-19/src/main/java/com/baeldung/features/records/Location.java @@ -0,0 +1,5 @@ +package com.baeldung.features.records; + +public record Location(String name, GPSPoint gpsPoint) implements ILocation { +} + diff --git a/core-java-modules/core-java-19/src/main/java/com/baeldung/features/records/LocationWrapper.java b/core-java-modules/core-java-19/src/main/java/com/baeldung/features/records/LocationWrapper.java new file mode 100644 index 0000000000..e7ddfef5f0 --- /dev/null +++ b/core-java-modules/core-java-19/src/main/java/com/baeldung/features/records/LocationWrapper.java @@ -0,0 +1,3 @@ +package com.baeldung.features.records; + +public record LocationWrapper(T t, String description) { } diff --git a/core-java-modules/core-java-19/src/test/java/com/baeldung/features/JEP405RecordPatternsUnitTest.java b/core-java-modules/core-java-19/src/test/java/com/baeldung/features/JEP405RecordPatternsUnitTest.java new file mode 100644 index 0000000000..10ad33b411 --- /dev/null +++ b/core-java-modules/core-java-19/src/test/java/com/baeldung/features/JEP405RecordPatternsUnitTest.java @@ -0,0 +1,99 @@ +package com.baeldung.features; + +import com.baeldung.features.records.GPSPoint; +import com.baeldung.features.records.Location; +import com.baeldung.features.records.LocationWrapper; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + + +public class JEP405RecordPatternsUnitTest { + + Object object = new Location("Home", new GPSPoint(1.0, 2.0)); + + @Test + void givenObject_whenTestInstanceOfAndCastIdiom_shouldMatchNewInstanceOf() { + // old Code + if (object instanceof Location) { + Location l = (Location) object; + assertThat(l).isInstanceOf(Location.class); + } + // new code + if (object instanceof Location l) { + assertThat(l).isInstanceOf(Location.class); + } + } + + @Test + void givenObject_whenTestDestruct_shouldMatch() { + // when + if (object instanceof Location(var name, var gpsPoint)) { + // then + assertThat(name).isEqualTo("Home"); + assertThat(gpsPoint).isInstanceOf(GPSPoint.class); + } + + if (object instanceof Location(var name, GPSPoint(var lat, var lng))) { + assertThat(lat).isEqualTo(1.0); + assertThat(lng).isEqualTo(2.0); + } + } + + @Test + void givenObjectIsNull_whenTestNullCheck_shouldNotMatch() { + Location l = null; + if (l instanceof Location location) { + assertThat(location).isNotNull(); + } + } + + + @Test + void givenObject_whenTestGenericTypeInstanceOf_shouldMatch() { + LocationWrapper locationWrapper = new LocationWrapper<>(new Location("Home", new GPSPoint(1.0, 2.0)), "Description"); + if (locationWrapper instanceof LocationWrapper(var ignored, var description)) { + assertThat(description).isEqualTo("Description"); + } + } + + + @Test + void givenObject_whenTestSwitchExpressionWithTypePattern_shouldMatch() { + String result = switch (object) { + case Location l -> l.name(); + default -> "default"; + }; + assertThat(result).isEqualTo("Home"); + Double result2 = switch (object) { + case Location(var name, GPSPoint(var lat, var lng)) -> lat; + default -> 0.0; + }; + assertThat(result2).isEqualTo(1.0); + } + + @Test + void givenObject_whenTestGuardedSwitchExpressionWithTypePattern_shouldMatchAndGuard() { + String result = switch (object) { + case Location(var name, var ignored) when name.equals("Home") -> "Test"; + case Location(var name, var ignored) -> name; + default -> "default"; + }; + assertThat(result).isEqualTo("Test"); + + String otherResult = switch (new Location("Other", new GPSPoint(1.0, 2.0))) { + case Location(var name, var ignored) when name.equals("Home") -> "Test"; + case Location(var name, var ignored) -> name; + default -> "default"; + }; + assertThat(otherResult).isEqualTo("Other"); + + Object noLocation = new GPSPoint(1.0, 2.0); + String noLocationResult = switch (noLocation) { + case Location(var name, var ignored) when name.equals("Home") -> "Test"; + case Location(var name, var ignored) -> name; + default -> "default"; + }; + assertThat(noLocationResult).isEqualTo("default"); + } +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index f1c5bfe874..cc137d08b6 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -151,4 +151,4 @@ - \ No newline at end of file + diff --git a/pom.xml b/pom.xml index 3d2863e1f2..c4c9dbf925 100644 --- a/pom.xml +++ b/pom.xml @@ -1130,6 +1130,7 @@ + core-java-modules/core-java-collections-set core-java-modules/core-java-collections-list-4 core-java-modules/core-java-collections-maps-4