001package eu.cqse.check.framework.util.javascript; 002 003import java.util.HashMap; 004import java.util.Map; 005 006import org.conqat.lib.commons.collections.PairList; 007import org.conqat.lib.commons.collections.SetMap; 008 009import eu.cqse.check.framework.scanner.IToken; 010 011/** Collects Closure namespaces and their dependencies. */ 012public class ClosureDependencyResult { 013 014 /** 015 * Maps a declared namespace (Closure constructor) to the corresponding token. 016 */ 017 private final Map<String, IToken> namespaces = new HashMap<>(); 018 019 /** 020 * Maps namespaces to their dependency namespaces, which are stored with their 021 * respective token. 022 */ 023 private final Map<String, SetMap<String, IToken>> dependenciesByNamespace = new HashMap<>(); 024 025 /** 026 * Stores dependency namespaces for the next declared namespace in a file. 027 */ 028 private final PairList<String, IToken> insertsForNextNamespace = new PairList<>(); 029 030 /** 031 * Adds a new namespace for the current file, which is stored along with the 032 * token it is declared by (i.e., a constructor) 033 */ 034 public void addNamespace(String namespace, IToken declaringToken) { 035 namespaces.put(namespace, declaringToken); 036 insertsForNextNamespace.forEach((name, token) -> namespaces.put(name, token)); 037 } 038 039 /** 040 * Stores dependency namespaces for the next declared namespace in a file. This 041 * is a special case for Closure constructor comments, as we might see 042 * dependencies in them before the actual namespace is declard in the file. 043 */ 044 public void addDependencyForNextNamespace(String dependencyNamespace, IToken token) { 045 insertsForNextNamespace.add(dependencyNamespace, token); 046 } 047 048 /** @see #namespaces */ 049 public Map<String, IToken> getDeclaredNamespaces() { 050 return namespaces; 051 } 052 053 /** @see #dependenciesByNamespace */ 054 public Map<String, SetMap<String, IToken>> getDependenciesByNamespace() { 055 return dependenciesByNamespace; 056 } 057 058 /** 059 * Stores dependencyNamespace as a dependeny of declaredNamespace. 060 */ 061 public void addDependency(String declaredNamespace, String dependencyNamespace, IToken dependencyToken) { 062 if (!dependenciesByNamespace.containsKey(declaredNamespace)) { 063 dependenciesByNamespace.put(declaredNamespace, new SetMap<>()); 064 } 065 dependenciesByNamespace.get(declaredNamespace).add(dependencyNamespace, dependencyToken); 066 } 067}