Skip to content

Commit

Permalink
docs: add IntegrationDemoUsingWrapperAdapterUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Apr 1, 2024
1 parent 53ff187 commit 8d05425
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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<String, String> 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: <https://foldright.io/inspectable-wrappers/apidocs/>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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.
*/

0 comments on commit 8d05425

Please sign in to comment.