Utility to run consumers on a given thread. @param Type of content. @author Matt
| 15 | * @author Matt |
| 16 | */ |
| 17 | public class ThreadAction<T> { |
| 18 | private Supplier<T> supplier; |
| 19 | private Consumer<T> consumer; |
| 20 | private boolean consumerFx; |
| 21 | |
| 22 | /** |
| 23 | * @param <T> |
| 24 | * Type of content. |
| 25 | * |
| 26 | * @return Built action. |
| 27 | */ |
| 28 | public static <T> ThreadAction<T> create() { |
| 29 | return new ThreadAction<>(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param supplier |
| 34 | * Content source. |
| 35 | * |
| 36 | * @return Built action. |
| 37 | */ |
| 38 | public ThreadAction<T> supplier(Supplier<T> supplier) { |
| 39 | this.supplier = supplier; |
| 40 | return this; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @param consumer |
| 45 | * Action to run on content. |
| 46 | * |
| 47 | * @return Built action. |
| 48 | */ |
| 49 | public ThreadAction<T> consumer(Consumer<T> consumer) { |
| 50 | this.consumer = consumer; |
| 51 | return this; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Set consumer action to run on JavaFx thread. |
| 56 | * |
| 57 | * @return Built action. |
| 58 | */ |
| 59 | public ThreadAction<T> onUi() { |
| 60 | this.consumerFx = true; |
| 61 | return this; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Run 'em. |
| 66 | */ |
| 67 | public void run() { |
| 68 | ThreadUtil.run(() -> { |
| 69 | T value = supplier.get(); |
| 70 | if(consumer != null) { |
| 71 | if(consumerFx) { |
| 72 | Platform.runLater(() -> consumer.accept(value)); |
| 73 | } else { |
| 74 | consumer.accept(value); |
nothing calls this directly
no outgoing calls
no test coverage detected