01 /*
02 * Copyright 2008-2014 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.codehaus.griffon.runtime.core.threading;
17
18 import griffon.core.threading.ThreadingHandler;
19 import griffon.core.threading.UIThreadManager;
20
21 import javax.annotation.Nonnull;
22 import javax.inject.Inject;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.Future;
26
27 import static java.util.Objects.requireNonNull;
28
29 /**
30 * Base implementation of the ThreadingHandler interface.
31 *
32 * @author Andres Almiray
33 * @since 2.0.0
34 */
35 public abstract class AbstractThreadingHandler implements ThreadingHandler {
36 private static final String ERROR_RUNNABLE_NULL = "Argument 'runnable' must not be bull";
37 private static final String ERROR_CALLABLE_NULL = "Argument 'callable' must not be null";
38
39 private UIThreadManager uiThreadManager;
40
41 @Inject
42 public void setUIThreadManager(@Nonnull UIThreadManager uiThreadManager) {
43 this.uiThreadManager = requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be bull");
44 }
45
46 public boolean isUIThread() {
47 return uiThreadManager.isUIThread();
48 }
49
50 public void runInsideUIAsync(@Nonnull Runnable runnable) {
51 requireNonNull(runnable, ERROR_RUNNABLE_NULL);
52 uiThreadManager.runInsideUIAsync(runnable);
53 }
54
55 public void runInsideUISync(@Nonnull Runnable runnable) {
56 requireNonNull(runnable, ERROR_RUNNABLE_NULL);
57 uiThreadManager.runInsideUISync(runnable);
58 }
59
60 public void runOutsideUI(@Nonnull Runnable runnable) {
61 requireNonNull(runnable, ERROR_RUNNABLE_NULL);
62 uiThreadManager.runOutsideUI(runnable);
63 }
64
65 @Nonnull
66 public <R> Future<R> runFuture(@Nonnull ExecutorService executorService, @Nonnull Callable<R> callable) {
67 requireNonNull(callable, ERROR_CALLABLE_NULL);
68 return uiThreadManager.runFuture(executorService, callable);
69 }
70
71 @Nonnull
72 public <R> Future<R> runFuture(@Nonnull Callable<R> callable) {
73 requireNonNull(callable, ERROR_CALLABLE_NULL);
74 return uiThreadManager.runFuture(callable);
75 }
76 }
|