blob: 94f734b1c5df57d0602d0131147a53370459c7a3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
// 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<ComponentAdaptor<ChainSpecification>> chainSpecifications = new ComponentRegistry<>();
private final ComponentRegistry<ComponentAdaptor<ChainedComponentModel>> 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<ChainedComponentModel> allComponents() {
Collection<ChainedComponentModel> components = new ArrayList<>();
for (ComponentAdaptor<ChainedComponentModel> component : componentModels.allComponents()) {
components.add(component.model);
}
return components;
}
public Collection<ChainSpecification> allChainsFlattened() {
Resolver<ChainSpecification> resolver = new Resolver<ChainSpecification>() {
@Override
public ChainSpecification resolve(ComponentSpecification componentSpecification) {
ComponentAdaptor<ChainSpecification> spec = chainSpecifications.getComponent(componentSpecification);
return (spec==null) ? null : spec.model;
}
};
Collection<ChainSpecification> chains = new ArrayList<>();
for (ComponentAdaptor<ChainSpecification> chain : chainSpecifications.allComponents()) {
chains.add(chain.model.flatten(resolver));
}
return chains;
}
public void validate() {
allChainsFlattened();
for (ComponentAdaptor<ChainSpecification> 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<ComponentId, ComponentAdaptor<ChainSpecification>> chainSpecifications() {
return chainSpecifications.allComponentsById();
}
// For testing
Map<ComponentId, ComponentAdaptor<ChainedComponentModel>> componentModels() {
return componentModels.allComponentsById();
}
}
|