3
3
import edu .umd .cs .findbugs .annotations .NonNull ;
4
4
import edu .umd .cs .findbugs .annotations .Nullable ;
5
5
6
+ import javax .annotation .ParametersAreNonnullByDefault ;
6
7
import java .util .Optional ;
7
8
import java .util .function .Function ;
8
9
import java .util .function .Predicate ;
12
13
13
14
14
15
/**
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:
16
19
* <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)}
24
24
* </ul>
25
25
*
26
+ * @author Jerry Lee (oldratlee at gmail dot com)
27
+ * @author Zava (zava dot kid at gmail dot com)
26
28
* @see Wrapper
29
+ * @see Attachable
30
+ * @see WrapperAdapter
27
31
*/
32
+ @ ParametersAreNonnullByDefault
28
33
public final class Inspector {
29
-
30
- // no need to create instance at all
31
- private Inspector () {
32
- }
33
-
34
34
/**
35
35
* Reports whether any wrapper on the wrapper chain matches the given type.
36
36
* <p>
@@ -108,7 +108,6 @@ public static <W, K, V> V getAttachment(final W wrapper, final K key) {
108
108
}).orElse (null );
109
109
}
110
110
111
-
112
111
/**
113
112
* Traverses the wrapper chain, and apply the given {@code process} function to each wrapper,
114
113
* 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) {
124
123
* @return the first non-empty({@link Optional#empty()}) result of the process function,
125
124
* otherwise returns {@link Optional#empty()}.
126
125
* @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}
129
127
*/
130
128
@ NonNull
131
129
@ SuppressWarnings ("unchecked" )
@@ -143,7 +141,9 @@ static <W, T> Optional<T> travel(final W wrapper, final Function<W, Optional<T>>
143
141
if (w instanceof WrapperAdapter ) {
144
142
final Object adaptee = ((WrapperAdapter <?>) w ).adaptee ();
145
143
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!" );
147
147
}
148
148
149
149
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>>
154
154
w = unwrapNonNull (w );
155
155
}
156
156
}
157
+
158
+ // no need to create instance at all
159
+ private Inspector () {
160
+ }
157
161
}
0 commit comments