Skip to content

Commit

Permalink
test/docs: improve demo codes; extract buildExecutorChain method 🧪
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Mar 23, 2024
1 parent e3b09f9 commit 86237c3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 37 deletions.
53 changes: 34 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -82,7 +81,7 @@ public class LazyExecutorWrapper implements Executor, Wrapper<Executor>, 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);
Expand Down Expand Up @@ -112,39 +111,55 @@ public class LazyExecutorWrapper implements Executor, Wrapper<Executor>, 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: <https://foldright.io/inspectable-wrappers/apidocs/>
Expand Down
48 changes: 31 additions & 17 deletions src/test/java/io/foldright/demo/Demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
2 changes: 1 addition & 1 deletion src/test/java/io/foldright/demo/LazyExecutorWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 86237c3

Please sign in to comment.