Skip to content

Commit

Permalink
docs/demo: add demo to integrate an existed executor wrapper without …
Browse files Browse the repository at this point in the history
…modification 📚
  • Loading branch information
oldratlee committed Mar 23, 2024
1 parent 86237c3 commit ade53d9
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 2 deletions.
104 changes: 102 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ The purpose of **Inspectable Wrappers** is to provide a standard for wrapper cha
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [🥑 Core Classes](#-core-classes)
- [🌰 Demo](#-demo)
- [🌰 Usage Demo](#-usage-demo)
- [wrapper implementations in your application code](#wrapper-implementations-in-your-application-code)
- [inspection of the wrapper chain](#inspection-of-the-wrapper-chain)
- [🌰 Integration Demo](#-integration-demo)
- [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)

Expand All @@ -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.

Expand Down Expand Up @@ -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(Runnable command) {
System.out.println("I'm existed executor, have nothing to do with ~inspectable~wrappers~.");
executor.execute(command);
}
}
```

### the integration code

```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<Executor> {
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: <https://foldright.io/inspectable-wrappers/apidocs/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.foldright.demo.integration;

import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.concurrent.Executor;


@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
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(Runnable command) {
System.out.println("I'm existed executor, have nothing to do with ~inspectable~wrappers~.");
executor.execute(command);
}
}
76 changes: 76 additions & 0 deletions src/test/java/io/foldright/demo/integration/IntegrationDemo.java
Original file line number Diff line number Diff line change
@@ -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<Executor> {
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.
*/

0 comments on commit ade53d9

Please sign in to comment.