Skip to content

Commit 8eed883

Browse files
committed
docs: improve javadoc 📚 add missing QA annotations
1 parent 72e4ad1 commit 8eed883

File tree

6 files changed

+68
-47
lines changed

6 files changed

+68
-47
lines changed

README.md

+15-14
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ The purpose of **Inspectable Wrappers** is to provide a standard for wrapper cha
3939

4040
--------------------------------------------------------------------------------
4141

42-
## 🥑 Core Classes
43-
44-
- [`Wrapper`](src/main/java/io/foldright/inspectablewrappers/Wrapper.java) is core interface, used to
45-
- identify the wrapper instances as a wrapper chain
46-
- provide static entry methods to inspect the wrapper chain
47-
- [`Attachable`](src/main/java/io/foldright/inspectablewrappers/Attachable.java) interface is used to
48-
enhance the wrapper instances with the attachment storage ability
49-
- [`WrapperAdapter`](src/main/java/io/foldright/inspectablewrappers/WrapperAdapter.java) interface is used to
50-
adapt an existed wrapper without modifying it
42+
## 🥑 Core
43+
44+
- Core interfaces:
45+
- [`Wrapper`](src/main/java/io/foldright/inspectablewrappers/Wrapper.java) interface is core interface, used to
46+
be implemented by wrapper classes, make an **inspectable wrapper chain**(linked list).
47+
- [`Attachable`](src/main/java/io/foldright/inspectablewrappers/Attachable.java) interface is used to
48+
enhance the wrapper instances with the attachment storage ability
49+
- [`WrapperAdapter`](src/main/java/io/foldright/inspectablewrappers/WrapperAdapter.java) interface is used to
50+
adapt an existed wrapper instance to type `Wrapper` without modifying it
51+
- [`Inspector`](src/main/java/io/foldright/inspectablewrappers/Inspector.java) class is used to
52+
inspect the **wrapper chain**
5153

5254
## 🌰 Usage Demo
5355

@@ -123,10 +125,10 @@ public class Demo {
123125
////////////////////////////////////////
124126

125127
System.out.println("Is executor lazy? " +
126-
Wrapper.isInstanceOf(executor, LazyExecutorWrapper.class));
128+
Inspector.isInstanceOf(executor, LazyExecutorWrapper.class));
127129
// print true
128130

129-
String busy = Wrapper.getAttachment(executor, "busy");
131+
String busy = Inspector.getAttachment(executor, "busy");
130132
System.out.println("Is executor busy? " + busy);
131133
// print "very, very busy!"
132134

@@ -203,9 +205,9 @@ public class IntegrationDemo {
203205
////////////////////////////////////////
204206

205207
System.out.println("Is executor ExistedExecutorWrapper? " +
206-
Wrapper.isInstanceOf(executor, ExistedExecutorWrapper.class));
208+
Inspector.isInstanceOf(executor, ExistedExecutorWrapper.class));
207209
// print true
208-
String adaptAttachment = Wrapper.getAttachment(executor, "adapted-existed-executor-wrapper-msg");
210+
String adaptAttachment = Inspector.getAttachment(executor, "adapted-existed-executor-wrapper-msg");
209211
System.out.println("Adapted existed executor wrapper msg: " + adaptAttachment);
210212
// print "I'm an adapter of an existed executor which have nothing to do with ~inspectable~wrappers~."
211213

@@ -259,7 +261,6 @@ public class IntegrationDemo {
259261
attachable.setAttachment(key, value);
260262
}
261263

