Class StackOverlay

java.lang.Object
org.apache.juneau.commons.inject.StackOverlay
All Implemented Interfaces:
BeanStore

public final class StackOverlay extends Object implements BeanStore
Stack-aware 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:

// Create an overlay-capable bean store. StackOverlay overlay = new StackOverlay(); BasicBeanStore store = new BasicBeanStore(null, overlay); // Push a per-test override frame. overlay.push(new BasicBeanStore().addBean(MyService.class, mock)); store.getBean(MyService.class); // returns the mock // Pop it at end of test. overlay.pop();

See Also:
Since:
10.0.0
  • Constructor Details

  • Method Details

    • push

      public StackOverlay push(BeanStore overlay)
      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 be null or an IllegalArgumentException is thrown.
      Returns:
      This object.
    • pop

      public BeanStore pop()
      Pops the top frame off the stack.
      Returns:
      The popped frame.
      Throws:
      IllegalStateException - If the stack is empty.
    • depth

      public int depth()
      Returns the current depth of the stack.
      Returns:
      The number of frames currently on the stack.
    • peek

      public BeanStore peek()
      Returns the top frame on the stack without removing it.
      Returns:
      The top frame, or null if the stack is empty.
    • getBean

      public <T> Optional<T> getBean(Class<T> beanType)
      Description copied from interface: BeanStore
      Returns 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:
      getBean in interface BeanStore
      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

      public <T> Optional<T> getBean(Class<T> beanType, String name)
      Description copied from interface: BeanStore
      Returns a bean of the specified type and name.
      Specified by:
      getBean in interface BeanStore
      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The bean type.
      name - The bean name. Can be null for unnamed beans.
      Returns:
      The bean, or Optional.empty() if no bean of the specified type and name exists.
    • getBeansOfType

      public <T> Map<String,T> getBeansOfType(Class<T> beanType)
      Description copied from interface: BeanStore
      Returns 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:
      getBeansOfType in interface BeanStore
      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The bean type.
      Returns:
      A map of bean names to bean instances. Never null.
    • hasBean

      public boolean hasBean(Class<?> beanType)
      Description copied from interface: BeanStore
      Returns true if this store contains at least one bean of the specified type.
      Specified by:
      hasBean in interface BeanStore
      Parameters:
      beanType - The bean type.
      Returns:
      true if this store contains at least one bean of the specified type.
    • hasBean

      public boolean hasBean(Class<?> beanType, String name)
      Description copied from interface: BeanStore
      Returns true if this store contains a bean of the specified type and name.
      Specified by:
      hasBean in interface BeanStore
      Parameters:
      beanType - The bean type.
      name - The bean name. Can be null for unnamed beans.
      Returns:
      true if this store contains a bean of the specified type and name.
    • getBeanSupplier

      public <T> Optional<Supplier<T>> getBeanSupplier(Class<T> beanType)
      Description copied from interface: BeanStore
      Returns 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:
      getBeanSupplier in interface BeanStore
      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

      public <T> Optional<Supplier<T>> getBeanSupplier(Class<T> beanType, String name)
      Description copied from interface: BeanStore
      Returns 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:
      getBeanSupplier in interface BeanStore
      Type Parameters:
      T - The bean type.
      Parameters:
      beanType - The bean type.
      name - The bean name. Can be null for unnamed beans.
      Returns:
      The supplier, or Optional.empty() if no supplier of the specified type and name exists.
    • getBeanType

      public <T> Optional<Class<? extends T>> getBeanType(Class<T> beanType)
      Description copied from interface: BeanStore
      Returns 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 (see BasicBeanStore) to enable type binding.

      Specified by:
      getBeanType in interface BeanStore
      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

      public String toString()
      Overrides:
      toString in class Object