Lookup { public Lookup(Set<Component> components, Configuration configuration, Bindings bindings, I18nResources i18nResources) { this.components = components.stream().collect(Collectors.toMap(Component::getName, component -> component)); SetMultimap<String, String> flattenedDependencies = HashMultimap.create(); for (Component component : components) { for (Component dependency : component.getAllDependencies()) { flattenedDependencies.put(component.getName(), dependency.getName()); } } this.flattenedDependencies = flattenedDependencies; this.fragments = components.stream() .flatMap(component -> component.getFragments().stream()) .collect(Collectors.toMap(Fragment::getName, fragment -> fragment)); this.layouts = components.stream() .flatMap(component -> component.getLayouts().stream()) .collect(Collectors.toMap(Layout::getName, layout -> layout)); this.configuration = configuration; this.bindings = bindings; this.i18nResources = i18nResources; } Lookup(Set<Component> components, Configuration configuration, Bindings bindings,
                  I18nResources i18nResources); Optional<Component> getComponent(String componentName); Optional<Fragment> getFragmentIn(String componentName, String fragmentName); List<Fragment> getBindings(String componentName, String zoneName); Optional<Layout> getLayoutIn(String componentName, String layoutName); Configuration getConfiguration(); I18nResources getI18nResources();  }
@Test public void testLookup() { Component c1 = createComponent("c1", ImmutableSet.of(createFragment("c1.f1"), createFragment("c1.f2")), Collections.emptySet()); Component c2 = createComponent("c2", ImmutableSet.of(createFragment("c2.f1"), createFragment("c2.f2")), ImmutableSet.of(c1)); Component c3 = createComponent("c3", ImmutableSet.of(createFragment("c3.f1"), createFragment("c3.f2")), Collections.emptySet()); Component root = createComponent("root", ImmutableSet.of(createFragment("root.f1")), ImmutableSet.of(c2, c3)); Lookup lookup = new Lookup(ImmutableSet.of(c1, c2, c3, root), null, null, null); Assert.assertEquals(lookup.getFragmentIn("root", "f1").get().getName(), "root.f1"); Assert.assertEquals(lookup.getFragmentIn("root", "root.f1").get().getName(), "root.f1"); Assert.assertEquals(lookup.getFragmentIn("root", "c2.f2").get().getName(), "c2.f2"); Assert.assertEquals(lookup.getFragmentIn("root", "c3.f1").get().getName(), "c3.f1"); Assert.assertEquals(lookup.getFragmentIn("root", "c1.f1").get().getName(), "c1.f1"); Assert.assertEquals(lookup.getFragmentIn("root", "foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("root", "root.foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("root", "c2.foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("root", "c3.foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("root", "c1.foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("root", "c0.foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c2", "f1").get().getName(), "c2.f1"); Assert.assertEquals(lookup.getFragmentIn("c2", "f2").get().getName(), "c2.f2"); Assert.assertEquals(lookup.getFragmentIn("c2", "c1.f1").get().getName(), "c1.f1"); Assert.assertEquals(lookup.getFragmentIn("c2", "c1.f2").get().getName(), "c1.f2"); Assert.assertEquals(lookup.getFragmentIn("c2", "foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c2", "c1.foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c2", "root.foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c2", "c0.foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c3", "f1").get().getName(), "c3.f1"); Assert.assertEquals(lookup.getFragmentIn("c3", "f2").get().getName(), "c3.f2"); Assert.assertEquals(lookup.getFragmentIn("c3", "foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c3", "c3.f00").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c3", "root.f00").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c3", "c0.f00").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c1", "f1").get().getName(), "c1.f1"); Assert.assertEquals(lookup.getFragmentIn("c1", "f2").get().getName(), "c1.f2"); Assert.assertEquals(lookup.getFragmentIn("c1", "foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c1", "c1.foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c1", "c2.foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c1", "root.foo").isPresent(), false); Assert.assertEquals(lookup.getFragmentIn("c1", "c0.foo").isPresent(), false); }