Skip to content

Commit 89197ef

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 909994d + ec578d1 commit 89197ef

File tree

71 files changed

+554
-186
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+554
-186
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+
}

core-java-modules/core-java-collections-list/src/main/java/com/baeldung/findanelement/FindACustomerInGivenList.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.List;
55

66
import org.apache.commons.collections4.IterableUtils;
7-
7+
import org.apache.commons.collections4.CollectionUtils;
88
import com.google.common.base.Predicate;
99
import com.google.common.collect.Iterables;
1010

@@ -74,4 +74,11 @@ public boolean evaluate(Customer customer) {
7474
});
7575
}
7676

77-
}
77+
public boolean findUsingExists(String name, List<Customer> customers) {
78+
return CollectionUtils.exists(customers, new org.apache.commons.collections4.Predicate<Customer>() {
79+
public boolean evaluate(Customer customer) {
80+
return customer.getName().equals(name);
81+
}
82+
});
83+
}
84+
}

core-java-modules/core-java-collections-list/src/test/java/com/baeldung/findanelement/FindACustomerInGivenListUnitTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.baeldung.findanelement;
22

3-
import static org.junit.Assert.*;
4-
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
import static org.junit.Assert.assertNotNull;
6+
import static org.junit.Assert.assertTrue;
7+
import static org.junit.Assert.assertFalse;
58
import java.util.ArrayList;
69
import java.util.List;
710

@@ -155,4 +158,16 @@ public void givenName_whenCustomerWithNameNotFoundUsingGuava_thenReturnNull() {
155158
assertNull(john);
156159
}
157160

161+
@Test
162+
public void givenName_whenCustomerWithNameFoundUsingExists_thenReturnTrue() {
163+
boolean isJamesPresent = findACustomerInGivenList.findUsingExists("James", customers);
164+
assertTrue(isJamesPresent);
165+
}
166+
167+
@Test
168+
public void givenName_whenCustomerWithNameNotFoundUsingExists_thenReturnFalse() {
169+
boolean isJohnPresent = findACustomerInGivenList.findUsingExists("John", customers);
170+
assertFalse(isJohnPresent);
171+
}
172+
158173
}

core-java-modules/core-java-concurrency-simple/src/test/java/com/baeldung/concurrent/completablefuture/CompletableFutureLongRunningUnitTest.java renamed to core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/CompletableFutureLongRunningUnitTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.baeldung.concurrent.completablefuture;
22

3-
import org.junit.Test;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertSame;
6+
import static org.junit.Assert.assertTrue;
47

58
import java.util.concurrent.CancellationException;
69
import java.util.concurrent.CompletableFuture;
@@ -12,10 +15,7 @@
1215
import java.util.stream.Collectors;
1316
import java.util.stream.Stream;
1417

15-
import static org.junit.Assert.assertEquals;
16-
import static org.junit.Assert.assertFalse;
17-
import static org.junit.Assert.assertSame;
18-
import static org.junit.Assert.assertTrue;
18+
import org.junit.Test;
1919

2020
public class CompletableFutureLongRunningUnitTest {
2121

core-java-modules/core-java-concurrency-simple/src/main/java/com/baeldung/concurrent/synchronize/SynchronizedBlocks.java renamed to core-java-modules/core-java-concurrency-basic-4/src/main/java/com/baeldung/concurrent/synchronize/SynchronizedBlocks.java

File renamed without changes.

core-java-modules/core-java-concurrency-simple/src/main/java/com/baeldung/concurrent/synchronize/SynchronizedMethods.java renamed to core-java-modules/core-java-concurrency-basic-4/src/main/java/com/baeldung/concurrent/synchronize/SynchronizedMethods.java

File renamed without changes.

core-java-modules/core-java-concurrency-simple/src/main/java/com/baeldung/concurrent/volatilekeyword/SharedObject.java renamed to core-java-modules/core-java-concurrency-basic-4/src/main/java/com/baeldung/concurrent/volatilekeyword/SharedObject.java

File renamed without changes.

core-java-modules/core-java-concurrency-simple/src/main/java/com/baeldung/concurrent/volatilekeyword/TaskRunner.java renamed to core-java-modules/core-java-concurrency-basic-4/src/main/java/com/baeldung/concurrent/volatilekeyword/TaskRunner.java

File renamed without changes.

core-java-modules/core-java-concurrency-simple/src/test/java/com/baeldung/concurrent/synchronize/SynchronizedBlocksUnitTest.java renamed to core-java-modules/core-java-concurrency-basic-4/src/test/java/com/baeldung/concurrent/synchronize/SynchronizedBlocksUnitTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.baeldung.concurrent.synchronize;
22

3-
import org.junit.Test;
3+
import static org.junit.Assert.assertEquals;
44

55
import java.util.concurrent.ExecutorService;
66
import java.util.concurrent.Executors;
77
import java.util.concurrent.TimeUnit;
88
import java.util.stream.IntStream;
99

10-
import static org.junit.Assert.assertEquals;
10+
import org.junit.Test;
1111

1212
public class SynchronizedBlocksUnitTest {
1313

0 commit comments

Comments
 (0)