Class StackOverlay
- All Implemented Interfaces:
BeanStore
BeanStore overlay.
A StackOverlay delegates to a LIFO stack of BeanStore frames. Each lookup walks the stack
from top to bottom and returns the first match. When the stack is empty the lookup returns
Optional.empty() so that an outer chain (typically a BasicBeanStore's overridingParent
slot) can fall through to its remaining tiers.
This class is designed to live in the overridingParent slot of a BasicBeanStore. Pushing a
frame inserts a new tier-1 layer; popping it removes that layer. Stack order is enforced LIFO — popping
out of order or popping an empty stack throws IllegalStateException.
The implementation uses a ConcurrentLinkedDeque so that reads from concurrent HTTP request threads
see a consistent view while pushes and pops are in flight. Pushes/pops are expected to be infrequent (test
setup/teardown); reads are the hot path.
Composition, not inheritance. StackOverlay implements BeanStore rather than
extending BasicBeanStore so that it does not inherit the entries / defaults / parent storage
that a BasicBeanStore provides. A StackOverlay is purely a stack of pluggable bean stores.
Example:
See Also:
BasicBeanStore- The bean store this overlay sits in front of.
- Since:
- 10.0.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintdepth()Returns the current depth of the stack.<T> Optional<T>Returns a bean of the specified type.<T> Optional<T>Returns a bean of the specified type and name.getBeansOfType(Class<T> beanType) Returns all beans of the specified type, keyed by bean name.getBeanSupplier(Class<T> beanType) Returns the supplier for an unnamed bean of the specified type.getBeanSupplier(Class<T> beanType, String name) Returns the supplier for a named bean of the specified type.getBeanType(Class<T> beanType) Returns the implementation class registered for the specified bean type, if any.booleanReturnstrue if this store contains at least one bean of the specified type.booleanReturnstrue if this store contains a bean of the specified type and name.peek()Returns the top frame on the stack without removing it.pop()Pops the top frame off the stack.Pushes a new overlay frame onto the top of the stack.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.juneau.commons.inject.BeanStore
createBeanFromMethod, createBeanFromMethod, getMissingParams, getParams, hasAllParams, instantiate, invokeBeanMethod
-
Constructor Details
-
StackOverlay
public StackOverlay()Constructor.
-
-
Method Details
-
push
Pushes a new overlay frame onto the top of the stack.The frame becomes the highest-priority lookup target for subsequent
getBean(Class)/getBeanSupplier(Class)calls until it is popped.- Parameters:
overlay- The bean store to push. Must not benull or anIllegalArgumentExceptionis thrown.- Returns:
- This object.
-
pop
Pops the top frame off the stack.- Returns:
- The popped frame.
- Throws:
IllegalStateException- If the stack is empty.
-
depth
Returns the current depth of the stack.- Returns:
- The number of frames currently on the stack.
-
peek
Returns the top frame on the stack without removing it.- Returns:
- The top frame, or
null if the stack is empty.
-
getBean
Description copied from interface:BeanStoreReturns a bean of the specified type.If multiple beans of the specified type exist, the behavior is implementation-dependent. Typically, implementations will return the primary bean or throw an exception.
- Specified by:
getBeanin interfaceBeanStore- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.- Returns:
- The bean, or
Optional.empty()if no bean of the specified type exists.
-
getBean
Description copied from interface:BeanStoreReturns a bean of the specified type and name.- Specified by:
getBeanin interfaceBeanStore- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.name- The bean name. Can benull for unnamed beans.- Returns:
- The bean, or
Optional.empty()if no bean of the specified type and name exists.
-
getBeansOfType
Description copied from interface:BeanStoreReturns all beans of the specified type, keyed by bean name.The map keys are the bean names, and the values are the bean instances. If no beans of the specified type exist, returns an empty map.
- Specified by:
getBeansOfTypein interfaceBeanStore- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.- Returns:
- A map of bean names to bean instances. Never
null .
-
hasBean
Description copied from interface:BeanStoreReturnstrue if this store contains at least one bean of the specified type. -
hasBean
Description copied from interface:BeanStoreReturnstrue if this store contains a bean of the specified type and name. -
getBeanSupplier
Description copied from interface:BeanStoreReturns the supplier for an unnamed bean of the specified type.If no supplier is found in this store, searches the parent store recursively.
- Specified by:
getBeanSupplierin interfaceBeanStore- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.- Returns:
- The supplier, or
Optional.empty()if no supplier of the specified type exists.
-
getBeanSupplier
Description copied from interface:BeanStoreReturns the supplier for a named bean of the specified type.If no supplier with the specified name is found in this store, searches the parent store recursively.
- Specified by:
getBeanSupplierin interfaceBeanStore- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.name- The bean name. Can benull for unnamed beans.- Returns:
- The supplier, or
Optional.empty()if no supplier of the specified type and name exists.
-
getBeanType
Description copied from interface:BeanStoreReturns the implementation class registered for the specified bean type, if any.Type bindings let callers register a default implementation class to be instantiated when an actual bean of
beanType is needed. Bindings are inherited through the parent chain.The default implementation returns
Optional.empty()(no binding). Override in concrete stores (seeBasicBeanStore) to enable type binding.- Specified by:
getBeanTypein interfaceBeanStore- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type whose implementation class is being looked up.- Returns:
- The implementation class, or
Optional.empty()if no binding exists.
-
toString
-