`
收藏列表
标题 标签 来源
Adapter Pattern java, design pattern, adapter JDK 1.5 java.util.concurrent.Executors
/**
     * Returns a {@link Callable} object that, when
     * called, runs the given task and returns the given result.  This
     * can be useful when applying methods requiring a
     * <tt>Callable</tt> to an otherwise resultless action.
     * @param task the task to run
     * @param result the result to return
     * @throws NullPointerException if task null
     * @return a callable object
     */
    public static <T> Callable<T> callable(Runnable task, T result) {
        if (task == null)
            throw new NullPointerException();
        return new RunnableAdapter<T>(task, result);
    }

/**
     * A callable that runs given task and returns given result
     */
    static final class RunnableAdapter<T> implements Callable<T> {
        final Runnable task;
        final T result;
        RunnableAdapter(Runnable  task, T result) {
            this.task = task; 
            this.result = result;
        }
        public T call() { 
            task.run(); 
            return result; 
        }
    }
Global site tag (gtag.js) - Google Analytics