Java Pipeline自定义实现

417 阅读1分钟

Java Pipeline自定义实现

先运行看结果

package com.zzqfsy.struture.pipeline;

import java.util.Date;

public class PipelineDemo {

    public static void main(String[] args) {
        Runnable p = new InitPipeline<>(() -> {
                    Date date = new Date();
                    return date;
                })
                .add(d -> {
                    System.out.println("input1:" + d);

                    try { Thread.sleep(1000);} catch (InterruptedException e) { throw new RuntimeException(e);}

                    d = new Date();
                    return d;
                })
                .add(d -> {
                    System.out.println("input2:" + d);

                    try { Thread.sleep(1000);} catch (InterruptedException e) { throw new RuntimeException(e);}

                    d = new Date();
                    return d;
                })
                .end(s -> {
                    System.err.println("input3:" + s);
                });

        p.run();
    }
}

输出结果

input1:Wed Nov 23 22:37:42 CST 2022
input2:Wed Nov 23 22:37:43 CST 2022
input3:Wed Nov 23 22:37:44 CST 2022

核心实现

InitPipeline -> MiddlePipeline * N -> EndPipeline InitPipeline -> EndPipeline 维护管道之间的链表关系即可

最终在run层面,从head开始执行

核心设计

InitPipeline

public class InitPipeline<R> extends AbstractPipeline {

    protected ICallable<R> task = null;

    protected MiddlePipeline<R, ?> next = null;

    protected EndPipeline<R> end = null;

    protected Object[] argCxt = null;

    public InitPipeline(ICallable<R> t) {
        this.task = t;
        this.head = this;
    }

    /**
     * 增加节点
     *
     * @param nextTask
     * @param <RT>
     * @return
     */
    public <RT> MiddlePipeline<R, RT> add(ICallable1<RT, R> nextTask) {
        if (null == next && null == end) {
            MiddlePipeline<R, RT> n = new MiddlePipeline<>(nextTask);
            this.next = n;
            n.head = this;
            return n;
        }
        throw new RuntimeException("Unsupport multi pipeline!!!");
    }

    /**
     * 结束节点
     *
     * @param nextTask
     * @return
     */
    public Runnable end(IRunnable1<R> nextTask) {
        if (null == next && null == end) {
            EndPipeline<R> e = new EndPipeline<>(nextTask);
            this.end = e;
            this.head = this;
            return e;
        }
        throw new RuntimeException("Unsupport multi pipeline!!!");
    }

    /**
     * 运行
     */
    @Override
    public void run() {
        MiddlePipeline nextPipeLine = next;
        // 前置节点
        MiddlePipeline prePipeline = null;
        try {
            // head execute
            Object r = task.call();

            // mid execute
            while (null != nextPipeLine) {
                r = nextPipeLine.task.call(r);
                prePipeline = nextPipeLine;
                nextPipeLine = nextPipeLine.next;
            }

            // end execute
            if (null != prePipeline) {
                EndPipeline endPipeline = prePipeline.end;
                if (null != endPipeline)
                    endPipeline.task.call(r);
            } else {
                if (null != this.end)
                    this.end.task.call((R) r);
            }
        } catch (Exception e) {
            throw new RuntimeException("Pipeline exec faild!!!", e);
        }
    }

}

MiddlePipeline

public class MiddlePipeline<C, P> extends AbstractPipeline {

    protected final ICallable1<P, C> task;

    protected MiddlePipeline<P, ?> next = null;

    protected EndPipeline<P> end = null;

    protected Object[] argCxt = new Object[]{null, null, null, null, null, null};

    protected MiddlePipeline(ICallable1<P, C> task) {
        this.task = task;
    }

    /**
     * 追加中间节点
     *
     * @param nextTaskx
     * @param <RT>
     * @return
     */
    public <RT> MiddlePipeline<P, RT> add(ICallable1<RT, P> nextTaskx) {
        if (null == next && null == end) {
            MiddlePipeline<P, RT> n = new MiddlePipeline<>(r -> {
                argCxt[0] = r;
                return nextTaskx.call(r);
            });
            this.next = n;
            n.head = this.head;
            return n;
        }
        throw new RuntimeException("Unsupport multi pipeline!!!");
    }

    /**
     * 结束节点
     *
     * @param nextTask
     * @return
     */
    public Runnable end(IRunnable1<P> nextTask) {
        if (null == next && null == end) {
            EndPipeline<P> e = new EndPipeline<>(r -> {
                argCxt[0] = r;
                nextTask.call(r);
            });
            this.end = e;
            e.head = this.head;
            return e;
        }
        throw new RuntimeException("Unsupport multi pipeline!!!");
    }
}

EndPipeline

public class EndPipeline<C> extends AbstractPipeline {

    protected final IRunnable1<C> task;

    public EndPipeline(IRunnable1<C> task) {
        this.task = task;
    }

}

AbstractPipeline

public abstract class AbstractPipeline<R> implements Runnable {
    protected InitPipeline<R> head = null;

    @Override
    public void run() {
        head.run();
    }
}