Completed
Branch master (f2c7a8)
by Christoph
06:39
created

notifyOnUiThread()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
c 1
b 0
f 0
cc 2
crap 6
rs 10
1
/*
2
 * This program is free software; you can redistribute it and/or
3
 * modify it under the terms of the GNU General Public License
4
 * as published by the Free Software Foundation; either version 2
5
 * of the License, or (at your option) any later version.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
15
 * Boston, MA 02110-1301, USA.
16
 *
17
 * For information about the authors of this project Have a look
18
 * at the AUTHORS file in the root of this project.
19
 */
20
package net.sourceforge.fullsync.ui;
21
22
import java.util.concurrent.CompletableFuture;
23
import java.util.concurrent.ScheduledExecutorService;
24
import java.util.function.Consumer;
25
import java.util.function.Supplier;
26
27
import javax.inject.Inject;
28
29
import org.eclipse.swt.widgets.Display;
30
31
import net.sourceforge.fullsync.ThrowingSupplier;
32
33
public class BackgroundExecutor {
34
	private final ScheduledExecutorService scheduledExecutorService;
35
	private final Display display;
36
37
	@Inject
38
	public BackgroundExecutor(ScheduledExecutorService scheduledExecutorService, Display display) {
39
		this.scheduledExecutorService = scheduledExecutorService;
40
		this.display = display;
41
	}
42
43
	public <T, R> void runAsync(ThrowingSupplier<R> supplier, Consumer<R> successConsumer, Consumer<Exception> errorConsumer) {
0 ignored issues
show
Unused Code introduced by
T is not used in the method.
Loading history...
44
		CompletableFuture<R> future = CompletableFuture.supplyAsync(unchecked(supplier), scheduledExecutorService);
45
		UIUpdateTask<R> task = new UIUpdateTask<>(display, future, successConsumer, errorConsumer);
46
		future.thenRun(task);
47
	}
48
49
	// from http://4comprehension.com/sneakily-throwing-exceptions-in-lambda-expressions-in-java/
50
	static <R> Supplier<R> unchecked(ThrowingSupplier<R> f) {
51
		return () -> {
52
			try {
53
				return f.get();
54
			}
55
			catch (Exception ex) {
56
				return sneakyThrow(ex);
57
			}
58
		};
59
	}
60
61
	// from http://4comprehension.com/sneakily-throwing-exceptions-in-lambda-expressions-in-java/
62
	@SuppressWarnings("unchecked")
63
	static <T extends Exception, R> R sneakyThrow(Exception t) throws T {
64
		throw (T) t; // ( ͡° ͜ʖ ͡°)
65
	}
66
67
	private static class UIUpdateTask<T> implements Runnable {
68
		private final Display display;
69
		private final CompletableFuture<T> future;
70
		private final Consumer<T> successConsumer;
71
		private final Consumer<Exception> errorConsumer;
72
73
		public UIUpdateTask(Display display, CompletableFuture<T> future, Consumer<T> successConsumer, Consumer<Exception> errorConsumer) {
74
			this.display = display;
75
			this.future = future;
76
			this.successConsumer = successConsumer;
77
			this.errorConsumer = errorConsumer;
78
		}
79
80
		@Override
81
		public void run() {
82
			display.asyncExec(this::notifyOnUiThread);
83
		}
84
85
		private void notifyOnUiThread() {
86
			try {
87
				successConsumer.accept(future.get());
88
			}
89
			catch (Exception ex) {
90
				errorConsumer.accept(ex);
91
			}
92
		}
93
	}
94
}
95