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

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

/**
 * Succeeded future implementation.
 *
 * @author Julien Viet
 */
public final class SucceededFuture extends FutureBase {

  /**
   * Stateless instance of empty results that can be shared safely.
   */
  public static final SucceededFuture EMPTY = new SucceededFuture(null, null);

  private final T result;

  /**
   * Create a future that has already succeeded
   * @param result the result
   */
  public SucceededFuture(T result) {
    this(null, result);
  }

  /**
   * Create a future that has already succeeded
   * @param context the context
   * @param result the result
   */
  public SucceededFuture(ContextInternal context, T result) {
    super(context);
    this.result = result;
  }

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

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

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

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

  @Override
  public void addListener(Listener listener) {
    emitSuccess(result ,listener);
  }

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

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

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

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

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

  @Override
  public Future otherwise(Function mapper) {
    Objects.requireNonNull(mapper, "No null mapper accepted");
    return this;
  }

  @Override
  public Future otherwise(T value) {
    return this;
  }

  @Override
  public String toString() {
    return "Future{result=" + result + "}";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy