Class Components
- All Implemented Interfaces:
DescribableComponent
Map of Components stored per Component.Identifier.
Provides a cleaner interface to the ComponentRegistry and Configuration when interacting with the
configured Components.
- Since:
- 5.0.0
- Author:
- Steven van Beelen
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<C> Component<C> computeIfAbsent(Component.Identifier<C> identifier, Supplier<Component<C>> compute) Computes aComponentfor the givenidentifierwhen absent, otherwise returns theComponentput(Component)under theidentifier.booleancontains(Component.Identifier<?> identifier) Check whether there is aComponentpresent for the givenidentifier.voiddescribeTo(ComponentDescriptor descriptor) Describe the properties ofthis DescribableComponentwith the givendescriptor.get(Component.Identifier<C> identifier) Returns the identifiers of the components currently registered.voidpostProcessComponents(Consumer<Component<?>> processor) Invoke the givenprocessoron all components that are registered in this collection.<C> Component<C> Puts the givencomponent, identified by the givenidentifier, in this collection.<C> booleanreplace(Component.Identifier<C> identifier, UnaryOperator<Component<C>> replacement) Replace the component registered under the givenidentifierwith the instance returned by givenreplacementfunction.
-
Constructor Details
-
Components
public Components()
-
-
Method Details
-
get
Get anOptionalon theComponentregistered under the givenidentifier.When no exact match is found with the given
identifier, aClass.isAssignableFrom(Class)check is done between the storedtypesand the type of the givenidentifier.- Type Parameters:
C- The type of the component to retrieve.- Parameters:
identifier- The identifier to retrieve aComponentfor.- Returns:
- An
Optionalon theComponentregistered under the givenidentifier. - Throws:
AmbiguousComponentMatchException- When multiple matchingComponentsare found for the givenidentifier.
-
put
Puts the givencomponent, identified by the givenidentifier, in this collection.- Type Parameters:
C- The type of the component to put.- Parameters:
component- The component to put in this collection.- Returns:
- A previous component registered under the given
identifier, if present.
-
computeIfAbsent
@Nonnull public <C> Component<C> computeIfAbsent(@Nonnull Component.Identifier<C> identifier, @Nonnull Supplier<Component<C>> compute) Computes aComponentfor the givenidentifierwhen absent, otherwise returns theComponentput(Component)under theidentifier.The given
computeoperation is only invoked when there is noComponentpresent for the givenidentifier.- Type Parameters:
C- The type of the component to get and compute if absent.- Parameters:
identifier- The identifier for which to check if aComponentis already present.compute- The lambda computing theComponentto put into this collection when absent.- Returns:
- The previously
put Componentidentifier by the givenidentifier. When absent, the outcome of thecomputeoperation is returned
-
contains
-
identifiers
Returns the identifiers of the components currently registered.- Returns:
- A set with the identifiers of registered components.
-
replace
public <C> boolean replace(@Nonnull Component.Identifier<C> identifier, @Nonnull UnaryOperator<Component<C>> replacement) Replace the component registered under the givenidentifierwith the instance returned by givenreplacementfunction. If no component is registered under the given identifier, nothing happens.If the given
replacementfunction returns null, the component registration is removed.- Type Parameters:
C- The type of component registered.- Parameters:
identifier- The identifier of the component to replace.replacement- The function providing the replacement value, based on the currently registered component.- Returns:
trueif a component is present and has been replaced,falseif no component was present, or has been removed by the replacement function.
-
postProcessComponents
Invoke the givenprocessoron all components that are registered in this collection.Exceptions thrown by the processor will be rethrown to the caller before all components have been processed.
- Parameters:
processor- The action to invoke for each component.
-
describeTo
Description copied from interface:DescribableComponentDescribe the properties ofthis DescribableComponentwith the givendescriptor.Components should call the appropriate
describePropertymethods on the descriptor to register their properties. The descriptor is responsible for determining how these properties are formatted and structured in the final output.Best Practices: As a general rule, all relevant fields of a
DescribableComponentimplementation should be described in this method. However, developers have discretion to include only the fields that make sense in the context. Not every field may be meaningful for description purposes, especially internal implementation details. Furthermore, components might want to expose different information based on their current state. The final decision on what properties to include lies with the person implementing thedescribeTomethod, who should focus on providing information that is useful for understanding the component's configuration and state.Example implementation:
public void describeTo(ComponentDescriptor descriptor) { descriptor.describeProperty("name", this.name); descriptor.describeProperty("enabled", this.enabled); descriptor.describeProperty("configuration", this.configuration); // A nested component descriptor.describeProperty("handlers", this.eventHandlers); // A collection }- Specified by:
describeToin interfaceDescribableComponent- Parameters:
descriptor- The component descriptor to describethis DescribableComponentn its properties in.
-