/* * Copyright 2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ratpack.exec; import ratpack.exec.api.NonBlocking; import ratpack.func.Action; import ratpack.func.Block; import ratpack.func.Factory; import ratpack.func.Function; import ratpack.func.Predicate; import java.util.Optional; /** * A logical operation. *

* An operation encapsulates a logical piece of work, which will complete some time in the future. * It is similar to a {@link Promise} except that it does not produce a value. * It merely succeeds, or throws an exception. *

* The {@link #then(Block)} method allows specifying what should happen after the operation completes. * The {@link #onError(Action)} method allows specifying what should happen if the operation fails. * Like {@link Promise}, the operation will not start until it is subscribed to, via {@link #then(Block)} or {@link #then()}. *

* It is common for methods that would naturally return {@code void} to return an {@link Operation} instead, * to allow the method implementation to be effectively asynchronous. * The caller of the method is then expected to use the {@link #then(Block)} method to specify what should happen after the operation * that the method represents finishes. *

{@code
 * import ratpack.exec.Blocking;
 * import ratpack.exec.Operation;
 * import com.google.common.collect.Lists;
 * import ratpack.test.exec.ExecHarness;
 *
 * import java.util.Arrays;
 * import java.util.List;
 *
 * import static org.junit.Assert.assertEquals;
 *
 * public class Example {
 *   public static void main(String... args) throws Exception {
 *     List events = Lists.newArrayList();
 *     ExecHarness.runSingle(e ->
 *       Operation.of(() ->
 *         Blocking.get(() -> events.add("1"))
 *           .then(b -> events.add("2"))
 *       )
 *       .then(() -> events.add("3"))
 *     );
 *     assertEquals(Arrays.asList("1", "2", "3"), events);
 *   }
 * }
 * }
*/ public interface Operation { static Operation of(Block block) { return null; } static Operation flatten(Factory factory) { return null; } default Operation onError(Action onError) { return null; } Operation onError(Predicate predicate, Action errorHandler); default Operation onError(Class errorType, Action errorHandler) { return null; } default Operation mapError(Action action) { return null; } @NonBlocking void then(Block block); @NonBlocking default void then() { // empty } Promise promise(); default Promise map(Factory factory) { return null; } default Promise flatMap(Factory> factory) { return null; } default Promise flatMap(Promise promise) { return null; } default Operation next(Operation operation) { return null; } default Operation next(Block operation) { return null; } default Operation blockingNext(Block operation) { return null; } default O to(Function function) throws Exception { return null; } default Operation wiretap(Action> action) { return null; } static Operation noop() { return null; } }