Skip to content

Commit 6ef3b3a

Browse files
Added CompletableFuture support to TzevaAdomNotifier#listen
1 parent f5f4dc5 commit 6ef3b3a

3 files changed

Lines changed: 26 additions & 116 deletions

File tree

src/main/java/dte/tzevaadomapi/notifier/TzevaAdomNotifier.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.LinkedList;
99
import java.util.Objects;
1010
import java.util.Set;
11+
import java.util.concurrent.CompletableFuture;
1112
import java.util.concurrent.TimeUnit;
1213
import 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()

src/main/java/dte/tzevaadomapi/utils/UncheckedExceptions.java

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/test/java/dte/tzevaadomapi/notifier/TzevaAdomNotifierTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package dte.tzevaadomapi.notifier;
22

3-
import static dte.tzevaadomapi.utils.UncheckedExceptions.unchecked;
43
import static org.junit.Assert.assertEquals;
54
import static org.mockito.Mockito.when;
65

76
import java.time.Duration;
87
import java.time.LocalDateTime;
9-
import java.util.concurrent.CompletableFuture;
108

119
import org.junit.jupiter.api.Test;
1210
import org.junit.jupiter.api.TestInstance;
@@ -74,7 +72,7 @@ private TzevaAdomNotifier simulateNotifier(int alertsAmount) throws InterruptedE
7472
.onFailedRequest(Exception::printStackTrace)
7573
.build();
7674

77-
CompletableFuture.runAsync(unchecked(notifier::listen));
75+
notifier.listen();
7876

7977
//because I'm ahla gever, every alert gets 10ms to be recorded
8078
Thread.sleep(Duration.ofMillis(alertsAmount * 10).toMillis());

0 commit comments

Comments
 (0)