262-
@Nullable
263264
@Override
264265
public String getAttachment(String key) {
265266
return attachable.getAttachment(key);

src/main/java/io/foldright/inspectablewrappers/Inspector.java

+21-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import edu.umd.cs.findbugs.annotations.NonNull;
44
import edu.umd.cs.findbugs.annotations.Nullable;
55

6+
import javax.annotation.ParametersAreNonnullByDefault;
67
import java.util.Optional;
78
import java.util.function.Function;
89
import java.util.function.Predicate;
@@ -12,25 +13,24 @@
1213

1314

1415
/**
15-
* This {@code Inspector} is used to inspect wrapper implementors:
16+
* This {@code Inspector} class is used to inspect the wrapper chain.
17+
* <p>
18+
* Common usages:
1619
* <ul>
17-
* <li>
18-
* Reports whether any wrapper on the wrapper chain matches the given type by static method {@link #isInstanceOf(Object, Class)}
19-
* </li>
20-
* <li>
21-
* Retrieve the attachment from wrapper chain(wrapper instances implement interface {@link Wrapper})
22-
* by static method {@link #getAttachment(Object, Object)}.
23-
* </li>
20+
* <li>Reports whether any wrapper on the wrapper chain matches the given type
21+
* by static method {@link #isInstanceOf(Object, Class)}
22+
* <li>Retrieve the attachment from wrapper chain(wrapper instances implement interface {@link Wrapper})
23+
* by static method {@link #getAttachment(Object, Object)}
2424
* </ul>
2525
*
26+
* @author Jerry Lee (oldratlee at gmail dot com)
27+
* @author Zava (zava dot kid at gmail dot com)
2628
* @see Wrapper
29+
* @see Attachable
30+
* @see WrapperAdapter
2731
*/
32+
@ParametersAreNonnullByDefault
2833
public final class Inspector {
29-
30-
// no need to create instance at all
31-
private Inspector() {
32-
}
33-
3434
/**
3535
* Reports whether any wrapper on the wrapper chain matches the given type.
3636
* <p>
@@ -108,7 +108,6 @@ public static <W, K, V> V getAttachment(final W wrapper, final K key) {
108108
}).orElse(null);
109109
}
110110

111-
112111
/**
113112
* Traverses the wrapper chain, and apply the given {@code process} function to each wrapper,
114113
* and returns the first non-empty({@link Optional#empty()}) result of the process function,
@@ -124,8 +123,7 @@ public static <W, K, V> V getAttachment(final W wrapper, final K key) {
124123
* @return the first non-empty({@link Optional#empty()}) result of the process function,
125124
* otherwise returns {@link Optional#empty()}.
126125
* @throws NullPointerException if any arguments is null or any wrapper {{@link Wrapper#unwrap()}} returns null
127-
* @throws IllegalStateException if the adaptee of {@link WrapperAdapter} is a wrapper instance,
128-
* the use of WrapperAdapter is unnecessary!
126+
* @throws IllegalStateException if the adaptee of {@link WrapperAdapter} is type {@link Wrapper}
129127
*/
130128
@NonNull
131129
@SuppressWarnings("unchecked")
@@ -143,7 +141,9 @@ static <W, T> Optional<T> travel(final W wrapper, final Function<W, Optional<T>>
143141
if (w instanceof WrapperAdapter) {
144142
final Object adaptee = ((WrapperAdapter<?>) w).adaptee();
145143
if (adaptee instanceof Wrapper) {
146-
throw new IllegalStateException("adaptee(" + adaptee.getClass().getName() + ") of WrapperAdapter(" + w.getClass().getName() + ") is a wrapper instance, the use of WrapperAdapter is unnecessary!");
144+
throw new IllegalStateException("adaptee(" + adaptee.getClass().getName() +
145+
") of WrapperAdapter(" + w.getClass().getName() +
146+
") is type Wrapper, adapting a Wrapper to a Wrapper is unnecessary!");
147147
}
148148

149149
Optional<T> r = process.apply((W) adaptee);
@@ -154,4 +154,8 @@ static <W, T> Optional<T> travel(final W wrapper, final Function<W, Optional<T>>
154154
w = unwrapNonNull(w);
155155
}
156156
}
157+
158+
// no need to create instance at all
159+
private Inspector() {
160+
}
157161
}

src/main/java/io/foldright/inspectablewrappers/Wrapper.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
import edu.umd.cs.findbugs.annotations.NonNull;
44

5-
import javax.annotation.ParametersAreNonnullByDefault;
6-
75

86
/**
97
* This {@code Wrapper} interface is used to be implemented by wrapper classes,
10-
* make {@code wrapper instances} as an <b>inspectable wrapper chain</b>(linked list).
8+
* make an <b>inspectable wrapper chain</b>(linked list).
9+
* <p>
10+
* <b><i>Note about wrapper chain:<br></i></b>
11+
* <ul>
12+
* <li>The wrapper chain consists of wrapper itself, followed by the wrappers
13+
* obtained by repeatedly calling {@link Wrapper#unwrap()}
14+
* <li>The last instance of wrapper chain is never type {@link Wrapper}
15+
* <li>Uses the static methods in {@link Inspector} to inspect the wrapper chain
16+
* </ul>
1117
*
1218
* @param <T> the type of instances that be wrapped
1319
* @author Jerry Lee (oldratlee at gmail dot com)
20+
* @author Zava (zava dot kid at gmail dot com)
1421
* @see Attachable
1522
* @see WrapperAdapter
1623
* @see Inspector
1724
*/
18-
@ParametersAreNonnullByDefault
1925
public interface Wrapper<T> {
2026
/**
2127
* Returns the underlying instance that be wrapped.
@@ -24,6 +30,4 @@ public interface Wrapper<T> {
2430
*/
2531
@NonNull
2632
T unwrap();
27-
28-
2933
}

src/main/java/io/foldright/inspectablewrappers/WrapperAdapter.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,31 @@
44

55

66
/**
7-
* Adaption an existed wrapper without modifying it.
7+
* This {@code WrapperAdapter} interface is used to
8+
* adapt an existed wrapper instance to type {@link Wrapper} without modifying it.
89
* <p>
910
* The existed wrapper may not be able to be modified to integrate with {@code inspectable wrappers},
1011
* aka. implements the {@link Wrapper} interface and/or {@link Attachable} interface.
1112
*
1213
* @param <T> the type of instances that be wrapped
14+
* @author Jerry Lee (oldratlee at gmail dot com)
15+
* @author Zava (zava dot kid at gmail dot com)
1316
* @see Wrapper
1417
*/
1518
public interface WrapperAdapter<T> extends Wrapper<T> {
1619
/**
1720
* Returns the adapted/existed wrapper.
21+
* <p>
22+
* <b><i>Note:<br></i></b>
23+
* <ul>
24+
* <li>The adaptee MUST not a {@link Wrapper},
25+
* since adapting a {@link Wrapper} to a {@link Wrapper} is unnecessary.
26+
* <li>If adapting a {@link Wrapper}, the inspection operations of {@link Inspector} will
27+
* throw {@link IllegalStateException} when touch the {@link Wrapper} type adaptee.
28+
* </ul>
1829
*
1930
* @return the adapted wrapper.
20-
* @see Inspector#isInstanceOf(Object, Class)
31+
* @see Inspector
2132
*/
2233
@NonNull
2334
T adaptee();

src/main/java/io/foldright/inspectablewrappers/package-info.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
*
44
* <ul>
55
* <li>{@link io.foldright.inspectablewrappers.Wrapper} is core interface, used to
6-
* <ul>
7-
* <li>identify the wrapper instances as a wrapper chain
8-
* <li>provide static entry methods to inspect the wrapper chain
9-
* </ul>
6+
* be implemented by wrapper classes, make an <b>inspectable wrapper chain</b>(linked list).
107
* <li>{@link io.foldright.inspectablewrappers.Attachable} interface is used to
118
* enhance the wrapper instances with the attachment storage ability
129
* <li>{@link io.foldright.inspectablewrappers.WrapperAdapter} interface is used to
13-
* adapt an existed wrapper without modifying it
10+
* adapt an existed wrapper instance to type {@link io.foldright.inspectablewrappers.Wrapper} without modifying it.
1411
* </ul>
12+
* <p>
13+
* The {@link io.foldright.inspectablewrappers.Inspector} class is used to inspect the wrapper chain.
1514
*
15+
* @author Jerry Lee (oldratlee at gmail dot com)
1616
* @see io.foldright.inspectablewrappers.Wrapper
1717
* @see io.foldright.inspectablewrappers.Attachable
1818
* @see io.foldright.inspectablewrappers.WrapperAdapter
19+
* @see io.foldright.inspectablewrappers.Inspector
1920
*/
2021
package io.foldright.inspectablewrappers;

src/test/java/io/foldright/inspectablewrappers/WrapperAdapterTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class WrapperAdapterTest : FunSpec({
6060

6161
val errMsg = "adaptee(io.foldright.inspectablewrappers.ChattyExecutorWrapper)" +
6262
" of WrapperAdapter(io.foldright.inspectablewrappers.ChattyExecutorWrapperAdapter)" +
63-
" is a wrapper instance, the use of WrapperAdapter is unnecessary!"
63+
" is type Wrapper, adapting a Wrapper to a Wrapper is unnecessary!"
6464

6565
shouldThrow<IllegalStateException> {
6666
Inspector.isInstanceOf(chain, ExecutorService::class.java)
@@ -77,7 +77,7 @@ class WrapperAdapterTest : FunSpec({
7777
})
7878

7979
/**
80-
* Adaption an existed wrapper(`ExistedExecutorWrapper`) without modifying it.
80+
* Adaption an existed wrapper([ExistedExecutorWrapper]) without modifying it.
8181
*/
8282
private class ExistedExecutorWrapperAdapter(private val adaptee: ExistedExecutorWrapper) :
8383
Executor by adaptee, WrapperAdapter<Executor>, Attachable<String, String> by AttachableDelegate() {
@@ -93,7 +93,7 @@ class ExistedExecutorWrapper(val executor: Executor) : Executor {
9393
}
9494

9595
/**
96-
* Wrong use the [WrapperAdapter], the adaptee is [Wrapper].
96+
* Wrong use the [WrapperAdapter], the adaptee is already [Wrapper]!
9797
*/
9898
private class ChattyExecutorWrapperAdapter(private val adaptee: ChattyExecutorWrapper) :
9999
Executor by adaptee, WrapperAdapter<Executor>, Attachable<String, String> by AttachableDelegate() {

0 commit comments

Comments
 (0)