diff --git a/README.md b/README.md index de3a421..4d0116e 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,6 @@ The purpose of **Inspectable Wrappers** is to provide a standard for wrapper cha ## 🌰 Demo Below use the `Executor Wrapper` to demonstrate the usage. -(Runnable demo codes in project: [`Demo.java`](src/test/java/io/foldright/demo/Demo.java)) ### wrapper implementations in your application code @@ -82,7 +81,7 @@ public class LazyExecutorWrapper implements Executor, Wrapper, Attacha @Override public void execute(Runnable command) { - System.out.println("I'm lazy, sleep before work"); + System.out.println("I'm lazy, sleep before work."); sleep(); executor.execute(command); @@ -112,39 +111,55 @@ public class LazyExecutorWrapper implements Executor, Wrapper, Attacha ```java public class Demo { public static void main(String[] args) { - //////////////////////////////////////// - // prepare executor instance and wrappers - //////////////////////////////////////// - - final Executor executor = Runnable::run; - - final LazyExecutorWrapper lazy = new LazyExecutorWrapper(executor); - lazy.setAttachment("busy", "very, very busy!"); - - final Executor chatty = new ChattyExecutorWrapper(lazy); + final Executor executor = buildExecutorChain(); //////////////////////////////////////// - // inspect the wrapper chain + // inspect the executor(wrapper chain) //////////////////////////////////////// - System.out.println("Is chatty executor LazyExecutor? " + - Wrapper.isInstanceOf(chatty, LazyExecutorWrapper.class)); + System.out.println("Is executor lazy? " + + Wrapper.isInstanceOf(executor, LazyExecutorWrapper.class)); // print true - String busy = Wrapper.getAttachment(chatty, "busy"); - System.out.println("Is chatty executor busy? " + busy); + String busy = Wrapper.getAttachment(executor, "busy"); + System.out.println("Is executor busy? " + busy); // print "very, very busy!" //////////////////////////////////////// - // call executor + // call executor(wrapper chain) //////////////////////////////////////// System.out.println(); - chatty.execute(() -> System.out.println("work!")); + executor.execute(() -> System.out.println("I'm working.")); + } + + /** + * prepare executor instances/wrappers, build the executor/wrapper chain + **/ + private static Executor buildExecutorChain() { + final Executor base = Runnable::run; + + final LazyExecutorWrapper lazy = new LazyExecutorWrapper(base); + lazy.setAttachment("busy", "very, very busy!"); + + return new ChattyExecutorWrapper(lazy); } } + +/* +demo output: + +Is executor lazy? true +Is executor busy? very, very busy! + +BlaBlaBla... +I'm lazy, sleep before work. +I'm working. + */ ``` +> Runnable demo codes in project: [`Demo.java`](src/test/java/io/foldright/demo/Demo.java) + ## 🍼 Java API Docs The current version Java API documentation: diff --git a/src/test/java/io/foldright/demo/Demo.java b/src/test/java/io/foldright/demo/Demo.java index 8e23321..de74061 100644 --- a/src/test/java/io/foldright/demo/Demo.java +++ b/src/test/java/io/foldright/demo/Demo.java @@ -7,34 +7,48 @@ public class Demo { public static void main(String[] args) { - //////////////////////////////////////// - // prepare executor instance and wrappers - //////////////////////////////////////// - - final Executor executor = Runnable::run; - - final LazyExecutorWrapper lazy = new LazyExecutorWrapper(executor); - lazy.setAttachment("busy", "very, very busy!"); - - final Executor chatty = new ChattyExecutorWrapper(lazy); + final Executor executor = buildExecutorChain(); //////////////////////////////////////// - // inspect the wrapper chain + // inspect the executor(wrapper chain) //////////////////////////////////////// - System.out.println("Is chatty executor LazyExecutor? " + - Wrapper.isInstanceOf(chatty, LazyExecutorWrapper.class)); + System.out.println("Is executor lazy? " + + Wrapper.isInstanceOf(executor, LazyExecutorWrapper.class)); // print true - String busy = Wrapper.getAttachment(chatty, "busy"); - System.out.println("Is chatty executor busy? " + busy); + String busy = Wrapper.getAttachment(executor, "busy"); + System.out.println("Is executor busy? " + busy); // print "very, very busy!" //////////////////////////////////////// - // call executor + // call executor(wrapper chain) //////////////////////////////////////// System.out.println(); - chatty.execute(() -> System.out.println("work!")); + executor.execute(() -> System.out.println("I'm working.")); + } + + /** + * prepare executor instances/wrappers, build the executor/wrapper chain + **/ + private static Executor buildExecutorChain() { + final Executor base = Runnable::run; + + final LazyExecutorWrapper lazy = new LazyExecutorWrapper(base); + lazy.setAttachment("busy", "very, very busy!"); + + return new ChattyExecutorWrapper(lazy); } } + +/* +demo output: + +Is executor lazy? true +Is executor busy? very, very busy! + +BlaBlaBla... +I'm lazy, sleep before work. +I'm working. + */ diff --git a/src/test/java/io/foldright/demo/LazyExecutorWrapper.java b/src/test/java/io/foldright/demo/LazyExecutorWrapper.java index 8ae589b..a31bb14 100644 --- a/src/test/java/io/foldright/demo/LazyExecutorWrapper.java +++ b/src/test/java/io/foldright/demo/LazyExecutorWrapper.java @@ -21,7 +21,7 @@ public LazyExecutorWrapper(Executor executor) { @Override public void execute(Runnable command) { - System.out.println("I'm lazy, sleep before work"); + System.out.println("I'm lazy, sleep before work."); sleep(); executor.execute(command);