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
Wrapperto the underlying instance by static methodunwrap(Object) - Checks the input object is an instance of
Wrapperor not by static methodisWrapper(Object)
Advanced usages
- Reports whether any instance on the wrapper chain satisfy the given
Predicateby static methodtestWrapperChain(Object, Predicate) - Performs the given
actionfor each instance on the wrapper chain by static methodforEachOnWrapperChain(Object, Consumer) - Traverses the wrapper chain and applies the given
Functionto 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> booleancontainsInstanceTypeOnWrapperChain(W wrapper, Class<?> instanceType) Reports whether any instance on the wrapper chain matches the given type.static <W> voidforEachOnWrapperChain(W wrapper, Consumer<? super W> action) Performs the givenactionfor 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> WgetBaseOfWrapperChain(W wrapper) Gets the base of the wrapper chain, aka.static <W> List<W>getInstancesOfWrapperChain(W wrapper) Gets the wrapper chain, aka.static booleanstatic <W> booleantestWrapperChain(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 givenprocessfunction to each instance on the wrapper chain, returns the first non-empty(Optional.empty()) result of the process function, otherwise returnsOptional.empty().static <W> Wunwrap(W obj) static <W> voidverifyWrapperChainContracts(W wrapper) Verifies the compliance of wrapper chain with the specification contracts.static <W> voidverifyWrapperChainContracts(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
falseif 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 ofWrapperAdapteris nullIllegalStateException- if the adaptee ofWrapperAdapteris an instance ofWrapperor 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 ofWrapperAdapteris nullClassCastException- if the return value is not type<V>IllegalStateException- if the adaptee ofWrapperAdapteris an instance ofWrapperor 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 ofWrapperAdapteris nullIllegalStateException- if the adaptee ofWrapperAdapteris an instance ofWrapperor 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 ofWrapperAdapteris nullIllegalStateException- if the adaptee ofWrapperAdapteris an instance ofWrapperor CYCLIC wrapper chain
-
unwrap
@Nullable @Contract(value="null -> null; !null -> !null", pure=true) public static <W> W unwrap(@Nullable W obj) UnwrapsWrapperto the underlying instance if input is aWrapperinstance.This method is
null-safe, returnnulliff input parameter isnull; If input parameter is not aWrapperjust 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 ofWrapperor not, returnfalseif inputnull.A convenience method for
Wrapperinterface.- 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 ofWrapperAdapteris nullIllegalStateException- if the adaptee ofWrapperAdapteris an instance ofWrapperor 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 ofWrapperAdapteris nullIllegalStateException- if any instance on the wrapper chain is not an instance ofbizInterface, or the adaptee ofWrapperAdapteris an instance ofWrapperor CYCLIC wrapper chain
-
testWrapperChain
Reports whether any instance on the wrapper chain satisfies the givenpredicate. Exceptions thrown by thepredicateare 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
falseif 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 ofWrapperAdapteris nullIllegalStateException- if the adaptee ofWrapperAdapteris an instance ofWrapperor CYCLIC wrapper chain
-
forEachOnWrapperChain
Performs the givenactionfor each instance on the wrapper chain until all elements have been processed or the action throws an exception. Exceptions thrown by theactionare 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 ofWrapperAdapteris nullIllegalStateException- if the adaptee ofWrapperAdapteris an instance ofWrapperor 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 givenprocessfunction 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 ofWrapperAdapteris nullIllegalStateException- if the adaptee ofWrapperAdapteris an instance ofWrapperor CYCLIC wrapper chain- See Also:
-