Class ComponentRegistry<COMPONENT>


  • public class ComponentRegistry<COMPONENT>
    extends java.lang.Object
    A generic superclass for component registries. Supports registration and lookup of components by id. The registry resolves id requests to the newest matching component version registered.

    This registry supports the freeze pattern - changes can be made to this registry until freeze() is called. Subsequent change attempts will cause an exception. Freezing a registry after building makes it possible toi avoid locking and memory synchronization on lookups.

    Author:
    bratseth
    • Constructor Detail

      • ComponentRegistry

        public ComponentRegistry()
    • Method Detail

      • freeze

        public void freeze()
        Freezes this registry to prevent further changes. Override this to freeze internal data structures and dependent objects. Overrides must call super. Calling freeze on an already frozen registry must have no effect.
      • isFrozen

        public final boolean isFrozen()
        returns whether this is currently frozen
      • register

        public void register​(ComponentId id,
                             COMPONENT component)
        Registers a component unless this registry is frozen. This will succeed even if this component name and version is already registered.
        Throws:
        java.lang.IllegalStateException - if this chain is frozen
      • unregister

        public COMPONENT unregister​(ComponentId id)
        Unregisters a component unless this registry is frozen. Note that the component is not deconstructed or otherwise modified in any way, this is the responsiblity of the caller.
        Parameters:
        id - the id of the component to be unregistered
        Returns:
        the component that was unregistered, or null if no such component was already registered
      • getComponent

        public COMPONENT getComponent​(java.lang.String componentSpecification)
        See getComponent(ComponentSpecification)
        Parameters:
        componentSpecification - a component specification string, see Version
        Returns:
        the component or null if no component of this name (and version, if specified) is registered here
      • getComponent

        public COMPONENT getComponent​(ComponentSpecification id)
        Returns a component. If the id does not specify an (exact) version, the newest (matching) version is returned. For example, if version 3.1 is specified and we have 3.1.0, 3.1.1 and 3.1.3 registered, 3.1.3 is returned.
        Parameters:
        id - the id of the component to return. May not include a version, or include an underspecified version, in which case the highest (matching) version which does not contain a qualifier is returned
        Returns:
        the search chain or null if no component of this name (and matching version, if specified) is registered
      • findBestMatch

        protected static Version findBestMatch​(VersionSpecification versionSpec,
                                               java.util.Set<Version> versions)
        Finds the best (highest) matching version among a set.
        Returns:
        the matching version, or null if there are no matches
      • allComponents

        public java.util.List<COMPONENT> allComponents()
        Returns an unmodifiable snapshot of all components present in this registry.
      • allComponentsById

        public java.util.Map<ComponentId,​COMPONENT> allComponentsById()
        Returns an unmodifiable snapshot of all components present in this registry, by id.
      • getComponentCount

        public int getComponentCount()
        Returns the number of components in this
      • singleton

        public static <COMPONENT> ComponentRegistry<COMPONENT> singleton​(ComponentId id,
                                                                         COMPONENT component)
        Returns a frozen registry with a single component, for convenience