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

com.arangodb.shaded.vertx.core.impl.future.FutureBase Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
/*
 * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
 * which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */

package com.arangodb.shaded.vertx.core.impl.future;

import com.arangodb.shaded.vertx.core.AsyncResult;
import com.arangodb.shaded.vertx.core.Future;
import com.arangodb.shaded.vertx.core.Handler;
import com.arangodb.shaded.vertx.core.impl.ContextInternal;

import java.util.Objects;
import java.util.function.Function;

/**
 * Future base implementation.
 *
 * @author Julien Viet
 */
abstract class FutureBase implements FutureInternal {

  protected final ContextInternal context;

  /**
   * Create a future that hasn't completed yet
   */
  FutureBase() {
    this(null);
  }

  /**
   * Create a future that hasn't completed yet
   */
  FutureBase(ContextInternal context) {
    this.context = context;
  }

  public final ContextInternal context() {
    return context;
  }

  protected final void emitSuccess(T value, Listener listener) {
    if (context != null && !context.isRunningOnContext()) {
      context.execute(() -> {
        ContextInternal prev = context.beginDispatch();
        try {
          listener.onSuccess(value);
        } finally {
          context.endDispatch(prev);
        }
      });
    } else {
      listener.onSuccess(value);
    }
  }

  protected final void emitFailure(Throwable cause, Listener listener) {
    if (context != null && !context.isRunningOnContext()) {
      context.execute(() -> {
        ContextInternal prev = context.beginDispatch();
        try {
          listener.onFailure(cause);
        } finally {
          context.endDispatch(prev);
        }
      });
    } else {
      listener.onFailure(cause);
    }
  }

  @Override
  public  Future compose(Function> successMapper, Function> failureMapper) {
    Objects.requireNonNull(successMapper, "No null success mapper accepted");
    Objects.requireNonNull(failureMapper, "No null failure mapper accepted");
    Composition operation = new Composition<>(context, successMapper, failureMapper);
    addListener(operation);
    return operation;
  }

  @Override
  public  Future transform(Function, Future> mapper) {
    Objects.requireNonNull(mapper, "No null mapper accepted");
    Transformation operation = new Transformation<>(context, this, mapper);
    addListener(operation);
    return operation;
  }

  @Override
  public  Future eventually(Function> mapper) {
    Objects.requireNonNull(mapper, "No null mapper accepted");
    Eventually operation = new Eventually<>(context, mapper);
    addListener(operation);
    return operation;
  }

  @Override
  public  Future map(Function mapper) {
    Objects.requireNonNull(mapper, "No null mapper accepted");
    Mapping operation = new Mapping<>(context, mapper);
    addListener(operation);
    return operation;
  }

  @Override
  public  Future map(V value) {
    FixedMapping transformation = new FixedMapping<>(context, value);
    addListener(transformation);
    return transformation;
  }

  @Override
  public Future otherwise(Function mapper) {
    Objects.requireNonNull(mapper, "No null mapper accepted");
    Otherwise transformation = new Otherwise<>(context, mapper);
    addListener(transformation);
    return transformation;
  }

  @Override
  public Future otherwise(T value) {
    FixedOtherwise operation = new FixedOtherwise<>(context, value);
    addListener(operation);
    return operation;
  }
}