InjectorUtil { public static <T> T findIn( Class<T> type, Injector injector, List<Object> instances, boolean required) { final Optional<T> found = findAll(type, injector, instances) .findFirst(); if (required) { return found.orElseThrow(() -> new IllegalArgumentException( "Could not find any installed implementation of " + type.getName() + "." ) ); } else { return found.orElse(null); } } private  InjectorUtil(); static T findIn(
            Class<T> type, 
            Injector injector, 
            List<Object> instances, 
            boolean required); static Stream<T> findAll(
            Class<T> type, 
            Injector injector, 
            List<Object> instances);  }
@Test void findInNotRequired() { final Foo expected = InjectorUtil.findIn(Foo.class, injector, instances, false); assertEquals(foo, expected); assertNull(InjectorUtil.findIn(Integer.class, injector, instances, false)); }
@Test void findInRequired() { assertNotNull(InjectorUtil.findIn(Foo.class, injector, instances, true)); assertThrows(IllegalArgumentException.class, () -> InjectorUtil.findIn(Integer.class, injector, instances, true)); }
@Test void findInInjector() { assertSame(injector, InjectorUtil.findIn(Injector.class, injector, instances, true)); }