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+ }
0 commit comments