diff --git a/src/org/redkale/util/AsyncHandler.java b/src/org/redkale/util/AsyncHandler.java new file mode 100644 index 000000000..03c2483c1 --- /dev/null +++ b/src/org/redkale/util/AsyncHandler.java @@ -0,0 +1,97 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.util; + +import java.util.function.BiConsumer; +import java.nio.channels.CompletionHandler; +import java.util.function.*; + +/** + * 异步回调函数 + * + * + *

+ * 详情见: https://redkale.org + * + * @deprecated 使用 java.nio.channels.CompletionHandler 代替 + * @author zhangjx + * @param 结果对象的泛型 + * @param 附件对象的泛型 + */ +@Deprecated +public interface AsyncHandler extends CompletionHandler { + + /** + * 创建 AsyncHandler 对象 + * + * @param 结果对象的泛型 + * @param 附件对象的泛型 + * @param success 成功的回调函数 + * @param fail 失败的回调函数 + * + * @return AsyncHandler + */ + public static AsyncHandler create(final BiConsumer success, final BiConsumer fail) { + return new AsyncHandler() { + @Override + public void completed(V result, A attachment) { + if (success != null) success.accept(result, attachment); + } + + @Override + public void failed(Throwable exc, A attachment) { + if (fail != null) fail.accept(exc, attachment); + } + }; + } + + /** + * 创建没有返回结果的 AsyncHandler 对象 + * + * @param 附件对象的泛型 + * @param success 成功的回调函数 + * @param fail 失败的回调函数 + * + * @return AsyncHandler + */ + public static AsyncHandler create(final Consumer success, final BiConsumer fail) { + return new AsyncHandler() { + @Override + public void completed(Void result, A attachment) { + if (success != null) success.accept(attachment); + } + + @Override + public void failed(Throwable exc, A attachment) { + if (fail != null) fail.accept(exc, attachment); + } + }; + } + + /** + * 创建没有附件对象的 AsyncNoResultHandler 对象 + * + * @param 结果对象的泛型 + * @param success 成功的回调函数 + * @param fail 失败的回调函数 + * + * @return AsyncHandler + */ + public static AsyncHandler create(final Consumer success, final Consumer fail) { + return new AsyncHandler() { + @Override + public void completed(V result, Void attachment) { + if (success != null) success.accept(result); + } + + @Override + public void failed(Throwable exc, Void attachment) { + if (fail != null) fail.accept(exc); + } + }; + } + +}