Skip to content

Commit 1023634

Browse files
authored
feat BAEL-6092 Record Patterns (eugenp#13243)
* feat BAEL-6092 Record Patterns * Update pom.xml * Update pom.xml * Add additional Switch Test Case
1 parent 5983e19 commit 1023634

8 files changed

Lines changed: 154 additions & 1 deletion

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.baeldung.core-java-modules</groupId>
8+
<artifactId>core-java-modules</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>core-java-19</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>19</maven.compiler.source>
16+
<maven.compiler.target>19</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<configuration>
25+
<source>19</source>
26+
<target>19</target>
27+
<compilerArgs>--enable-preview</compilerArgs>
28+
</configuration>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
33+
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.features.records;
2+
3+
public record GPSPoint (double lat, double lng) { }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.features.records;
2+
3+
public sealed interface ILocation permits Location {
4+
default String getName() {
5+
return switch (this) {
6+
case Location(var name, var ignored) -> name;
7+
};
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.features.records;
2+
3+
public record Location(String name, GPSPoint gpsPoint) implements ILocation {
4+
}
5+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.features.records;
2+
3+
public record LocationWrapper<T>(T t, String description) { }
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.baeldung.features;
2+
3+
import com.baeldung.features.records.GPSPoint;
4+
import com.baeldung.features.records.Location;
5+
import com.baeldung.features.records.LocationWrapper;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
9+
10+
11+
public class JEP405RecordPatternsUnitTest {
12+
13+
Object object = new Location("Home", new GPSPoint(1.0, 2.0));
14+
15+
@Test
16+
void givenObject_whenTestInstanceOfAndCastIdiom_shouldMatchNewInstanceOf() {
17+
// old Code
18+
if (object instanceof Location) {
19+
Location l = (Location) object;
20+
assertThat(l).isInstanceOf(Location.class);
21+
}
22+
// new code
23+
if (object instanceof Location l) {
24+
assertThat(l).isInstanceOf(Location.class);
25+
}
26+
}
27+
28+
@Test
29+
void givenObject_whenTestDestruct_shouldMatch() {
30+
// when
31+
if (object instanceof Location(var name, var gpsPoint)) {
32+
// then
33+
assertThat(name).isEqualTo("Home");
34+
assertThat(gpsPoint).isInstanceOf(GPSPoint.class);
35+
}
36+
37+
if (object instanceof Location(var name, GPSPoint(var lat, var lng))) {
38+
assertThat(lat).isEqualTo(1.0);
39+
assertThat(lng).isEqualTo(2.0);
40+
}
41+
}
42+
43+
@Test
44+
void givenObjectIsNull_whenTestNullCheck_shouldNotMatch() {
45+
Location l = null;
46+
if (l instanceof Location location) {
47+
assertThat(location).isNotNull();
48+
}
49+
}
50+
51+
52+
@Test
53+
void givenObject_whenTestGenericTypeInstanceOf_shouldMatch() {
54+
LocationWrapper<Location> locationWrapper = new LocationWrapper<>(new Location("Home", new GPSPoint(1.0, 2.0)), "Description");
55+
if (locationWrapper instanceof LocationWrapper<Location>(var ignored, var description)) {
56+
assertThat(description).isEqualTo("Description");
57+
}
58+
}
59+
60+
61+
@Test
62+
void givenObject_whenTestSwitchExpressionWithTypePattern_shouldMatch() {
63+
String result = switch (object) {
64+
case Location l -> l.name();
65+
default -> "default";
66+
};
67+
assertThat(result).isEqualTo("Home");
68+
Double result2 = switch (object) {
69+
case Location(var name, GPSPoint(var lat, var lng)) -> lat;
70+
default -> 0.0;
71+
};
72+
assertThat(result2).isEqualTo(1.0);
73+
}
74+
75+
@Test
76+
void givenObject_whenTestGuardedSwitchExpressionWithTypePattern_shouldMatchAndGuard() {
77+
String result = switch (object) {
78+
case Location(var name, var ignored) when name.equals("Home") -> "Test";
79+
case Location(var name, var ignored) -> name;
80+
default -> "default";
81+
};
82+
assertThat(result).isEqualTo("Test");
83+
84+
String otherResult = switch (new Location("Other", new GPSPoint(1.0, 2.0))) {
85+
case Location(var name, var ignored) when name.equals("Home") -> "Test";
86+
case Location(var name, var ignored) -> name;
87+
default -> "default";
88+
};
89+
assertThat(otherResult).isEqualTo("Other");
90+
91+
Object noLocation = new GPSPoint(1.0, 2.0);
92+
String noLocationResult = switch (noLocation) {
93+
case Location(var name, var ignored) when name.equals("Home") -> "Test";
94+
case Location(var name, var ignored) -> name;
95+
default -> "default";
96+
};
97+
assertThat(noLocationResult).isEqualTo("default");
98+
}
99+
}

core-java-modules/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,4 @@
151151
</dependencies>
152152
</dependencyManagement>
153153

154-
</project>
154+
</project>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@
11301130
<!-- <module>core-java-modules/core-java-15</module> --> <!-- uses preview features, to be decided how to handle -->
11311131
<!-- <module>core-java-modules/core-java-16</module> --> <!-- uses preview features, to be decided how to handle -->
11321132
<!-- <module>core-java-modules/core-java-17</module> --> <!-- uses preview features, to be decided how to handle -->
1133+
<!-- <module>core-java-modules/core-java-19</module> --> <!-- uses preview features, to be decided how to handle -->
11331134
<module>core-java-modules/core-java-collections-set</module>
11341135
<module>core-java-modules/core-java-collections-list-4</module>
11351136
<module>core-java-modules/core-java-collections-maps-4</module>

0 commit comments

Comments
 (0)