Package 

Class Borg


  • 
    public final class Borg<C extends Object>
    
                        

    A thread-safe dependency injection and initialization orchestrator inspired by Star Trek's Borg collective.

    Why Borg?

    • Ensures deterministic initialization of complex, interdependent components (drones)

    • Prevents common pitfalls like circular dependencies and race conditions

    • Maximizes performance through parallel initialization where dependencies allow

    • Provides fail-fast behavior by validating the dependency graph upfront

    Key Features:

    • Thread-safe initialization with results cached for subsequent access

    • Automatic parallel initialization of independent components

    • Topological sorting to respect dependency order

    • Early detection of missing or circular dependencies

    • Coroutine-based for non-blocking operation

    • Generic context type for flexible initialization

    • Access to cached drone values during assimilation

    Example usage:

    // Dagger component example
    class DaggerDrone : BorgDrone<DaggerComponent, Context> {
        override suspend fun assimilate(context: Context) =
            DaggerAppComponent.factory().create(context)
    }
    
    class RepositoryDrone(private val daggerDrone: DaggerDrone) : BorgDrone<Repository, Context> {
        override fun requiredDrones() = listOf(daggerDrone::class.java)
    
        override suspend fun assimilate(context: Context, borg: Borg<Context>): Repository {
            val daggerComponent = borg.getAssimilated(daggerDrone::class.java)
            return daggerComponent.repository()
        }
    }
    • Method Summary

      Modifier and Type Method Description
      final Unit log(String message)
      final <T extends Any> T getAssimilated(Class<out BorgDrone<T, C>> droneClass) Gets a previously assimilated drone value.
      final <T extends Any> T requireAssimilated(Class<out BorgDrone<T, C>> droneClass) Gets a previously assimilated drone value, throwing if not found.
      final <ERROR CLASS> assimilate(C context) Orchestrates the initialization of all components in the collective while maximizing parallelism.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getAssimilated

         final <T extends Any> T getAssimilated(Class<out BorgDrone<T, C>> droneClass)

        Gets a previously assimilated drone value.

        Parameters:
        droneClass - The class of the drone whose value you want to retrieve
      • requireAssimilated

         final <T extends Any> T requireAssimilated(Class<out BorgDrone<T, C>> droneClass)

        Gets a previously assimilated drone value, throwing if not found.

        Parameters:
        droneClass - The class of the drone whose value you want to retrieve
      • assimilate

         final <ERROR CLASS> assimilate(C context)

        Orchestrates the initialization of all components in the collective while maximizing parallelism.

        Parameters:
        context - The context object needed for initialization of all drones