com.yahoo.component.chain.model.ChainsModel Maven / Gradle / Ivy
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.component.chain.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.component.provider.ComponentRegistry;
/**
* A model of how the chains and components should be created.
*
* @author Tony Vaagenes
*/
public class ChainsModel {
private final ComponentRegistry> chainSpecifications = new ComponentRegistry<>();
private final ComponentRegistry> componentModels = new ComponentRegistry<>();
public void register(ChainSpecification chainSpecification) {
chainSpecifications.register(chainSpecification.componentId,
ComponentAdaptor.create(chainSpecification.componentId, chainSpecification));
}
public void register(ComponentId globalComponentId, ChainedComponentModel componentModel) {
assert (componentModel.getComponentId().withoutNamespace().equals(
globalComponentId.withoutNamespace()));
componentModels.register(globalComponentId, ComponentAdaptor.create(globalComponentId, componentModel));
}
public Collection allComponents() {
Collection components = new ArrayList<>();
for (ComponentAdaptor component : componentModels.allComponents()) {
components.add(component.model);
}
return components;
}
public Collection allChainsFlattened() {
Resolver resolver = new Resolver() {
@Override
public ChainSpecification resolve(ComponentSpecification componentSpecification) {
ComponentAdaptor spec = chainSpecifications.getComponent(componentSpecification);
return (spec==null) ? null : spec.model;
}
};
Collection chains = new ArrayList<>();
for (ComponentAdaptor chain : chainSpecifications.allComponents()) {
chains.add(chain.model.flatten(resolver));
}
return chains;
}
public void validate() {
allChainsFlattened();
for (ComponentAdaptor chain : chainSpecifications.allComponents()) {
validate(chain.model);
}
}
private void validate(ChainSpecification model) {
for (ComponentSpecification componentSpec : model.componentReferences) {
if (componentModels.getComponent(componentSpec) == null) {
throw new RuntimeException("No component matching the component specification " + componentSpec);
}
}
}
// For testing
Map> chainSpecifications() {
return chainSpecifications.allComponentsById();
}
// For testing
Map> componentModels() {
return componentModels.allComponentsById();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy