Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 109fbb1

Browse filesBrowse files
committed
BAEL-3759: Guava's Futures and ListenableFuture
* Added new module guava-modules/guava-concurrency * Added implementation code for simple usages of ListenableFuture * Added implementation code for complex usages of ListenableFuture
1 parent ecbb4f0 commit 109fbb1
Copy full SHA for 109fbb1

File tree

Expand file treeCollapse file tree

6 files changed

+523
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

6 files changed

+523
-0
lines changed
Open diff view settings
Collapse file
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
<parent>
6+
<artifactId>guava-modules</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>guava-concurrency</artifactId>
13+
14+
</project>
Collapse file
+105Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.baeldung.guava.future;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Random;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.FutureTask;
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.stream.Collectors;
10+
11+
import com.baeldung.guava.future.exception.ListenableFutureException;
12+
import com.google.common.util.concurrent.Futures;
13+
import com.google.common.util.concurrent.ListenableFuture;
14+
import com.google.common.util.concurrent.ListenableFutureTask;
15+
import com.google.common.util.concurrent.ListeningExecutorService;
16+
import com.google.common.util.concurrent.MoreExecutors;
17+
18+
public class ListenableFutureService {
19+
20+
private final ListeningExecutorService lExecService;
21+
22+
public ListenableFutureService() {
23+
this.lExecService = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
24+
}
25+
26+
public ListenableFutureService(ListeningExecutorService lExecService) {
27+
this.lExecService = lExecService;
28+
}
29+
30+
public ListenableFuture<String> fetchConfig(String configKey) {
31+
return lExecService.submit(() -> {
32+
TimeUnit.MILLISECONDS.sleep(500);
33+
return String.format("%s.%d", configKey, new Random().nextInt(Integer.MAX_VALUE));
34+
});
35+
}
36+
37+
public FutureTask<String> fetchConfigTask(String configKey) {
38+
return new FutureTask<>(() -> {
39+
TimeUnit.MILLISECONDS.sleep(500);
40+
return String.format("%s.%d", configKey, new Random().nextInt(Integer.MAX_VALUE));
41+
});
42+
}
43+
44+
public ListenableFutureTask<String> fetchConfigListenableTask(String configKey) {
45+
return ListenableFutureTask.create(() -> {
46+
TimeUnit.MILLISECONDS.sleep(500);
47+
return String.format("%s.%d", configKey, new Random().nextInt(Integer.MAX_VALUE));
48+
});
49+
}
50+
51+
public ListenableFuture<Integer> succeedingTask() {
52+
return Futures.immediateFuture(new Random().nextInt(Integer.MAX_VALUE));
53+
}
54+
55+
public <T> ListenableFuture<T> failingTask() {
56+
return Futures.immediateFailedFuture(new ListenableFutureException());
57+
}
58+
59+
public ListenableFuture<Integer> getCartId() {
60+
return lExecService.submit(() -> {
61+
TimeUnit.MILLISECONDS.sleep(500);
62+
return new Random().nextInt(Integer.MAX_VALUE);
63+
});
64+
}
65+
66+
public ListenableFuture<String> getCustomerName() {
67+
String[] names = new String[] { "Mark", "Jane", "June" };
68+
return lExecService.submit(() -> {
69+
TimeUnit.MILLISECONDS.sleep(500);
70+
return names[new Random().nextInt(names.length)];
71+
});
72+
}
73+
74+
public ListenableFuture<List<String>> getCartItems() {
75+
String[] items = new String[] { "Apple", "Orange", "Mango", "Pineapple" };
76+
return lExecService.submit(() -> {
77+
TimeUnit.MILLISECONDS.sleep(500);
78+
79+
int noOfItems = new Random().nextInt(items.length);
80+
if (noOfItems == 0) ++noOfItems;
81+
82+
return Arrays.stream(items, 0, noOfItems).collect(Collectors.toList());
83+
});
84+
}
85+
86+
public ListenableFuture<String> generateUsername(String firstName) {
87+
return lExecService.submit(() -> {
88+
TimeUnit.MILLISECONDS.sleep(500);
89+
return firstName.replaceAll("[^a-zA-Z]+","")
90+
.concat("@service.com");
91+
});
92+
}
93+
94+
public ListenableFuture<String> generatePassword(String username) {
95+
return lExecService.submit(() -> {
96+
TimeUnit.MILLISECONDS.sleep(500);
97+
if (username.contains("@")) {
98+
String[] parts = username.split("@");
99+
return parts[0] + "123@" + parts[1];
100+
} else {
101+
return username + "123";
102+
}
103+
});
104+
}
105+
}
Collapse file
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.guava.future.exception;
2+
3+
public class ListenableFutureException extends Exception {
4+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.