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

com.arangodb.shaded.vertx.core.impl.future.FailedFuture 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 com.arangodb.shaded.vertx.core.impl.NoStackTraceThrowable;

import java.util.function.Function;

/**
 * Failed future implementation.
 *
 * @author Julien Viet
 */
public final class FailedFuture extends FutureBase {

  private final Throwable cause;

  /**
   * Create a future that has already failed
   * @param t the throwable
   */
  public FailedFuture(Throwable t) {
    this(null, t);
  }

  /**
   * Create a future that has already failed
   * @param t the throwable
   */
  public FailedFuture(ContextInternal context, Throwable t) {
    super(context);
    this.cause = t != null ? t : new NoStackTraceThrowable(null);
  }

  /**
   * Create a future that has already failed
   * @param failureMessage the failure message
   */
  public FailedFuture(String failureMessage) {
    this(null, failureMessage);
  }

  /**
   * Create a future that has already failed
   * @param failureMessage the failure message
   */
  public FailedFuture(ContextInternal context, String failureMessage) {
    this(context, new NoStackTraceThrowable(failureMessage));
  }

  @Override
  public boolean isComplete() {
    return true;
  }

  @Override
  public Future onComplete(Handler> handler) {
    if (handler instanceof Listener) {
      emitFailure(cause, (Listener) handler);
    } else if (context != null) {
      context.emit(this, handler);
    } else {
      handler.handle(this);
    }
    return this;
  }

  @Override
  public Future onSuccess(Handler handler) {
    return this;
  }

  @Override
  public Future onFailure(Handler handler) {
    if (context != null) {
      context.emit(cause, handler);
    } else {
      handler.handle(cause);
    }
    return this;
  }

  @Override
  public void addListener(Listener listener) {
    emitFailure(cause, listener);
  }

  @Override
  public T result() {
    return null;
  }

  @Override
  public Throwable cause() {
    return cause;
  }

  @Override
  public boolean succeeded() {
    return false;
  }

  @Override
  public boolean failed() {
    return true;
  }

  @Override
  public  Future map(Function mapper) {
    return (Future) this;
  }

  @Override
  public  Future map(V value) {
    return (Future) this;
  }

  @Override
  public Future otherwise(T value) {
    return new SucceededFuture<>(context, value);
  }

  @Override
  public String toString() {
    return "Future{cause=" + cause.getMessage() + "}";
  }
}