Skip to content

Commit 79c1da8

Browse files
Merge pull request #13 from DavidTheExplorer/1.7.0
v1.7.0 - Immutability change, Better thread name.
2 parents 152c6ef + e4213a2 commit 79c1da8

File tree

6 files changed

+16
-24
lines changed

6 files changed

+16
-24
lines changed

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.6.0</version>
7+
<version>1.7.0</version>
88

99
<build>
1010
<plugins>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import dte.tzevaadomapi.alert.Alert;
66

77
/**
8-
* Responsible of providing information about Tzeva Adoms in Israel. The info comes in form of {@link Alert} objects.
8+
* Provides information about Tzeva Adoms in Israel, in form of {@link Alert} objects.
99
*/
1010
public interface AlertSource
1111
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import java.net.URLConnection;
1010

1111
/**
12-
* Represents a source that requires internet connection in order to access.
12+
* Represents a source that requires internet connection in order to access.
1313
* <p>
14-
* This class holds the essential connection details(the url, a possible proxy, etc) and provides convenient connection methods.
14+
* This class takes care of the essential connection details(the url, a possible proxy) and provides convenient connection methods.
1515
*/
1616
public abstract class OnlineAlertSource implements AlertSource
1717
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import dte.tzevaadomapi.utils.UncheckedExceptions.CheckedFunction;
2121

2222
/**
23-
* Requests Alerts from the the website of Pikud Ha Oref.
23+
* Requests Alerts from Pikud Ha'Oref.
2424
*/
2525
public class PHOAlertSource extends OnlineAlertSource
2626
{

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,12 @@ public class TzevaAdomHistory implements Iterable<Alert>
1515
{
1616
private final Deque<Alert> history = new LinkedList<>();
1717

18-
/**
19-
* Records a new alert.
20-
*
21-
* @param alert The alert.
22-
*/
23-
public void update(Alert alert)
24-
{
25-
this.history.add(alert);
26-
}
27-
2818
/**
2919
* Records a collection of alerts.
3020
*
3121
* @param alerts The alerts.
3222
*/
33-
public void update(Collection<Alert> alerts)
23+
void update(Collection<Alert> alerts)
3424
{
3525
this.history.addAll(alerts);
3626
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.HashSet;
66
import java.util.Set;
77
import java.util.concurrent.CompletableFuture;
8+
import java.util.concurrent.Executor;
9+
import java.util.concurrent.Executors;
810
import java.util.concurrent.TimeUnit;
911
import java.util.function.Consumer;
1012

@@ -24,7 +26,7 @@ public class TzevaAdomNotifier
2426
private final Consumer<Exception> requestFailureHandler;
2527
private final Set<TzevaAdomListener> listeners = new HashSet<>();
2628
private final TzevaAdomHistory history = new TzevaAdomHistory();
27-
private Alert mostRecentAlert; //only used in listen(), holding it here solves the effectively final problem
29+
private Alert mostRecentAlert; //only used in listenAsync(), holding it here solves the effectively final problem
2830

2931
private TzevaAdomNotifier(AlertSource alertSource, Duration requestDelay, Consumer<Exception> requestFailureHandler)
3032
{
@@ -49,7 +51,7 @@ public CompletableFuture<Void> listenAsync()
4951

5052
while(true)
5153
{
52-
Deque<Alert> newAlerts = request(() -> this.alertSource.getSince(this.mostRecentAlert));
54+
Deque<Alert> newAlerts = requestFromSource(() -> this.alertSource.getSince(this.mostRecentAlert));
5355

5456
//if there are no new alerts - it's not Tzeva Adom
5557
if(newAlerts.isEmpty())
@@ -62,7 +64,7 @@ public CompletableFuture<Void> listenAsync()
6264
//notify Tzeva Adom
6365
newAlerts.forEach(this::notifyListeners);
6466
}
65-
});
67+
}, Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "Tzeva-Adom-Notifier")));
6668
}
6769

6870
/**
@@ -76,7 +78,7 @@ public void addListener(TzevaAdomListener listener)
7678
}
7779

7880
/**
79-
* Returns the Tzeva Adom history since {@link #listen()} was called for this notifier.
81+
* Returns the captured history since {@link #listenAsync()} was called.
8082
*
8183
* @return The tzeva adom history.
8284
*/
@@ -92,27 +94,27 @@ private Alert requestMostRecentAlert()
9294
//wait until Hamas decides to launch rockets
9395
do
9496
{
95-
alert = request(this.alertSource::getMostRecentAlert);
97+
alert = requestFromSource(this.alertSource::getMostRecentAlert);
9698
}
9799
while(alert == AlertSource.NO_RESULT);
98100

99101
return alert;
100102
}
101103

102-
private <R> R request(CheckedSupplier<R> resultFactory)
104+
private <T> T requestFromSource(CheckedSupplier<T> resultFactory)
103105
{
104106
while(true)
105107
{
106108
try
107109
{
108-
//sleep a reasonable amount of time
110+
//sleep the defined delay
109111
TimeUnit.MILLISECONDS.sleep(this.requestDelay.toMillis());
110112

111113
return resultFactory.get();
112114
}
113115
catch(Exception exception)
114116
{
115-
//pass the exception to the handler
117+
//handle the exception and request again
116118
this.requestFailureHandler.accept(exception);
117119
}
118120
}

0 commit comments

Comments
 (0)