Class Inspector
Inspector
class is used to inspect the wrapper chain.
Common simple usages
- Reports whether any instance on the wrapper chain matches the given type
by static method
containsInstanceTypeOnWrapperChain(Object, Class)
- Retrieves the attachment of instance on the wrapper chain
by static method
getAttachmentFromWrapperChain(Object, Object)
- Gets the wrapper chain, aka. the list of all instances on the wrapper chain
by static method
getInstancesOfWrapperChain(Object)
- Gets the base of the wrapper chain, aka. the last instance of the wrapper chain
by static method
getBaseOfWrapperChain(Object)
- Verifies the compliance of wrapper chain with the specification contracts
by static method
verifyWrapperChainContracts(Object)
orverifyWrapperChainContracts(Object, Class)
Convenience methods for Wrapper
interface
- Unwraps
Wrapper
to the underlying instance by static methodunwrap(Object)
- Checks the input object is an instance of
Wrapper
or not by static methodisWrapper(Object)
Advanced usages
- Reports whether any instance on the wrapper chain satisfy the given
Predicate
by static methodtestWrapperChain(Object, Predicate)
- Performs the given
action
for each instance on the wrapper chain by static methodforEachOnWrapperChain(Object, Consumer)
- Traverses the wrapper chain and applies the given
Function
to each instance on the wrapper chain by static methodtravelWrapperChain(Object, Function)
You can implement your own inspection logic using above advanced methods.
Note about usage and methods naming
All method names contain the word "wrapper chain",
so the usage code is easily recognizable as related to inspectable wrappers
.
Because the method names are long and informative,
it's recommended to static import
these methods.
- Author:
- Jerry Lee (oldratlee at gmail dot com), Zava Xu (zava dot kid at gmail dot com)
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <W> boolean
containsInstanceTypeOnWrapperChain
(W wrapper, Class<?> instanceType) Reports whether any instance on the wrapper chain matches the given type.static <W> void
forEachOnWrapperChain
(W wrapper, Consumer<? super W> action) Performs the givenaction
for each instance on the wrapper chain until all elements have been processed or the action throws an exception.static <W,
K, V> V getAttachmentFromWrapperChain
(W wrapper, K key) Retrieves the attachment of instance on the wrapper chain for the given key by callingAttachable.getAttachment_(Object)
.static <W> W
getBaseOfWrapperChain
(W wrapper) Gets the base of the wrapper chain, aka.static <W> List<W>
getInstancesOfWrapperChain
(W wrapper) Gets the wrapper chain, aka.static boolean
static <W> boolean
testWrapperChain
(W wrapper, Predicate<? super W> predicate) Reports whether any instance on the wrapper chain satisfies the givenpredicate
.static <W,
T> Optional<T> travelWrapperChain
(W wrapper, Function<? super W, Optional<T>> process) Traverses the wrapper chain and applies the givenprocess
function to each instance on the wrapper chain, returns the first non-empty(Optional.empty()
) result of the process function, otherwise returnsOptional.empty()
.static <W> W
unwrap
(W obj) static <W> void
verifyWrapperChainContracts
(W wrapper) Verifies the compliance of wrapper chain with the specification contracts.static <W> void
verifyWrapperChainContracts
(W wrapper, Class<W> bizInterface) Verifies the compliance of wrapper chain with the specification contracts, and checks all instances on wrapper chain is an instance of the givenbizInterface
.
-
Method Details
-
containsInstanceTypeOnWrapperChain
@Contract(pure=true) public static <W> boolean containsInstanceTypeOnWrapperChain(W wrapper, Class<?> instanceType) Reports whether any instance on the wrapper chain matches the given type.The wrapper chain consists of wrapper itself, followed by the wrappers obtained by repeatedly calling
Wrapper.unwrap_()
.- Type Parameters:
W
- the type of instances that be wrapped- Parameters:
wrapper
- wrapper instance/wrapper chaininstanceType
- target type- Returns:
- return
false
if no wrapper on the wrapper chain matches the given type, otherwise returntrue
- Throws:
NullPointerException
- if any arguments is null, or any wrapperWrapper.unwrap_()
returns null, or the adaptee ofWrapperAdapter
is nullIllegalStateException
- if the adaptee ofWrapperAdapter
is an instance ofWrapper
or CYCLIC wrapper chain
-
getAttachmentFromWrapperChain
@Nullable @Contract(pure=true) public static <W,K, V getAttachmentFromWrapperChainV> (W wrapper, K key) Retrieves the attachment of instance on the wrapper chain for the given key by callingAttachable.getAttachment_(Object)
.The wrapper chain consists of wrapper itself, followed by the wrappers obtained by repeatedly calling
Wrapper.unwrap_()
.If the same key exists in multiple wrappers, outer wrapper win.
- Type Parameters:
W
- the type of instances that be wrappedK
- the type of attachment keyV
- the type of attachment value- Parameters:
wrapper
- wrapper instancekey
- the attachment key- Returns:
- the attachment value of wrapper for given key on the wrapper chain, or null if the attachment is absent
- Throws:
NullPointerException
- if any arguments is null, or any wrapperWrapper.unwrap_()
returns null, or the adaptee ofWrapperAdapter
is nullClassCastException
- if the return value is not type<V>
IllegalStateException
- if the adaptee ofWrapperAdapter
is an instance ofWrapper
or CYCLIC wrapper chain- See Also:
-
getInstancesOfWrapperChain
Gets the wrapper chain, aka. the list of all instances on the wrapper chain.The wrapper chain consists of wrapper itself, followed by the wrappers obtained by repeatedly calling
Wrapper.unwrap_()
.- Type Parameters:
W
- the type of instances that be wrapped- Parameters:
wrapper
- wrapper instance- Throws:
NullPointerException
- if wrapped argument is null, or any wrapperWrapper.unwrap_()
returns null, or the adaptee ofWrapperAdapter
is nullIllegalStateException
- if the adaptee ofWrapperAdapter
is an instance ofWrapper
or CYCLIC wrapper chain
-
getBaseOfWrapperChain
Gets the base of the wrapper chain, aka. the last instance of the wrapper chain.The wrapper chain consists of wrapper itself, followed by the wrappers obtained by repeatedly calling
Wrapper.unwrap_()
.- Type Parameters:
W
- the type of instances that be wrapped- Parameters:
wrapper
- wrapper instance- Throws:
NullPointerException
- if wrapped argument is null, or any wrapperWrapper.unwrap_()
returns null, or the adaptee ofWrapperAdapter
is nullIllegalStateException
- if the adaptee ofWrapperAdapter
is an instance ofWrapper
or CYCLIC wrapper chain
-
unwrap
@Nullable @Contract(value="null -> null; !null -> !null", pure=true) public static <W> W unwrap(@Nullable W obj) UnwrapsWrapper
to the underlying instance if input is aWrapper
instance.This method is
null
-safe, returnnull
iff input parameter isnull
; If input parameter is not aWrapper
just return input.A convenience method for
Wrapper.unwrap_()
- Type Parameters:
W
- the type of instances that be wrapped- Parameters:
obj
- wrapper instance- Throws:
NullPointerException
- ifWrapper.unwrap_()
returns null- See Also:
-
isWrapper
Checks the input object is an instance ofWrapper
or not, returnfalse
if inputnull
.A convenience method for
Wrapper
interface.- See Also:
-
verifyWrapperChainContracts
public static <W> void verifyWrapperChainContracts(W wrapper) Verifies the compliance of wrapper chain with the specification contracts.The wrapper chain consists of wrapper itself, followed by the wrappers obtained by repeatedly calling
Wrapper.unwrap_()
.more about the specification contracts see the doc of below methods:
- Type Parameters:
W
- the type of instances that be wrapped- Parameters:
wrapper
- wrapper instance- Throws:
NullPointerException
- if wrapped argument is null, or any wrapperWrapper.unwrap_()
returns null, or the adaptee ofWrapperAdapter
is nullIllegalStateException
- if the adaptee ofWrapperAdapter
is an instance ofWrapper
or CYCLIC wrapper chain
-
verifyWrapperChainContracts
Verifies the compliance of wrapper chain with the specification contracts, and checks all instances on wrapper chain is an instance of the givenbizInterface
.The wrapper chain consists of wrapper itself, followed by the wrappers obtained by repeatedly calling
Wrapper.unwrap_()
.more about the specification contracts see the doc of below methods:
- Type Parameters:
W
- the type of instances that be wrapped- Parameters:
wrapper
- wrapper instance- Throws:
NullPointerException
- if any arguments is null, or any wrapperWrapper.unwrap_()
returns null, or the adaptee ofWrapperAdapter
is nullIllegalStateException
- if any instance on the wrapper chain is not an instance ofbizInterface
, or the adaptee ofWrapperAdapter
is an instance ofWrapper
or CYCLIC wrapper chain
-
testWrapperChain
Reports whether any instance on the wrapper chain satisfies the givenpredicate
. Exceptions thrown by thepredicate
are relayed to the caller.The wrapper chain consists of wrapper itself, followed by the wrappers obtained by repeatedly calling
Wrapper.unwrap_()
.- Type Parameters:
W
- the type of instances that be wrapped- Parameters:
wrapper
- wrapper instance/wrapper chainpredicate
- inspect logic- Returns:
- return
false
if no wrapper on the wrapper chain satisfy the givenpredicate
, otherwise returntrue
- Throws:
NullPointerException
- if any arguments is null, or any wrapperWrapper.unwrap_()
returns null, or the adaptee ofWrapperAdapter
is nullIllegalStateException
- if the adaptee ofWrapperAdapter
is an instance ofWrapper
or CYCLIC wrapper chain
-
forEachOnWrapperChain
Performs the givenaction
for each instance on the wrapper chain until all elements have been processed or the action throws an exception. Exceptions thrown by theaction
are relayed to the caller.The wrapper chain consists of wrapper itself, followed by the wrappers obtained by repeatedly calling
Wrapper.unwrap_()
.- Type Parameters:
W
- the type of instances that be wrapped- Parameters:
wrapper
- wrapper instance/wrapper chainaction
- The action to be performed for each instance- Throws:
NullPointerException
- if any arguments is null, or any wrapperWrapper.unwrap_()
returns null, or the adaptee ofWrapperAdapter
is nullIllegalStateException
- if the adaptee ofWrapperAdapter
is an instance ofWrapper
or CYCLIC wrapper chain- See Also:
-
travelWrapperChain
@NonNull public static <W,T> Optional<T> travelWrapperChain(W wrapper, Function<? super W, Optional<T>> process) Traverses the wrapper chain and applies the givenprocess
function to each instance on the wrapper chain, returns the first non-empty(Optional.empty()
) result of the process function, otherwise returnsOptional.empty()
. Exceptions thrown by the process function are relayed to the caller.The wrapper chain consists of wrapper itself, followed by the wrappers obtained by repeatedly calling
Wrapper.unwrap_()
.- Type Parameters:
W
- the type of instances that be wrappedT
- the return data type of process function- Parameters:
wrapper
- wrapper instanceprocess
- process function- Returns:
- the first non-empty(
Optional.empty()
) result of the process function, otherwise returnsOptional.empty()
- Throws:
NullPointerException
- if any arguments is null, or any wrapperWrapper.unwrap_()
returns null, or the adaptee ofWrapperAdapter
is nullIllegalStateException
- if the adaptee ofWrapperAdapter
is an instance ofWrapper
or CYCLIC wrapper chain- See Also:
-