diff --git a/README.md b/README.md index 8f51a35..8b397f5 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ The purpose of **Inspectable Wrappers** is to provide a standard for wrapper cha - [🌰 Integration Demo](#-integration-demo) - [the demo existed wrapper cannot be modified](#the-demo-existed-wrapper-cannot-be-modified) - [the integration code](#the-integration-code) +- [🌰 Integration Demo using `WrapperAdapterUtils`](#-integration-demo-using-wrapperadapterutils) - [🍼 Java API Docs](#-java-api-docs) - [🍪 Dependency](#-dependency) @@ -290,6 +291,64 @@ I'm working. > Runnable demo codes in project: [`IntegrationDemo.java`](src/test/java/io/foldright/demo/integration/IntegrationDemo.java) +## 🌰 Integration Demo using `WrapperAdapterUtils` + +Uses `WrapperAdapterUtils` to create `WrapperAdapter` instances without writing the boilerplate code to create a new adapter class. + +```java +public class IntegrationDemoUsingWrapperAdapterUtils { + public static void main(String[] args) { + final Executor executor = buildExecutorChain(); + + //////////////////////////////////////// + // inspect the executor(wrapper chain) + //////////////////////////////////////// + + System.out.println("Is executor ExistedExecutorWrapper? " + + containsInstanceOnWrapperChain(executor, ExistedExecutorWrapper.class)); + // print true + String adaptAttachment = getAttachmentFromWrapperChain(executor, "adapted-existed-executor-wrapper-msg"); + System.out.println("Adapted existed executor wrapper msg: " + adaptAttachment); + // print "I'm an adapter of an existed executor which have nothing to do with ~inspectable~wrappers~." + + //////////////////////////////////////// + // 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 Executor adapter = createExistedExecutorWrapperAdapter(base); + return new ChattyExecutorWrapper(adapter); + } + + private static Executor createExistedExecutorWrapperAdapter(Executor base) { + final Executor existed = new ExistedExecutorWrapper(base); + + Attachable attachable = new AttachableDelegate<>(); + attachable.setAttachment("adapted-existed-executor-wrapper-msg", "I'm an adapter of an existed executor which have nothing to do with ~inspectable~wrappers~."); + + return WrapperAdapterUtils.createWrapperAdapter(Executor.class, base, existed, attachable); + } +} + +/* +demo output: + +Is executor ExistedExecutorWrapper? true +Adapted existed executor wrapper msg: I'm an adapter of an existed executor which have nothing to do with ~inspectable~wrappers~. + +BlaBlaBla... +I'm an adapter of an existed executor which have nothing to do with ~inspectable~wrappers~. +I'm working. + */ +``` + +> Runnable demo codes in project: [`IntegrationDemoUsingWrapperAdapterUtils.java`](src/test/java/io/foldright/demo/integration/IntegrationDemoUsingWrapperAdapterUtils.java) + ## 🍼 Java API Docs The current version Java API documentation: diff --git a/src/test/java/io/foldright/demo/integration/IntegrationDemoUsingWrapperAdapterUtils.java b/src/test/java/io/foldright/demo/integration/IntegrationDemoUsingWrapperAdapterUtils.java new file mode 100644 index 0000000..9d0c227 --- /dev/null +++ b/src/test/java/io/foldright/demo/integration/IntegrationDemoUsingWrapperAdapterUtils.java @@ -0,0 +1,62 @@ +package io.foldright.demo.integration; + +import io.foldright.demo.ChattyExecutorWrapper; +import io.foldright.inspectablewrappers.Attachable; +import io.foldright.inspectablewrappers.utils.AttachableDelegate; +import io.foldright.inspectablewrappers.utils.WrapperAdapterUtils; + +import java.util.concurrent.Executor; + +import static io.foldright.inspectablewrappers.Inspector.containsInstanceOnWrapperChain; +import static io.foldright.inspectablewrappers.Inspector.getAttachmentFromWrapperChain; + + +public class IntegrationDemoUsingWrapperAdapterUtils { + public static void main(String[] args) { + final Executor executor = buildExecutorChain(); + + //////////////////////////////////////// + // inspect the executor(wrapper chain) + //////////////////////////////////////// + + System.out.println("Is executor ExistedExecutorWrapper? " + + containsInstanceOnWrapperChain(executor, ExistedExecutorWrapper.class)); + // print true + String adaptAttachment = getAttachmentFromWrapperChain(executor, "adapted-existed-executor-wrapper-msg"); + System.out.println("Adapted existed executor wrapper msg: " + adaptAttachment); + // print "I'm an adapter of an existed executor which have nothing to do with ~inspectable~wrappers~." + + //////////////////////////////////////// + // 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 Executor adapter = createExistedExecutorWrapperAdapter(base); + return new ChattyExecutorWrapper(adapter); + } + + private static Executor createExistedExecutorWrapperAdapter(Executor base) { + final Executor existed = new ExistedExecutorWrapper(base); + + Attachable attachable = new AttachableDelegate<>(); + attachable.setAttachment("adapted-existed-executor-wrapper-msg", "I'm an adapter of an existed executor which have nothing to do with ~inspectable~wrappers~."); + + return WrapperAdapterUtils.createWrapperAdapter(Executor.class, base, existed, attachable); + } +} + +/* +demo output: + +Is executor ExistedExecutorWrapper? true +Adapted existed executor wrapper msg: I'm an adapter of an existed executor which have nothing to do with ~inspectable~wrappers~. + +BlaBlaBla... +I'm an adapter of an existed executor which have nothing to do with ~inspectable~wrappers~. +I'm working. + */