EventUtil { public static <T> Stream<T> getImplementingComponents( Stream<Element> elementStream, Class<T> type) { return elementStream .flatMap(element -> element.getComponent().map(Stream::of) .orElseGet(Stream::empty)) .map(component -> getComponent(component, type)) .filter(Objects::nonNull); } private  EventUtil(); static List<BeforeEnterObserver> collectBeforeEnterObservers(
            Collection<? extends HasElement> oldChain,
            Collection<? extends HasElement> newChain); static List<BeforeEnterObserver> collectBeforeEnterObserversFromChain(
            Collection<? extends HasElement> chain,
            Collection<? extends HasElement> childrenExclusions); static List<BeforeEnterObserver> collectBeforeEnterObserversFromChainElement(
            HasElement element,
            Collection<? extends HasElement> childrenExclusions); static List<BeforeLeaveObserver> collectBeforeLeaveObservers(UI ui); static List<AfterNavigationObserver> collectAfterNavigationObservers(
            UI ui); static List<LocaleChangeObserver> collectLocaleChangeObservers(
            Element element); static List<LocaleChangeObserver> collectLocaleChangeObservers(
            List<HasElement> components); static void informLocaleChangeObservers(UI ui); static void informLocaleChangeObservers(UI ui,
            List<HasElement> components); static Stream<T> getImplementingComponents(
            Stream<Element> elementStream, Class<T> type); static void inspectHierarchy(Element node,
            Collection<Element> descendants, Predicate<Element> filter);  }
@Test public void getImplementingComponents() throws Exception { Element node = new Element("root"); node.appendChild(new Element("main"), new Element("menu")); Element nested = new Element("nested"); nested.appendChild(new Element("nested-child"), new Element("nested-child-2")); node.appendChild(nested); Component.from(nested, EnterObserver.class); List<Element> elements = new ArrayList<>(); EventUtil.inspectHierarchy(node, elements, element -> true); List<BeforeEnterObserver> listenerComponents = EventUtil .getImplementingComponents(elements.stream(), BeforeEnterObserver.class) .collect(Collectors.toList()); Assert.assertEquals("Wrong amount of listener instances found", 1, listenerComponents.size()); }
@Test public void getImplementingComponents_hasComposite_originalComponentIsReturned() { CompositeWrapper wrapper = new CompositeWrapper(); List<BeforeEnterObserver> components = EventUtil .getImplementingComponents(Stream.of(wrapper.getElement()), BeforeEnterObserver.class) .distinct().collect(Collectors.toList()); Assert.assertEquals(1, components.size()); Assert.assertEquals(EnterObserver.class, components.get(0).getClass()); }