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

io.vertx.groovy.core.Future.groovy Maven / Gradle / Ivy

There is a newer version: 5.0.0.CR3
Show newest version
/*
 * Copyright 2014 Red Hat, Inc.
 *
 * Red Hat licenses this file to you 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 io.vertx.groovy.core;
import groovy.transform.CompileStatic
import io.vertx.lang.groovy.InternalHelper
import io.vertx.core.json.JsonObject
import io.vertx.core.AsyncResult
import io.vertx.core.Handler
/**
 * Represents the result of an action that may, or may not, have occurred yet.
 * 

*/ @CompileStatic public class Future { private final def io.vertx.core.Future delegate; public Future(Object delegate) { this.delegate = (io.vertx.core.Future) delegate; } public Object getDelegate() { return delegate; } /** * Create a future that hasn't completed yet * @return the future */ public static Future future() { def ret= InternalHelper.safeCreate(io.vertx.core.Future.future(), io.vertx.groovy.core.Future.class); return ret; } /** * Create a succeeded future with a null result * @return the future */ public static Future succeededFuture() { def ret= InternalHelper.safeCreate(io.vertx.core.Future.succeededFuture(), io.vertx.groovy.core.Future.class); return ret; } /** * Created a succeeded future with the specified result. * @param result the result * @return the future */ public static Future succeededFuture(T result) { def ret= InternalHelper.safeCreate(io.vertx.core.Future.succeededFuture(InternalHelper.unwrapObject(result)), io.vertx.groovy.core.Future.class); return ret; } /** * Create a failed future with the specified failure message. * @param failureMessage the failure message * @return the future */ public static Future failedFuture(String failureMessage) { def ret= InternalHelper.safeCreate(io.vertx.core.Future.failedFuture(failureMessage), io.vertx.groovy.core.Future.class); return ret; } /** * Has the future completed? *

* It's completed if it's either succeeded or failed. * @return true if completed, false if not */ public boolean isComplete() { def ret = this.delegate.isComplete(); return ret; } /** * Set a handler for the result. *

* If the future has already been completed it will be called immediately. Otherwise it will be called when the * future is completed. * @param handler the Handler that will be called with the result * @return a reference to this, so it can be used fluently */ public Future setHandler(Handler> handler) { this.delegate.setHandler(new Handler>() { public void handle(AsyncResult event) { AsyncResult f if (event.succeeded()) { f = InternalHelper.result(InternalHelper.wrapObject(event.result())) } else { f = InternalHelper.failure(event.cause()) } handler.handle(f) } }); return this; } /** * Set the result. Any handler will be called, if there is one, and the future will be marked as completed. * @param result the result */ public void complete(T result) { this.delegate.complete(InternalHelper.unwrapObject(result)); } /** * Set a null result. Any handler will be called, if there is one, and the future will be marked as completed. */ public void complete() { this.delegate.complete(); } /** * Set the failure. Any handler will be called, if there is one, and the future will be marked as completed. * @param throwable the failure cause */ public void fail(Throwable throwable) { this.delegate.fail(throwable); } /** * Set the failure. Any handler will be called, if there is one, and the future will be marked as completed. * @param failureMessage the failure message */ public void fail(String failureMessage) { this.delegate.fail(failureMessage); } /** * The result of the operation. This will be null if the operation failed. * @return the result or null if the operation failed. */ public T result() { // This cast is cleary flawed def ret = (T) InternalHelper.wrapObject(this.delegate.result()); return ret; } /** * A Throwable describing failure. This will be null if the operation succeeded. * @return the cause or null if the operation succeeded. */ public Throwable cause() { def ret = this.delegate.cause(); return ret; } /** * Did it succeed? * @return true if it succeded or false otherwise */ public boolean succeeded() { def ret = this.delegate.succeeded(); return ret; } /** * Did it fail? * @return true if it failed or false otherwise */ public boolean failed() { def ret = this.delegate.failed(); return ret; } /** * Compose this future with another future. * * When this future succeeds, the handler will be called with the value. * * When this future fails, the failure will be propagated to the next future. * @param handler the handler * @param next the next future */ public void compose(Handler handler, Future next) { this.delegate.compose(new Handler() { public void handle(Object event) { handler.handle(InternalHelper.wrapObject(event)) } }, (io.vertx.core.Future)next.getDelegate()); } /** * @return an handler completing this future * @return */ public Handler> completer() { if (cached_0 != null) { return cached_0; } def handlerDelegate = this.delegate.completer(); Handler> ret = new Handler>() { public void handle(AsyncResult event) { if (event.succeeded()) { handlerDelegate.handle(InternalHelper.result(InternalHelper.unwrapObject(event.result()))); } else { handlerDelegate.handle(InternalHelper.failure(event.cause())); } } }; cached_0 = ret; return ret; } private Handler> cached_0; }