feat BAEL-6092 Record Patterns (#13243)

* feat BAEL-6092 Record Patterns

* Update pom.xml

* Update pom.xml

* Add additional Switch Test Case
This commit is contained in:
Kilian Schneider 2023-01-06 23:12:38 +01:00 committed by GitHub
parent 0ce4cfcfda
commit 0a9424696a
8 changed files with 154 additions and 1 deletions

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>core-java-19</artifactId>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>19</source>
<target>19</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,3 @@
package com.baeldung.features.records;
public record GPSPoint (double lat, double lng) { }

View File

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

View File

@ -0,0 +1,5 @@
package com.baeldung.features.records;
public record Location(String name, GPSPoint gpsPoint) implements ILocation {
}

View File

@ -0,0 +1,3 @@
package com.baeldung.features.records;
public record LocationWrapper<T>(T t, String description) { }

View File

@ -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<Location> locationWrapper = new LocationWrapper<>(new Location("Home", new GPSPoint(1.0, 2.0)), "Description");
if (locationWrapper instanceof LocationWrapper<Location>(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");
}
}

View File

@ -151,4 +151,4 @@
</dependencies>
</dependencyManagement>
</project>
</project>

View File

@ -1130,6 +1130,7 @@
<!-- <module>core-java-modules/core-java-15</module> --> <!-- uses preview features, to be decided how to handle -->
<!-- <module>core-java-modules/core-java-16</module> --> <!-- uses preview features, to be decided how to handle -->
<!-- <module>core-java-modules/core-java-17</module> --> <!-- uses preview features, to be decided how to handle -->
<!-- <module>core-java-modules/core-java-19</module> --> <!-- uses preview features, to be decided how to handle -->
<module>core-java-modules/core-java-collections-set</module>
<module>core-java-modules/core-java-collections-list-4</module>
<module>core-java-modules/core-java-collections-maps-4</module>