001/*
002 * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
003 *
004 *  Permission is hereby granted, free of charge, to any person obtaining
005 *  a copy of this software and associated documentation files (the
006 *  "Software"), to deal in the Software without restriction, including
007 *  without limitation the rights to use, copy, modify, merge, publish,
008 *  distribute, sublicense, and/or sell copies of the Software, and to
009 *  permit persons to whom the Software is furnished to do so, subject to
010 *  the following conditions:
011 *
012 *  The above copyright notice and this permission notice shall be
013 *  included in all copies or substantial portions of the Software.
014 *
015 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
016 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
017 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
018 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
019 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
020 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
021 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
022 */
023
024package co.aikar.taskchain;
025
026import java.util.concurrent.CompletableFuture;
027import java.util.function.Consumer;
028
029@SuppressWarnings("WeakerAccess")
030public class TaskChainTasks {
031    /**
032     * Generic task with synchronous return (but may execute on any thread)
033     *
034     * @param <R>
035     * @param <A>
036     */
037    public interface Task<R, A> {
038        /**
039         * @see TaskChain#getCurrentChain()
040         */
041        default TaskChain<?> getCurrentChain() {
042            return TaskChain.getCurrentChain();
043        }
044
045        R run(A input);
046    }
047
048    /**
049     * A task that expects no input, and returns a value.
050     * Likely to be the first task in the chain
051     */
052    public interface FirstTask<R> extends Task<R, Object> {
053        @Override
054        default R run(Object input) {
055            return run();
056        }
057
058        R run();
059    }
060
061
062    /**
063     * A task that expects input, but will not provide a response.
064     * Likely to be the last task in the chain
065     * @param <A>
066     */
067    public interface LastTask<A> extends Task<Object, A> {
068        @Override
069        default Object run(A input) {
070            runLast(input);
071            return null;
072        }
073
074        void runLast(A input);
075    }
076
077    /**
078     * A task that expects no input or output
079     */
080    public interface GenericTask extends Task<Object, Object> {
081        @Override
082        default Object run(Object input) {
083            runGeneric();
084            return null;
085        }
086
087        void runGeneric();
088    }
089
090    /**
091     * A task that returns a future to be completed later. Takes input from chain and when the future completes,
092     * the value will be passed to the next task.
093     *
094     * @param <R> The type the Future will complete with
095     * @param <A> The type from the previous task
096     */
097    public interface FutureTask<R, A> extends Task<R, A> {
098        @Override
099        default R run(A input) {
100            return null;
101        }
102
103        CompletableFuture<R> runFuture(A input);
104    }
105
106    /**
107     * A Task that returns a future to be completed later. Expects no input, and  when the future completes,
108     * the value will be passed to the next task.
109     */
110    public interface FutureFirstTask<R> extends FutureTask<R, Object> {
111        @Override
112        default CompletableFuture<R> runFuture(Object input) {
113            return runFuture();
114        }
115
116        CompletableFuture<R> runFuture();
117    }
118
119
120    /**
121     * A generic task that expects no input or output, but uses a future style API
122     * to control moving to the next task.
123     */
124    public interface FutureGenericTask extends FutureTask<Object, Object> {
125        @Override
126        default CompletableFuture<Object> runFuture(Object input) {
127            return runFuture();
128        }
129
130        CompletableFuture<Object> runFuture();
131    }
132
133    /**
134     * A task that does not return immediately. A supplied Consumer controls when
135     * the chain should proceed to the next task, and providing a response to be passed
136     * to the next task. This is a Callback style API in relation to the Future based API.
137     */
138    public interface AsyncExecutingTask<R, A> extends Task<R, A> {
139        @Override
140        default R run(A input) {
141            // unused
142            return null;
143        }
144
145        void runAsync(A input, Consumer<R> next);
146    }
147
148    /**
149     * @see AsyncExecutingTask
150     * @see FirstTask
151     */
152    public interface AsyncExecutingFirstTask<R> extends AsyncExecutingTask<R, Object> {
153        @Override
154        default R run(Object input) {
155            // Unused
156            return null;
157        }
158
159        @Override
160        default void runAsync(Object input, Consumer<R> next) {
161            run(next);
162        }
163
164        void run(Consumer<R> next);
165    }
166
167    /**
168     * @see AsyncExecutingTask
169     * @see GenericTask
170     */
171    public interface AsyncExecutingGenericTask extends AsyncExecutingTask<Object, Object> {
172        @Override
173        default Object run(Object input) {
174            return null;
175        }
176
177        @Override
178        default void runAsync(Object input, Consumer<Object> next) {
179            run(() -> next.accept(null));
180        }
181
182        void run(Runnable next);
183    }
184}