481717 - Make Callback and Promise CompletableFuture-friendly.
Introduced methods from(CompletableFuture) and subclass Completable.
This commit is contained in:
parent
8ed67c009f
commit
b800ffc983
|
@ -16,24 +16,10 @@
|
|||
// ========================================================================
|
||||
//
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012 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 org.eclipse.jetty.util;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* <p>A callback abstraction that handles completed/failed events of asynchronous operations.</p>
|
||||
*
|
||||
|
@ -71,6 +57,56 @@ public interface Callback
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a non-blocking callback from the given incomplete CompletableFuture.</p>
|
||||
* <p>When the callback completes, either succeeding or failing, the
|
||||
* CompletableFuture is also completed, respectively via
|
||||
* {@link CompletableFuture#complete(Object)} or
|
||||
* {@link CompletableFuture#completeExceptionally(Throwable)}.</p>
|
||||
*
|
||||
* @param completable the CompletableFuture to convert into a callback
|
||||
* @return a callback that when completed, completes the given CompletableFuture
|
||||
*/
|
||||
static Callback from(CompletableFuture<?> completable)
|
||||
{
|
||||
return from(completable, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a callback from the given incomplete CompletableFuture,
|
||||
* with the given {@code blocking} characteristic.</p>
|
||||
*
|
||||
* @param completable the CompletableFuture to convert into a callback
|
||||
* @param blocking whether the callback is blocking
|
||||
* @return a callback that when completed, completes the given CompletableFuture
|
||||
*/
|
||||
static Callback from(CompletableFuture<?> completable, boolean blocking)
|
||||
{
|
||||
if (completable instanceof Callback)
|
||||
return (Callback)completable;
|
||||
|
||||
return new Callback()
|
||||
{
|
||||
@Override
|
||||
public void succeeded()
|
||||
{
|
||||
completable.complete(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable x)
|
||||
{
|
||||
completable.completeExceptionally(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNonBlocking()
|
||||
{
|
||||
return !blocking;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback interface that declares itself as non-blocking
|
||||
*/
|
||||
|
@ -82,4 +118,40 @@ public interface Callback
|
|||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>A CompletableFuture that is also a Callback.</p>
|
||||
*/
|
||||
class Completable extends CompletableFuture<Void> implements Callback
|
||||
{
|
||||
private final boolean blocking;
|
||||
|
||||
public Completable()
|
||||
{
|
||||
this(false);
|
||||
}
|
||||
|
||||
public Completable(boolean blocking)
|
||||
{
|
||||
this.blocking = blocking;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void succeeded()
|
||||
{
|
||||
complete(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable x)
|
||||
{
|
||||
completeExceptionally(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNonBlocking()
|
||||
{
|
||||
return !blocking;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
|
||||
package org.eclipse.jetty.util;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
|
||||
/**
|
||||
|
@ -33,25 +35,28 @@ public interface Promise<C>
|
|||
* @param result the context
|
||||
* @see #failed(Throwable)
|
||||
*/
|
||||
public abstract void succeeded(C result);
|
||||
default void succeeded(C result)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Callback invoked when the operation fails.</p>
|
||||
*
|
||||
* @param x the reason for the operation failure
|
||||
*/
|
||||
public void failed(Throwable x);
|
||||
|
||||
default void failed(Throwable x)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Empty implementation of {@link Promise}</p>
|
||||
* <p>Empty implementation of {@link Promise}.</p>
|
||||
*
|
||||
* @param <C> the type of the context object
|
||||
* @param <U> the type of the result
|
||||
*/
|
||||
public static class Adapter<C> implements Promise<C>
|
||||
class Adapter<U> implements Promise<U>
|
||||
{
|
||||
@Override
|
||||
public void succeeded(C result)
|
||||
public void succeeded(U result)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -62,4 +67,54 @@ public interface Promise<C>
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Creates a promise from the given incomplete CompletableFuture.</p>
|
||||
* <p>When the promise completes, either succeeding or failing, the
|
||||
* CompletableFuture is also completed, respectively via
|
||||
* {@link CompletableFuture#complete(Object)} or
|
||||
* {@link CompletableFuture#completeExceptionally(Throwable)}.</p>
|
||||
*
|
||||
* @param completable the CompletableFuture to convert into a promise
|
||||
* @return a promise that when completed, completes the given CompletableFuture
|
||||
*/
|
||||
static <T> Promise<T> from(CompletableFuture<? super T> completable)
|
||||
{
|
||||
if (completable instanceof Promise)
|
||||
return (Promise<T>)completable;
|
||||
|
||||
return new Promise<T>()
|
||||
{
|
||||
@Override
|
||||
public void succeeded(T result)
|
||||
{
|
||||
completable.complete(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable x)
|
||||
{
|
||||
completable.completeExceptionally(x);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>A CompletableFuture that is also a Promise.</p>
|
||||
*
|
||||
* @param <S> the type of the result
|
||||
*/
|
||||
class Completable<S> extends CompletableFuture<S> implements Promise<S>
|
||||
{
|
||||
@Override
|
||||
public void succeeded(S result)
|
||||
{
|
||||
complete(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable x)
|
||||
{
|
||||
completeExceptionally(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue