public ChainSpecification flatten(Resolver<ChainSpecification> allChainSpecifications) { Deque<ComponentId> path = new ArrayDeque<>(); return flatten(allChainSpecifications, path); }
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; }
/** * @param allChainSpecifications resolves ChainSpecifications from ComponentSpecifications * as given in the inheritance fields. * @param path tracks which chains are used in each recursive invocation of flatten, used for detecting cycles. * @return ChainSpecification directly containing all the component references and phases of the inherited chains. */ private ChainSpecification flatten(Resolver<ChainSpecification> allChainSpecifications, Deque<ComponentId> path) { path.push(componentId); //if this turns out to be a bottleneck(which I seriously doubt), please add memoization Map<String, ComponentSpecification> resultingComponents = componentsByName(componentReferences); Map<String, Phase> resultingPhases = new LinkedHashMap<>(phases); for (ComponentSpecification inheritedChainSpecification : inheritance.chainSpecifications) { ChainSpecification inheritedChain = resolveChain(path, allChainSpecifications, inheritedChainSpecification). flatten(allChainSpecifications, path); mergeInto(resultingComponents, filterByComponentSpecification( filterByName(inheritedChain.componentReferences, names(componentReferences)), inheritance.excludedComponents)); mergeInto(resultingPhases, inheritedChain.phases); } path.pop(); return new ChainSpecification(componentId, inheritance.flattened(), resultingPhases.values(), new LinkedHashSet<>(resultingComponents.values())); }