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:
Daniel Strmecki
2021-10-20 18:59:46 +02:00
committed by GitHub
parent 3320d8413b
commit b5560909a1
12 changed files with 365 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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