From c1c70760dd19b72833484eb93779c792bc3dfdd0 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 23 Mar 2024 17:29:06 +0800 Subject: [PATCH] =?UTF-8?q?docs/demo:=20add=20demo=20to=20integrate=20an?= =?UTF-8?q?=20existed=20executor=20wrapper=20without=20modification=20?= =?UTF-8?q?=F0=9F=93=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 102 +++++++++++++++++- .../integration/ExistedExecutorWrapper.java | 24 +++++ .../demo/integration/IntegrationDemo.java | 76 +++++++++++++ 3 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/foldright/demo/integration/ExistedExecutorWrapper.java create mode 100644 src/test/java/io/foldright/demo/integration/IntegrationDemo.java diff --git a/README.md b/README.md index 4d0116e..5beec0b 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,9 @@ The purpose of **Inspectable Wrappers** is to provide a standard for wrapper cha - [🌰 Demo](#-demo) - [wrapper implementations in your application code](#wrapper-implementations-in-your-application-code) - [inspection of the wrapper chain](#inspection-of-the-wrapper-chain) +- [Integrate an existed executor wrapper without modification](#integrate-an-existed-executor-wrapper-without-modification) + - [the demo existed wrapper cannot be modified](#the-demo-existed-wrapper-cannot-be-modified) + - [the integration demo](#the-integration-demo) - [🍼 Java API Docs](#-java-api-docs) - [🍪 Dependency](#-dependency) @@ -44,7 +47,7 @@ The purpose of **Inspectable Wrappers** is to provide a standard for wrapper cha - [`Attachable`](src/main/java/io/foldright/inspectablewrappers/Attachable.java) interface is used to enhance the wrapper instances with the attachment storage ability -## 🌰 Demo +## 🌰 Usage Demo Below use the `Executor Wrapper` to demonstrate the usage. @@ -160,6 +163,103 @@ I'm working. > Runnable demo codes in project: [`Demo.java`](src/test/java/io/foldright/demo/Demo.java) +## 🌰 Integration Demo + +Integrate an existed executor wrapper without modification. + +### the demo existed wrapper cannot be modified + +```java +public class ExistedExecutorWrapper implements Executor { + private final Executor executor; + + public ExistedExecutorWrapper(Executor executor) { + this.executor = executor; + } + + public Executor getExecutor() { + return executor; + } + + @Override + public void execute(@NotNull Runnable command) { + System.out.println("I'm existed executor, have nothing to do with ~inspectable~wrappers~."); + executor.execute(command); + } +} +``` + +### the integration demo + +```java +public class IntegrationDemo { + public static void main(String[] args) { + final Executor executor = buildExecutorChain(); + + //////////////////////////////////////// + // inspect the executor(wrapper chain) + //////////////////////////////////////// + + System.out.println("Is executor chatty? " + + Wrapper.isInstanceOf(executor, ChattyExecutorWrapper.class)); + // print true + System.out.println("Is executor IntegrateExistedExecutor? " + + Wrapper.isInstanceOf(executor, IntegrateExistedExecutorWrapper.class)); + // print true + + //////////////////////////////////////// + // call executor(wrapper chain) + //////////////////////////////////////// + + System.out.println(); + executor.execute(() -> System.out.println("I'm working.")); + } + + private static Executor buildExecutorChain() { + final Executor base = Runnable::run; + + final ExistedExecutorWrapper existed = new ExistedExecutorWrapper(base); + final IntegrateExistedExecutorWrapper integrate = new IntegrateExistedExecutorWrapper(existed); + + return new ChattyExecutorWrapper(integrate); + } + + /** + * Integrate an existed executor wrapper(`ExistedExecutorWrapper`) without modification + */ + private static class IntegrateExistedExecutorWrapper implements Executor, Wrapper { + private final ExistedExecutorWrapper existedExecutorWrapper; + + public IntegrateExistedExecutorWrapper(ExistedExecutorWrapper existedExecutorWrapper) { + this.existedExecutorWrapper = existedExecutorWrapper; + } + + @Override + public Executor unwrap() { + return existedExecutorWrapper.getExecutor(); + } + + @Override + public void execute(Runnable command) { + existedExecutorWrapper.execute(command); + } + } +} + +/* +demo output: + +Is executor chatty? true +Is executor IntegrateExistedExecutor? true + +BlaBlaBla... +I'm existed executor, have nothing to do with ~inspectable~wrappers~. +I'm working. + */ +``` + +> Runnable demo codes in project: [`IntegrationDemo.java`](src/test/java/io/foldright/demo/integration/IntegrationDemo.java) + ## 🍼 Java API Docs The current version Java API documentation: diff --git a/src/test/java/io/foldright/demo/integration/ExistedExecutorWrapper.java b/src/test/java/io/foldright/demo/integration/ExistedExecutorWrapper.java new file mode 100644 index 0000000..e33ecf7 --- /dev/null +++ b/src/test/java/io/foldright/demo/integration/ExistedExecutorWrapper.java @@ -0,0 +1,24 @@ +package io.foldright.demo.integration; + +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.Executor; + + +public class ExistedExecutorWrapper implements Executor { + private final Executor executor; + + public ExistedExecutorWrapper(Executor executor) { + this.executor = executor; + } + + public Executor getExecutor() { + return executor; + } + + @Override + public void execute(@NotNull Runnable command) { + System.out.println("I'm existed executor, have nothing to do with ~inspectable~wrappers~."); + executor.execute(command); + } +} diff --git a/src/test/java/io/foldright/demo/integration/IntegrationDemo.java b/src/test/java/io/foldright/demo/integration/IntegrationDemo.java new file mode 100644 index 0000000..6dc8f4b --- /dev/null +++ b/src/test/java/io/foldright/demo/integration/IntegrationDemo.java @@ -0,0 +1,76 @@ +package io.foldright.demo.integration; + +import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault; +import io.foldright.demo.ChattyExecutorWrapper; +import io.foldright.inspectablewrappers.Wrapper; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.util.concurrent.Executor; + + +@ParametersAreNonnullByDefault +@ReturnValuesAreNonnullByDefault +public class IntegrationDemo { + public static void main(String[] args) { + final Executor executor = buildExecutorChain(); + + //////////////////////////////////////// + // inspect the executor(wrapper chain) + //////////////////////////////////////// + + System.out.println("Is executor chatty? " + + Wrapper.isInstanceOf(executor, ChattyExecutorWrapper.class)); + // print true + System.out.println("Is executor IntegrateExistedExecutor? " + + Wrapper.isInstanceOf(executor, IntegrateExistedExecutorWrapper.class)); + // print true + + //////////////////////////////////////// + // call executor(wrapper chain) + //////////////////////////////////////// + + System.out.println(); + executor.execute(() -> System.out.println("I'm working.")); + } + + private static Executor buildExecutorChain() { + final Executor base = Runnable::run; + + final ExistedExecutorWrapper existed = new ExistedExecutorWrapper(base); + final IntegrateExistedExecutorWrapper integrate = new IntegrateExistedExecutorWrapper(existed); + + return new ChattyExecutorWrapper(integrate); + } + + /** + * Integrate an existed executor wrapper(`ExistedExecutorWrapper`) without modification + */ + private static class IntegrateExistedExecutorWrapper implements Executor, Wrapper { + private final ExistedExecutorWrapper existedExecutorWrapper; + + public IntegrateExistedExecutorWrapper(ExistedExecutorWrapper existedExecutorWrapper) { + this.existedExecutorWrapper = existedExecutorWrapper; + } + + @Override + public Executor unwrap() { + return existedExecutorWrapper.getExecutor(); + } + + @Override + public void execute(Runnable command) { + existedExecutorWrapper.execute(command); + } + } +} + +/* +demo output: + +Is executor chatty? true +Is executor IntegrateExistedExecutor? true + +BlaBlaBla... +I'm existed executor, have nothing to do with ~inspectable~wrappers~. +I'm working. + */