88import java .util .LinkedList ;
99import java .util .Objects ;
1010import java .util .Set ;
11+ import java .util .concurrent .CompletableFuture ;
1112import java .util .concurrent .TimeUnit ;
1213import java .util .function .Consumer ;
1314
@@ -44,24 +45,27 @@ public static Builder requestFromPikudHaoref()
4445 .requestFrom (new PHOAlertSource ());
4546 }
4647
47- public void listen () throws InterruptedException
48+ public CompletableFuture < Void > listen ()
4849 {
49- //start with an initial alert - against which future alerts will be compared
50- Alert lastTzevaAdom = getMostRecentAlert ();
51-
52- while (true )
50+ return CompletableFuture .runAsync (() ->
5351 {
54- Alert alert = getMostRecentAlert ();
55-
56- //if the last alert in history doesn't equal to the last requested one - It's TZEVA ADOM
57- if (lastTzevaAdom .equals (alert ))
58- continue ;
59-
60- lastTzevaAdom = alert ;
61-
62- this .listeners .forEach (listener -> listener .accept (alert ));
63- this .history .add (alert );
64- }
52+ //start with an initial alert - against which future alerts will be compared
53+ Alert lastTzevaAdom = getMostRecentAlert ();
54+
55+ while (true )
56+ {
57+ Alert alert = getMostRecentAlert ();
58+
59+ //if the last alert in history doesn't equal to the last requested one - It's TZEVA ADOM
60+ if (lastTzevaAdom .equals (alert ))
61+ continue ;
62+
63+ lastTzevaAdom = alert ;
64+
65+ this .listeners .forEach (listener -> listener .accept (alert ));
66+ this .history .add (alert );
67+ }
68+ });
6569 }
6670
6771 public void addListener (Consumer <Alert > tzevaAdomListener )
@@ -85,14 +89,14 @@ public Iterator<Alert> iterator()
8589 return this .history .iterator ();
8690 }
8791
88- private Alert getMostRecentAlert () throws InterruptedException
92+ private Alert getMostRecentAlert ()
8993 {
9094 while (true )
9195 {
92- TimeUnit .MILLISECONDS .sleep (this .requestDelay .toMillis ());
93-
9496 try
9597 {
98+ TimeUnit .MILLISECONDS .sleep (this .requestDelay .toMillis ());
99+
96100 return this .alertSource .getMostRecentAlert ();
97101 }
98102 catch (Exception exception )
@@ -135,9 +139,9 @@ public Builder onTzevaAdom(Consumer<Alert> listener)
135139 return this ;
136140 }
137141
138- public void listen () throws InterruptedException
142+ public CompletableFuture < Void > listen ()
139143 {
140- build ().listen ();
144+ return build ().listen ();
141145 }
142146
143147 public TzevaAdomNotifier build ()
0 commit comments