@Test public void testGetNoImplementation() { Assertions.assertThrows(IllegalStateException.class, () -> { Services.get(InterfaceWithoutImplementation.class); }); }
public static <S> S get(Class<S> serviceClass) { @SuppressWarnings("unchecked") S service = (S)INSTANCES.computeIfAbsent(serviceClass, sc -> { ServiceLoader<?> serviceLoader = ServiceLoader.load(sc); return StreamSupport.stream(serviceLoader.spliterator(), false).reduce((f1, f2) -> { throw new IllegalStateException( "Multiple implementations of " + sc.getName() + " found on the classpath: " + f1.getClass() + " and " + f2.getClass()); }).orElseThrow(() -> new IllegalStateException( "No implementation of " + sc.getName() + " found on the classpath.")); }); return service; }
Services { public static <S> S get(Class<S> serviceClass) { @SuppressWarnings("unchecked") S service = (S)INSTANCES.computeIfAbsent(serviceClass, sc -> { ServiceLoader<?> serviceLoader = ServiceLoader.load(sc); return StreamSupport.stream(serviceLoader.spliterator(), false).reduce((f1, f2) -> { throw new IllegalStateException( "Multiple implementations of " + sc.getName() + " found on the classpath: " + f1.getClass() + " and " + f2.getClass()); }).orElseThrow(() -> new IllegalStateException( "No implementation of " + sc.getName() + " found on the classpath.")); }); return service; } }
Services { public static <S> S get(Class<S> serviceClass) { @SuppressWarnings("unchecked") S service = (S)INSTANCES.computeIfAbsent(serviceClass, sc -> { ServiceLoader<?> serviceLoader = ServiceLoader.load(sc); return StreamSupport.stream(serviceLoader.spliterator(), false).reduce((f1, f2) -> { throw new IllegalStateException( "Multiple implementations of " + sc.getName() + " found on the classpath: " + f1.getClass() + " and " + f2.getClass()); }).orElseThrow(() -> new IllegalStateException( "No implementation of " + sc.getName() + " found on the classpath.")); }); return service; } private  Services(); }
Services { public static <S> S get(Class<S> serviceClass) { @SuppressWarnings("unchecked") S service = (S)INSTANCES.computeIfAbsent(serviceClass, sc -> { ServiceLoader<?> serviceLoader = ServiceLoader.load(sc); return StreamSupport.stream(serviceLoader.spliterator(), false).reduce((f1, f2) -> { throw new IllegalStateException( "Multiple implementations of " + sc.getName() + " found on the classpath: " + f1.getClass() + " and " + f2.getClass()); }).orElseThrow(() -> new IllegalStateException( "No implementation of " + sc.getName() + " found on the classpath.")); }); return service; } private  Services(); static S get(Class<S> serviceClass); }
Services { public static <S> S get(Class<S> serviceClass) { @SuppressWarnings("unchecked") S service = (S)INSTANCES.computeIfAbsent(serviceClass, sc -> { ServiceLoader<?> serviceLoader = ServiceLoader.load(sc); return StreamSupport.stream(serviceLoader.spliterator(), false).reduce((f1, f2) -> { throw new IllegalStateException( "Multiple implementations of " + sc.getName() + " found on the classpath: " + f1.getClass() + " and " + f2.getClass()); }).orElseThrow(() -> new IllegalStateException( "No implementation of " + sc.getName() + " found on the classpath.")); }); return service; } private  Services(); static S get(Class<S> serviceClass);  }