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

ratpack.exec.internal.DefaultPromise Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * Copyright 2014 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.internal;

import ratpack.exec.*;
import ratpack.func.Action;
import ratpack.func.Function;
import ratpack.func.NoArgAction;
import ratpack.func.Predicate;

import java.util.function.Consumer;
import java.util.function.Supplier;

public class DefaultPromise implements Promise {
  private final Consumer> fulfillment;
  private final Supplier executionProvider;

  public DefaultPromise(Supplier executionProvider, Consumer> fulfillment) {
    this.executionProvider = executionProvider;
    this.fulfillment = fulfillment;
  }

  @Override
  public SuccessPromise onError(final Action errorHandler) {
    return new DefaultSuccessPromise<>(executionProvider, fulfillment, errorHandler);
  }

  @Override
  public void then(Action then) {
    propagatingSuccessPromise().then(then);
  }

  private SuccessPromise propagatingSuccessPromise() {
    return onError(Action.throwException());
  }

  @Override
  public  Promise map(Function transformer) {
    return propagatingSuccessPromise().map(transformer);
  }

  @Override
  public  Promise blockingMap(Function transformer) {
    return propagatingSuccessPromise().blockingMap(transformer);
  }

  @Override
  public  Promise flatMap(Function> transformer) {
    return propagatingSuccessPromise().flatMap(transformer);
  }

  @Override
  public Promise route(Predicate predicate, Action action) {
    return propagatingSuccessPromise().route(predicate, action);
  }

  @Override
  public Promise onNull(NoArgAction action) {
    return propagatingSuccessPromise().onNull(action);
  }

  @Override
  public Promise cache() {
    return propagatingSuccessPromise().cache();
  }

  @Override
  public Promise defer(Action releaser) {
    return propagatingSuccessPromise().defer(releaser);
  }

  @Override
  public Promise onYield(Runnable onYield) {
    return propagatingSuccessPromise().onYield(onYield);
  }

  @Override
  public Promise wiretap(Action> listener) {
    return propagatingSuccessPromise().wiretap(listener);
  }

  @Override
  public Promise throttled(Throttle throttle) {
    return propagatingSuccessPromise().throttled(throttle);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy