-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs/demo: add demo to integrate an existed executor wrapper without …
…modification 📚
- Loading branch information
Showing
3 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/test/java/io/foldright/demo/integration/ExistedExecutorWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
76
src/test/java/io/foldright/demo/integration/IntegrationDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ |