Skip to content

Commit

Permalink
feat: add WrapperAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Mar 24, 2024
1 parent 5c60814 commit 9a23e5c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 27 deletions.
45 changes: 32 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ public class IntegrationDemo {
// 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));
System.out.println("Is executor ExistedExecutorWrapper? " +
Wrapper.isInstanceOf(executor, ExistedExecutorWrapper.class));
// print true
String adaptAttachment = Wrapper.getAttachment(executor, "adopted-existed-executor-wrapper-msg");
System.out.println("Adopted existed executor wrapper msg: " + adaptAttachment);
// print "very, very busy!"

////////////////////////////////////////
// call executor(wrapper chain)
Expand All @@ -220,37 +220,56 @@ public class IntegrationDemo {

final ExistedExecutorWrapper existed = new ExistedExecutorWrapper(base);
final IntegrateExistedExecutorWrapper integrate = new IntegrateExistedExecutorWrapper(existed);
integrate.setAttachment("adopted-existed-executor-wrapper-msg", "I'm existed executor, have nothing to do with ~inspectable~wrappers~.");

return new ChattyExecutorWrapper(integrate);
}

/**
* Integrate an existed executor wrapper(`ExistedExecutorWrapper`) without modification
*/
private static class IntegrateExistedExecutorWrapper implements Executor, Wrapper<Executor> {
private final ExistedExecutorWrapper existedExecutorWrapper;
private static class IntegrateExistedExecutorWrapper implements Executor, WrapperAdapter<Executor>, Attachable<String, String> {
private final ExistedExecutorWrapper adaptee;

public IntegrateExistedExecutorWrapper(ExistedExecutorWrapper existedExecutorWrapper) {
this.existedExecutorWrapper = existedExecutorWrapper;
public IntegrateExistedExecutorWrapper(ExistedExecutorWrapper adaptee) {
this.adaptee = adaptee;
}

@Override
public Executor unwrap() {
return existedExecutorWrapper.getExecutor();
return adaptee.getExecutor();
}

@Override
public Executor adaptee() {
return adaptee;
}

@Override
public void execute(Runnable command) {
existedExecutorWrapper.execute(command);
adaptee.execute(command);
}

private final Attachable<String, String> attachable = new AttachableDelegate<>();

@Override
public void setAttachment(String key, String value) {
attachable.setAttachment(key, value);
}

@Nullable
@Override
public String getAttachment(String key) {
return attachable.getAttachment(key);
}
}
}

/*
demo output:
Is executor chatty? true
Is executor IntegrateExistedExecutor? true
Is executor ExistedExecutorWrapper? true
Adopted existed executor wrapper msg: I'm existed executor, have nothing to do with ~inspectable~wrappers~.
BlaBlaBla...
I'm existed executor, have nothing to do with ~inspectable~wrappers~.
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/io/foldright/inspectablewrappers/Wrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ public interface Wrapper<T> {
static <W> boolean isInstanceOf(W wrapper, Class<?> clazz) {
requireNonNull(wrapper, "wrapper is null");
requireNonNull(clazz, "clazz is null");
return inspect(wrapper, w -> clazz.isAssignableFrom(w.getClass()));
return inspect(wrapper, w -> {
if (w instanceof WrapperAdapter) {
Object adaptee = ((WrapperAdapter<?>) w).adaptee();
if (clazz.isAssignableFrom(adaptee.getClass())) return true;
}
return clazz.isAssignableFrom(w.getClass());
});
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/io/foldright/inspectablewrappers/WrapperAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.foldright.inspectablewrappers;

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

import javax.annotation.ParametersAreNonnullByDefault;


/**
* Adaption an existed wrapper without modification.
* The existed wrapper can not be modified to integrated with {@code inspectable wrappers},
* aka. implements the {@link Wrapper} interface and/or {@link Attachable} interface.
*
* @param <T> the type of instances that be wrapped
*/
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
public interface WrapperAdapter<T> extends Wrapper<T> {
/**
* Returns the adapted/existed wrapper.
*
* @return the adapted wrapper.
*/
T adaptee();
}
49 changes: 36 additions & 13 deletions src/test/java/io/foldright/demo/integration/IntegrationDemo.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package io.foldright.demo.integration;

import edu.umd.cs.findbugs.annotations.Nullable;
import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
import io.foldright.demo.ChattyExecutorWrapper;
import io.foldright.inspectablewrappers.Attachable;
import io.foldright.inspectablewrappers.Wrapper;
import io.foldright.inspectablewrappers.WrapperAdapter;
import io.foldright.inspectablewrappers.utils.AttachableDelegate;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.concurrent.Executor;
Expand All @@ -18,12 +22,12 @@ public static void main(String[] args) {
// 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));
System.out.println("Is executor ExistedExecutorWrapper? " +
Wrapper.isInstanceOf(executor, ExistedExecutorWrapper.class));
// print true
String adaptAttachment = Wrapper.getAttachment(executor, "adopted-existed-executor-wrapper-msg");
System.out.println("Adopted existed executor wrapper msg: " + adaptAttachment);
// print "very, very busy!"

////////////////////////////////////////
// call executor(wrapper chain)
Expand All @@ -38,37 +42,56 @@ private static Executor buildExecutorChain() {

final ExistedExecutorWrapper existed = new ExistedExecutorWrapper(base);
final IntegrateExistedExecutorWrapper integrate = new IntegrateExistedExecutorWrapper(existed);
integrate.setAttachment("adopted-existed-executor-wrapper-msg", "I'm existed executor, have nothing to do with ~inspectable~wrappers~.");

return new ChattyExecutorWrapper(integrate);
}

/**
* Integrate an existed executor wrapper(`ExistedExecutorWrapper`) without modification
*/
private static class IntegrateExistedExecutorWrapper implements Executor, Wrapper<Executor> {
private final ExistedExecutorWrapper existedExecutorWrapper;
private static class IntegrateExistedExecutorWrapper implements Executor, WrapperAdapter<Executor>, Attachable<String, String> {
private final ExistedExecutorWrapper adaptee;

public IntegrateExistedExecutorWrapper(ExistedExecutorWrapper existedExecutorWrapper) {
this.existedExecutorWrapper = existedExecutorWrapper;
public IntegrateExistedExecutorWrapper(ExistedExecutorWrapper adaptee) {
this.adaptee = adaptee;
}

@Override
public Executor unwrap() {
return existedExecutorWrapper.getExecutor();
return adaptee.getExecutor();
}

@Override
public Executor adaptee() {
return adaptee;
}

@Override
public void execute(Runnable command) {
existedExecutorWrapper.execute(command);
adaptee.execute(command);
}

private final Attachable<String, String> attachable = new AttachableDelegate<>();

@Override
public void setAttachment(String key, String value) {
attachable.setAttachment(key, value);
}

@Nullable
@Override
public String getAttachment(String key) {
return attachable.getAttachment(key);
}
}
}

/*
demo output:
Is executor chatty? true
Is executor IntegrateExistedExecutor? true
Is executor ExistedExecutorWrapper? true
Adopted existed executor wrapper msg: I'm existed executor, have nothing to do with ~inspectable~wrappers~.
BlaBlaBla...
I'm existed executor, have nothing to do with ~inspectable~wrappers~.
Expand Down

0 comments on commit 9a23e5c

Please sign in to comment.