Skip to content

Commit ec578d1

Browse files
BAEL-9382: Stable Values in Java 25 (#18796)
* Stable Values in Java 25: Sample * Remove final From Local Variables --------- Co-authored-by: Josh Cummings <[email protected]>
1 parent b84bbe1 commit ec578d1

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
<artifactId>core-java-25</artifactId>
7+
8+
<parent>
9+
<groupId>com.baeldung.core-java-modules</groupId>
10+
<artifactId>core-java-modules</artifactId>
11+
<version>0.0.1-SNAPSHOT</version>
12+
</parent>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<configuration>
20+
<source>25</source>
21+
<target>25</target>
22+
<compilerArgs>--enable-preview</compilerArgs>
23+
</configuration>
24+
</plugin>
25+
</plugins>
26+
</build>
27+
28+
</project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.stablevalues;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
9+
import java.util.function.Function;
10+
11+
import org.junit.jupiter.api.BeforeEach
12+
13+
class StableValuesUnitTest {
14+
15+
private Set<String> cities;
16+
17+
private String expensiveMethodToDetermineCountry(String city) {
18+
switch(city) {
19+
case "Berlin":
20+
return "Germany";
21+
case "London":
22+
return "England";
23+
case "Madrid":
24+
return "Spain";
25+
case "Paris":
26+
return "France";
27+
default:
28+
throw new RuntimeException("Unsupported city");
29+
}
30+
}
31+
32+
@Test
33+
void givenStableFunctionForCityToCountry_whenValidInputsUsed_thenVerifyFunctionResultsAreExpected() {
34+
Function<String, String> cityToCountry = StableValue.function(cities, city -> expensiveMethodToDetermineCountry(city));
35+
36+
assertThat(cityToCountry.apply("London")).isEqualTo("England");
37+
assertThat(cityToCountry.apply("Madrid")).isEqualTo("Spain");
38+
assertThat(cityToCountry.apply("Paris")).isEqualTo("France");
39+
}
40+
41+
@Test
42+
void givenStableFunctionForCityToCountry_whenInvalidInputUsed_thenExceptionThrown() {
43+
Function<String, String> cityToCountry = StableValue.function(cities, city -> expensiveMethodToDetermineCountry(city));
44+
45+
assertThatIllegalArgumentException().isThrownBy(() -> cityToCountry.apply("Berlin"));
46+
}
47+
48+
@Test
49+
void givenStableListForFiveTimesTable_thenVerifyElementsAreExpected() {
50+
List<Integer> fiveTimesTable = StableValue.list(11, index -> index * 5);
51+
52+
assertThat(fiveTimesTable.get(0)).isEqualTo(0);
53+
assertThat(fiveTimesTable.get(1)).isEqualTo(5);
54+
assertThat(fiveTimesTable.get(2)).isEqualTo(10);
55+
assertThat(fiveTimesTable.get(3)).isEqualTo(15);
56+
assertThat(fiveTimesTable.get(4)).isEqualTo(20);
57+
assertThat(fiveTimesTable.get(5)).isEqualTo(25);
58+
assertThat(fiveTimesTable.get(6)).isEqualTo(30);
59+
assertThat(fiveTimesTable.get(7)).isEqualTo(35);
60+
assertThat(fiveTimesTable.get(8)).isEqualTo(40);
61+
assertThat(fiveTimesTable.get(9)).isEqualTo(45);
62+
assertThat(fiveTimesTable.get(10)).isEqualTo(50);
63+
}
64+
65+
@Test
66+
void givenStableMapForCityToCountry_thenVerifyValuesAreExpected() {
67+
Map<String, String> cityToCountry = StableValue.map(cities, city -> expensiveMethodToDetermineCountry(city));
68+
69+
assertThat(cityToCountry.get("London")).isEqualTo("England");
70+
assertThat(cityToCountry.get("Madrid")).isEqualTo("Spain");
71+
assertThat(cityToCountry.get("Paris")).isEqualTo("France");
72+
}
73+
74+
@BeforeEach
75+
void init() {
76+
cities = Set.of("London", "Madrid", "Paris");
77+
}
78+
}

0 commit comments

Comments
 (0)