All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.cheny.toolbox.designPattern.observer.async.Observable Maven / Gradle / Ivy

There is a newer version: 2.3.6-jdk8
Show newest version
package cn.cheny.toolbox.designPattern.observer.async;

import java.util.Vector;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * 任务源
 * 异步任务获取资源返回数据
 * 订阅该资源(1,任务已经完成数据已返回,同步执行订阅任务;2,任务还未完成数据未返回,将订阅者加入队列等待通知)
 * TODO: 2018/2/10 待完善
 *
 * @param 
 */
public class Observable {

    private Executor executor;

    private Vector> subscribers = new Vector<>();

    /**
     * 异步任务
     */
    private Action action;

    /**
     * 任务完成标志
     *
     * @param action
     */
    private boolean isCompleted;

    private Observable(Action action) {
        this.action = action;
        executor = Executors.newSingleThreadExecutor();
    }

    /**
     * 创建实例
     *
     * @param action 资源任务
     * @param 
     * @return
     */
    public static  Observable create(Action action) {
        Observable observable = new Observable<>(action);
        action.setObservable(observable);
        return observable;
    }

    /**
     * 执行异步获取资源任务
     */
    public void asyncAction() {
        executor.execute(action);
    }

    /**
     * 订阅
     * 如果action已经返回数据则同步执行,否则存起来等待通知
     *
     * @param subscriber
     */
    public void subscribe(Subscriber subscriber) {
        if (!isCompleted) {
            subscribers.add(subscriber);
        } else {
            subscriber.onExecute(action.getFuture());
        }
    }

    /**
     * 异步订阅
     * 返回数据通知执行
     *
     * @param future
     */
    private void executeSubscriber(T future) {
        for (Subscriber subscriber : subscribers) {
            subscriber.onExecute(future);
        }
    }

    /**
     * 静态内部类 异步任务
     *
     * @param 
     */
    public static class Action implements Runnable {

        /**
         * 持有执行此异步任务的任务源
         */
        private Observable observable;

        /**
         * 执行的任务
         */
        private Task task;

        /**
         * 任务执行完返回的数据
         */
        private T future;

        private Action(Task task) {
            this.task = task;
        }

        public static  Action create(Task task) {
            return new Action<>(task);
        }

        @Override
        public void run() {
            try {
                future = task.execute();
                observable.isCompleted = true;
            } catch (Exception e) {
                observable.isCompleted = false;
            }
            if (observable.isCompleted) {
                observable.executeSubscriber(future);
            }
        }


        private T getFuture() {
            return future;
        }

        private void setObservable(Observable observable) {
            this.observable = observable;
        }

    }

    public interface Task {
        T execute();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy