SingletonServiceLoader { @SuppressWarnings("unchecked") public static <T> SingletonServiceLoader<T> getServiceLoader(final Class<T> service) { if (null == service) { throw new NullPointerException("extension clazz is null"); } if (!service.isInterface()) { throw new IllegalArgumentException("extension clazz (" + service + "is not interface!"); } SingletonServiceLoader<T> serviceLoader = (SingletonServiceLoader<T>) LOADERS.get(service); if (null != serviceLoader) { return serviceLoader; } LOADERS.putIfAbsent(service, new SingletonServiceLoader<>(service)); return (SingletonServiceLoader<T>) LOADERS.get(service); } private  SingletonServiceLoader(final Class<T> service); @SuppressWarnings("unchecked") static SingletonServiceLoader<T> getServiceLoader(final Class<T> service); Optional<T> newServiceInstances();  }
@Test(expected = IllegalArgumentException.class) public void assertGetSingletonServiceLoaderWhenServiceIsNotAnInterface() { SingletonServiceLoader.getServiceLoader(String.class); }
@Test public void assertGetSingletonServiceLoader() { SingletonServiceLoader<TypedSPIFixture> actualFirstServiceLoader = SingletonServiceLoader.getServiceLoader(TypedSPIFixture.class); assertNotNull(actualFirstServiceLoader); SingletonServiceLoader<TypedSPIFixture> actualSecondServiceLoader = SingletonServiceLoader.getServiceLoader(TypedSPIFixture.class); assertNotNull(actualSecondServiceLoader); assertThat(actualFirstServiceLoader, is(actualSecondServiceLoader)); }
@Test(expected = NullPointerException.class) public void assertGetSingletonServiceLoaderWhenServiceIsNull() { SingletonServiceLoader.getServiceLoader(null); }