From 1918f7a7538dff6846c3286086112fced213a6b5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Wed, 27 Jun 2018 20:06:16 +0200
Subject: [PATCH] code: use whenComplete, thenCompose for more readable code

---
 .../core/BenchmarkJobManager.java             | 40 +++++++++----------
 .../ProgressNotifierTemporarySwitchOff.java   |  5 ++-
 2 files changed, 21 insertions(+), 24 deletions(-)

diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/BenchmarkJobManager.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/BenchmarkJobManager.java
index 6baaf057..25da9cf2 100644
--- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/BenchmarkJobManager.java
+++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/BenchmarkJobManager.java
@@ -418,28 +418,24 @@ public class BenchmarkJobManager implements Closeable {
 			String mainFile = job.getProperty(SPIM_OUTPUT_FILENAME_PATTERN) + ".xml";
 			final ProgressNotifierTemporarySwitchOff notifierSwitch = new ProgressNotifierTemporarySwitchOff(downloadNotifier, job);
 			
-			job.startDownload(downloadFileNameExtractDecorator(fileName->fileName.equals(mainFile)))
-			.whenComplete((X,e1)-> {
-				notifierSwitch.switchOn();
-				if(e1 == null) {
-					Set<String> otherFiles = extractNames(getOutputDirectory().resolve(mainFile));
-					try {
-						job.startDownload(downloadFileNameExtractDecorator(name -> otherFiles.contains(name)))
-								.whenComplete((X2,e2) -> {
-									result.complete(null);
-									if(e2 != null) {
-										log.error(e2.getMessage(), e2);
-									}
-								});
-					} catch (IOException e) {
-						e1 = e;
-					}
-				}
-				if(e1 != null){
-					log.error(e1.getMessage(), e1);
-					result.complete(null);
-				}
-			});
+			job.startDownload(downloadFileNameExtractDecorator(fileName -> fileName.equals(mainFile)))
+					.whenComplete((X, E) -> {
+						notifierSwitch.switchOn();
+					}).thenCompose(X -> {
+						Set<String> otherFiles = extractNames(getOutputDirectory().resolve(mainFile));
+						try {
+							return job
+									.startDownload(downloadFileNameExtractDecorator(name -> otherFiles.contains(name)));
+						} catch (IOException e) {
+							throw new RuntimeException(e);
+						}
+
+					}).whenComplete((X, e) -> {
+						if (e != null) {
+							log.error(e.getMessage(), e);
+						}
+						result.complete(null);
+					});
 		}
 		
 		private Set<String> extractNames(Path resolve) {
diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/ProgressNotifierTemporarySwitchOff.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/ProgressNotifierTemporarySwitchOff.java
index a1a1d275..f1bb0018 100644
--- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/ProgressNotifierTemporarySwitchOff.java
+++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/ProgressNotifierTemporarySwitchOff.java
@@ -6,7 +6,7 @@ import cz.it4i.fiji.haas_java_client.ProgressNotifier;
 
 public class ProgressNotifierTemporarySwitchOff {
 
-	private final ProgressNotifier notifier;
+	private ProgressNotifier notifier;
 	private final Job job;
 	
 	public ProgressNotifierTemporarySwitchOff(ProgressNotifier downloadNotifier, Job job) {
@@ -17,10 +17,11 @@ public class ProgressNotifierTemporarySwitchOff {
 		}
 	}
 
-	public void switchOn() {
+	synchronized public void switchOn() {
 		if(notifier != null) {
 			this.job.setDownloadNotifier(notifier);
 		}
+		notifier = null;
 	}
 
 }
-- 
GitLab