Skip to content

Commit 689c994

Browse files
Merge pull request #6 from DavidTheExplorer/1.2.0
v1.2.0 - CompletableFuture Support
2 parents 9c49bce + bddf73f commit 689c994

7 files changed

Lines changed: 45 additions & 125 deletions

File tree

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# Tzeva Adom API
2-
Simple Java API that listens to `Pikud Haoref's API` and notifies registered listeners once a Tzeva Adom takes place.
2+
Async Java API that listens to `Pikud Ha'oref API` and notifies registered listeners as soon as a Tzeva Adom happens.
33

44

55
## How to use
6-
Let's create a notifier object that logs a message when it's Tzeva Adom:
6+
Let's create a notifier that logs a message when it's Tzeva Adom:
77
```java
88
TzevaAdomNotifier
99
.requestFromPikudHaoref()
1010
.every(Duration.ofSeconds(3)) //amount of delay between requests
11-
.onFailedRequest(exception -> LOGGER.error("Failed to send a request to Pikud Ha'oref...", exception))
11+
.onFailedRequest(exception -> LOGGER.error("Failed to request the last alert from Pikud Ha'oref", exception))
1212
.onTzevaAdom(alert -> LOGGER.info("Tzeva Adom at: " + alert.getCity()))
13-
.listen();
13+
.listen(); //async
1414
```
1515

16-
You can save the notifier object in order to add functionality or get data from it, by calling `build()` instead of `listen()`:
16+
You can save the notifier object by calling `build()` instead of `listen()`, in order to add functionality or get data from it:
1717
```java
1818
TzevaAdomNotifier notifier = TzevaAdomNotifier
1919
// builder pattern goes here
@@ -22,14 +22,17 @@ TzevaAdomNotifier notifier = TzevaAdomNotifier
2222
notifier.listen();
2323
```
2424

25-
Adding Listeners anytime:
25+
Add more listeners anytime:
2626
```java
2727
notifier.addListener(alert -> ...);
2828
```
2929

30-
Retrieving the Tzeva Adom Alerts encountered while running:
30+
Retrieve the Tzeva Adom alerts encountered while running:
3131
```java
32+
//run the notifier async and sleep for a day
33+
notifier.run();
3234
TimeUnit.DAYS.sleep(1);
35+
3336
LOGGER.info("There were {} alerts in the last 24 hours:", notifier.getHistory().size());
3437

3538
//Pro Tip: TzevaAdomNotifier implements Iterable!

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>dte</groupId>
66
<artifactId>tzevaadomapi</artifactId>
7-
<version>1.1.0</version>
7+
<version>1.2.0</version>
88

99
<build>
1010
<plugins>

src/main/java/dte/tzevaadomapi/alertsource/AlertSource.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66
public interface AlertSource
77
{
88
Alert getMostRecentAlert() throws Exception;
9+
10+
11+
public static final Alert EMPTY_RESPONSE = null;
912
}

src/main/java/dte/tzevaadomapi/alertsource/json/JSONAlertSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Alert getMostRecentAlert() throws Exception
3131
JSONArray alertsJsonArray = requestAlertsJSON();
3232

3333
if(alertsJsonArray == null || alertsJsonArray.isEmpty())
34-
throw new IllegalArgumentException("Cannot get the most recent alert due to an empty JSON response!");
34+
return EMPTY_RESPONSE;
3535

3636
return fromJSON((JSONObject) alertsJsonArray.get(0));
3737
}

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

Lines changed: 29 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,31 @@ 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+
//ignore empty responses
60+
if(alert == AlertSource.EMPTY_RESPONSE)
61+
continue;
62+
63+
//if the last alert in history equals the the last requested - it's not Tzeva Adom
64+
if(lastTzevaAdom.equals(alert))
65+
continue;
66+
67+
lastTzevaAdom = alert;
68+
69+
this.listeners.forEach(listener -> listener.accept(alert));
70+
this.history.add(alert);
71+
}
72+
});
6573
}
6674

6775
public void addListener(Consumer<Alert> tzevaAdomListener)
@@ -85,14 +93,14 @@ public Iterator<Alert> iterator()
8593
return this.history.iterator();
8694
}
8795

88-
private Alert getMostRecentAlert() throws InterruptedException
96+
private Alert getMostRecentAlert()
8997
{
9098
while(true)
9199
{
92-
TimeUnit.MILLISECONDS.sleep(this.requestDelay.toMillis());
93-
94100
try
95101
{
102+
TimeUnit.MILLISECONDS.sleep(this.requestDelay.toMillis());
103+
96104
return this.alertSource.getMostRecentAlert();
97105
}
98106
catch(Exception exception)
@@ -135,9 +143,9 @@ public Builder onTzevaAdom(Consumer<Alert> listener)
135143
return this;
136144
}
137145

138-
public void listen() throws InterruptedException
146+
public CompletableFuture<Void> listen()
139147
{
140-
build().listen();
148+
return build().listen();
141149
}
142150

143151
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)