Feature/bael 5177 switch pattern matching (#11345)
* BAEL-5177: New module using Java 17 * BAEL-5177: Add unit tests * BAEL-5177: Add switch example * BAEL-5177: Update type pattern test * BAEL-5177: Total type example * BAEL-5177: Refactor * BAEL-5177: Move implementation to separate class * BAEL-5177: Tabs to spaces
This commit is contained in:
parent
ad674edd3d
commit
00b22063ef
3
core-java-modules/core-java-17/README.md
Normal file
3
core-java-modules/core-java-17/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
### Relevant articles:
|
||||
|
||||
- TODO
|
81
core-java-modules/core-java-17/pom.xml
Normal file
81
core-java-modules/core-java-17/pom.xml
Normal file
@ -0,0 +1,81 @@
|
||||
<?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>
|
||||
<artifactId>core-java-17</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-17</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<release>${maven.compiler.release}</release>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
<forkCount>1</forkCount>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-api</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>17</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>17</maven.compiler.target.version>
|
||||
<maven.compiler.release>17</maven.compiler.release>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||
<assertj.version>3.17.2</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,25 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class GuardedPatterns {
|
||||
|
||||
static double getDoubleValueUsingIf(Object o) {
|
||||
return switch (o) {
|
||||
case String s -> {
|
||||
if (s.length() > 0) {
|
||||
yield Double.parseDouble(s);
|
||||
} else {
|
||||
yield 0d;
|
||||
}
|
||||
}
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
static double getDoubleValueUsingGuardedPatterns(Object o) {
|
||||
return switch (o) {
|
||||
case String s && s.length() > 0 -> Double.parseDouble(s);
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class HandlingNullValues {
|
||||
|
||||
static double getDoubleUsingSwitchNullCase(Object o) {
|
||||
return switch (o) {
|
||||
case String s -> Double.parseDouble(s);
|
||||
case null -> 0d;
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
static double getDoubleUsingSwitchTotalType(Object o) {
|
||||
return switch (o) {
|
||||
case String s -> Double.parseDouble(s);
|
||||
case Object ob -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class ParenthesizedPatterns {
|
||||
|
||||
static double getDoubleValueUsingIf(Object o) {
|
||||
return switch (o) {
|
||||
case String s -> {
|
||||
if (s.length() > 0) {
|
||||
if (s.contains("#") || s.contains("@")) {
|
||||
yield 0d;
|
||||
} else {
|
||||
yield Double.parseDouble(s);
|
||||
}
|
||||
} else {
|
||||
yield 0d;
|
||||
}
|
||||
}
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
static double getDoubleValueUsingParenthesizedPatterns(Object o) {
|
||||
return switch (o) {
|
||||
case String s && s.length() > 0 && !(s.contains("#") || s.contains("@")) -> Double.parseDouble(s);
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class PatternMatching {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Object o = args[0];
|
||||
if (o instanceof String s) {
|
||||
System.out.printf("Object is a string %s", s);
|
||||
} else if(o instanceof Number n) {
|
||||
System.out.printf("Object is a number %n", n);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class SwitchStatement {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final String b = "B";
|
||||
switch (args[0]) {
|
||||
case "A" -> System.out.println("Parameter is A");
|
||||
case b -> System.out.println("Parameter is b");
|
||||
default -> System.out.println("Parameter is unknown");
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class TypePatterns {
|
||||
|
||||
static double getDoubleUsingIf(Object o) {
|
||||
double result;
|
||||
|
||||
if (o instanceof Integer) {
|
||||
result = ((Integer) o).doubleValue();
|
||||
} else if (o instanceof Float) {
|
||||
result = ((Float) o).doubleValue();
|
||||
} else if (o instanceof String) {
|
||||
result = Double.parseDouble(((String) o));
|
||||
} else {
|
||||
result = 0d;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static double getDoubleUsingSwitch(Object o) {
|
||||
return switch (o) {
|
||||
case Integer i -> i.doubleValue();
|
||||
case Float f -> f.doubleValue();
|
||||
case String s -> Double.parseDouble(s);
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static com.baeldung.switchpatterns.GuardedPatterns.*;
|
||||
|
||||
class GuardedPatternsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleValueUsingIf(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleValueUsingIf("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPatternsImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleValueUsingGuardedPatterns(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPatternsImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleValueUsingGuardedPatterns("10"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static com.baeldung.switchpatterns.HandlingNullValues.*;
|
||||
|
||||
class HandlingNullValuesUnitTest {
|
||||
|
||||
@Test
|
||||
void givenNullCaseInSwitch_whenUsingStringAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingSwitchNullCase("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTotalTypeInSwitch_whenUsingNullArgument_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleUsingSwitchNullCase(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTotalTypeInSwitch_whenUsingStringAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingSwitchTotalType("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNullCaseInSwitch_whenUsingNullArgument_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleUsingSwitchTotalType(null));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static com.baeldung.switchpatterns.ParenthesizedPatterns.*;
|
||||
|
||||
class ParenthesizedPatternsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleValueUsingIf(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleValueUsingIf("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenStringContainsSpecialChar_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleValueUsingIf("@10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPatternsImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleValueUsingParenthesizedPatterns(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPatternsImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleValueUsingParenthesizedPatterns("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPatternsImplementation_whenStringContainsSpecialChar_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleValueUsingParenthesizedPatterns("@10"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.baeldung.switchpatterns;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static com.baeldung.switchpatterns.TypePatterns.*;
|
||||
|
||||
class TypePatternsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingIntegerAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingIf(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingDoubleAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingIf(10.0f));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingStringAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingIf("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingCharAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleUsingIf('c'));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSwitchImplementation_whenUsingIntegerAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingSwitch(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSwitchImplementation_whenUsingDoubleAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingSwitch(10.0f));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSwitchImplementation_whenUsingStringAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingSwitch("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSwitchImplementation_whenUsingCharAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleUsingSwitch('c'));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user