Skip to content

Commit 0e97e59

Browse files
authored
Merge pull request #32 from panos-kakos/master
Update from master
2 parents 96adc28 + 507c344 commit 0e97e59

File tree

33 files changed

+418
-121
lines changed

33 files changed

+418
-121
lines changed

apache-kafka-2/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121

122122
<properties>
123123
<jna.version>5.7.0</jna.version>
124-
<kafka.version>3.6.1</kafka.version>
124+
<kafka.version>3.9.0</kafka.version>
125125
<testcontainers-kafka.version>1.19.3</testcontainers-kafka.version>
126126
<testcontainers-jupiter.version>1.19.3</testcontainers-jupiter.version>
127127
<jackson.databind.version>2.15.2</jackson.databind.version>
@@ -130,4 +130,4 @@
130130
<awaitility.version>3.0.0</awaitility.version>
131131
</properties>
132132

133-
</project>
133+
</project>

core-java-modules/core-java-17/src/main/java/com/baeldung/switchpatterns/ParenthesizedPatterns.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.switchpatterns;
2+
3+
public class GuardedPatterns {
4+
5+
static double getDoubleValueUsingIf(Object o) {
6+
return switch (o) {
7+
case String s -> {
8+
if (s.length() > 0) {
9+
yield Double.parseDouble(s);
10+
} else {
11+
yield 0d;
12+
}
13+
}
14+
default -> 0d;
15+
};
16+
}
17+
18+
static double getDoubleValueUsingGuardedPatterns(Object o) {
19+
return switch (o) {
20+
case String s when s.length() > 0 -> Double.parseDouble(s);
21+
default -> 0d;
22+
};
23+
}
24+
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.switchpatterns;
2+
3+
public class HandlingNullValues {
4+
5+
static double getDoubleUsingSwitchNullCase(Object o) {
6+
return switch (o) {
7+
case String s -> Double.parseDouble(s);
8+
case null -> 0d;
9+
default -> 0d;
10+
};
11+
}
12+
13+
static double getDoubleUsingSwitchTotalType(Object o) {
14+
return switch (o) {
15+
case String s -> Double.parseDouble(s);
16+
case Object ob -> 0d;
17+
};
18+
}
19+
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.switchpatterns;
2+
3+
public class PatternMatching {
4+
5+
public static void main(String[] args) {
6+
Object o = args[0];
7+
if (o instanceof String s) {
8+
System.out.printf("Object is a string %s", s);
9+
} else if(o instanceof Number n) {
10+
System.out.printf("Object is a number %n", n);
11+
}
12+
}
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.switchpatterns;
2+
3+
public class SwitchStatement {
4+
5+
public static void main(String[] args) {
6+
final String b = "B";
7+
switch (args[0]) {
8+
case "A" -> System.out.println("Parameter is A");
9+
case b -> System.out.println("Parameter is b");
10+
default -> System.out.println("Parameter is unknown");
11+
};
12+
}
13+
14+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.switchpatterns;
2+
3+
public class TypePatterns {
4+
5+
static double getDoubleUsingIf(Object o) {
6+
double result;
7+
8+
if (o instanceof Integer) {
9+
result = ((Integer) o).doubleValue();
10+
} else if (o instanceof Float) {
11+
result = ((Float) o).doubleValue();
12+
} else if (o instanceof String) {
13+
result = Double.parseDouble(((String) o));
14+
} else {
15+
result = 0d;
16+
}
17+
18+
return result;
19+
}
20+
21+
static double getDoubleUsingSwitch(Object o) {
22+
return switch (o) {
23+
case Integer i -> i.doubleValue();
24+
case Float f -> f.doubleValue();
25+
case String s -> Double.parseDouble(s);
26+
default -> 0d;
27+
};
28+
}
29+
30+
}
Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import org.junit.jupiter.api.Test;
44

55
import static org.junit.jupiter.api.Assertions.assertEquals;
6-
import static com.baeldung.switchpatterns.ParenthesizedPatterns.*;
6+
import static com.baeldung.switchpatterns.GuardedPatterns.*;
77

8-
class ParenthesizedPatternsUnitTest {
8+
class GuardedPatternsUnitTest {
99

1010
@Test
1111
void givenIfImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
@@ -17,24 +17,14 @@ void givenIfImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
1717
assertEquals(10d, getDoubleValueUsingIf("10"));
1818
}
1919

20-
@Test
21-
void givenIfImplementation_whenStringContainsSpecialChar_thenDoubleIsReturned() {
22-
assertEquals(0d, getDoubleValueUsingIf("@10"));
23-
}
24-
2520
@Test
2621
void givenPatternsImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
27-
assertEquals(0d, getDoubleValueUsingParenthesizedPatterns(""));
22+
assertEquals(0d, getDoubleValueUsingGuardedPatterns(""));
2823
}
2924

3025
@Test
3126
void givenPatternsImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
32-
assertEquals(10d, getDoubleValueUsingParenthesizedPatterns("10"));
33-
}
34-
35-
@Test
36-
void givenPatternsImplementation_whenStringContainsSpecialChar_thenDoubleIsReturned() {
37-
assertEquals(0d, getDoubleValueUsingParenthesizedPatterns("@10"));
27+
assertEquals(10d, getDoubleValueUsingGuardedPatterns("10"));
3828
}
3929

4030
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.switchpatterns;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static com.baeldung.switchpatterns.HandlingNullValues.*;
7+
8+
class HandlingNullValuesUnitTest {
9+
10+
@Test
11+
void givenNullCaseInSwitch_whenUsingStringAsArgument_thenDoubleIsReturned() {
12+
assertEquals(10d, getDoubleUsingSwitchNullCase("10"));
13+
}
14+
15+
@Test
16+
void givenTotalTypeInSwitch_whenUsingNullArgument_thenDoubleIsReturned() {
17+
assertEquals(0d, getDoubleUsingSwitchNullCase(null));
18+
}
19+
20+
@Test
21+
void givenTotalTypeInSwitch_whenUsingStringAsArgument_thenDoubleIsReturned() {
22+
assertEquals(10d, getDoubleUsingSwitchTotalType("10"));
23+
}
24+
25+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.switchpatterns;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static com.baeldung.switchpatterns.TypePatterns.*;
6+
7+
class TypePatternsUnitTest {
8+
9+
@Test
10+
void givenIfImplementation_whenUsingIntegerAsArgument_thenDoubleIsReturned() {
11+
assertEquals(10d, getDoubleUsingIf(10));
12+
}
13+
14+
@Test
15+
void givenIfImplementation_whenUsingDoubleAsArgument_thenDoubleIsReturned() {
16+
assertEquals(10d, getDoubleUsingIf(10.0f));
17+
}
18+
19+
@Test
20+
void givenIfImplementation_whenUsingStringAsArgument_thenDoubleIsReturned() {
21+
assertEquals(10d, getDoubleUsingIf("10"));
22+
}
23+
24+
@Test
25+
void givenIfImplementation_whenUsingCharAsArgument_thenDoubleIsReturned() {
26+
assertEquals(0d, getDoubleUsingIf('c'));
27+
}
28+
29+
@Test
30+
void givenSwitchImplementation_whenUsingIntegerAsArgument_thenDoubleIsReturned() {
31+
assertEquals(10d, getDoubleUsingSwitch(10));
32+
}
33+
34+
@Test
35+
void givenSwitchImplementation_whenUsingDoubleAsArgument_thenDoubleIsReturned() {
36+
assertEquals(10d, getDoubleUsingSwitch(10.0f));
37+
}
38+
39+
@Test
40+
void givenSwitchImplementation_whenUsingStringAsArgument_thenDoubleIsReturned() {
41+
assertEquals(10d, getDoubleUsingSwitch("10"));
42+
}
43+
44+
@Test
45+
void givenSwitchImplementation_whenUsingCharAsArgument_thenDoubleIsReturned() {
46+
assertEquals(0d, getDoubleUsingSwitch('c'));
47+
}
48+
49+
}

0 commit comments

Comments
 (0)