Package com.yahoo.component.provider
Class ComponentRegistry<COMPONENT>
- java.lang.Object
-
- com.yahoo.component.provider.ComponentRegistry<COMPONENT>
-
public class ComponentRegistry<COMPONENT> extends 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 to avoid locking and memory synchronization on lookups.- Author:
- bratseth
-
-
Constructor Summary
Constructors Constructor Description ComponentRegistry()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description List<COMPONENT>
allComponents()
Returns an unmodifiable snapshot of all components present in this registry.Map<ComponentId,COMPONENT>
allComponentsById()
Returns an unmodifiable snapshot of all components present in this registry, by id.protected static Version
findBestMatch(VersionSpecification versionSpec, Set<Version> versions)
Finds the best (highest) matching version among a set.void
freeze()
Freezes this registry to prevent further changes.COMPONENT
getComponent(ComponentId id)
COMPONENT
getComponent(ComponentSpecification id)
Returns a component.COMPONENT
getComponent(String componentSpecification)
See getComponent(ComponentSpecification)int
getComponentCount()
Returns the number of components in thisboolean
isFrozen()
returns whether this is currently frozenvoid
register(ComponentId id, COMPONENT component)
Registers a component unless this registry is frozen.static <COMPONENT>
ComponentRegistry<COMPONENT>singleton(ComponentId id, COMPONENT component)
Returns a frozen registry with a single component, for convenienceCOMPONENT
unregister(ComponentId id)
Unregisters a component unless this registry is frozen.
-
-
-
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:
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(String componentSpecification)
See getComponent(ComponentSpecification)- Parameters:
componentSpecification
- a component specification string, seeVersion
- Returns:
- the component or null if no component of this name (and version, if specified) is registered here
-
getComponent
public COMPONENT getComponent(ComponentId id)
-
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, 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 List<COMPONENT> allComponents()
Returns an unmodifiable snapshot of all components present in this registry.
-
allComponentsById
public 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
-
-