/** * Deregisters the given provider from the given category. * * @param pProvider the provider instance * @param pCategory the category class * @return {@code true} if {@code pProvider} was previously registered in * the given category */ public boolean deregister(final Object pProvider, final Class<?> pCategory) { return getRegistry(pCategory).deregister(pProvider); }
void processDeregistration(final T pOldProvider) { if (pOldProvider instanceof RegisterableService) { RegisterableService service = (RegisterableService) pOldProvider; service.onDeregistration(ServiceRegistry.this, category); } }
void processRegistration(final T pProvider) { if (pProvider instanceof RegisterableService) { RegisterableService service = (RegisterableService) pProvider; service.onRegistration(ServiceRegistry.this, category); } }
public boolean register(final T pProvider) { checkCategory(pProvider); // NOTE: We only register the new instance, if we don't already have an instance of pProvider's class. if (!contains(pProvider)) { providers.put(pProvider.getClass(), pProvider); processRegistration(pProvider); return true; } return false; }
/** * Returns an {@code Iterator} containing all providers in the given * category. * <p/> * The iterator supports removal. * <p/> * <small> * NOTE: Removing a provider from the iterator, deregisters the current * provider (as returned by the last invocation of {@code next()}) from * {@code pCategory}, it does <em>not</em> remove the provider * from other categories in the registry. * </small> * * @param pCategory the category class * @return an {@code Iterator} containing all providers in the given * category. * @throws IllegalArgumentException if {@code pCategory} is not a valid * category in this registry */ protected <T> Iterator<T> providers(Class<T> pCategory) { return getRegistry(pCategory).providers(); }
private <T> boolean registerImpl(final Object pProvider, final Class<T> pCategory) { return getRegistry(pCategory).register(pCategory.cast(pProvider)); }
public boolean deregister(final Object pProvider) { checkCategory(pProvider); // NOTE: We remove any provider of the same class, this may or may // not be the same instance as pProvider. T oldProvider = providers.remove(pProvider.getClass()); if (oldProvider != null) { processDeregistration(oldProvider); return true; } return false; }
@Test(expected = IllegalArgumentException.class) public void testCreateNull() { new ServiceRegistry(null); }
/** * Returns an {@code Iterator} containing all categories in this registry * the given {@code pProvider} <em>may be registered with</em>. * <p/> * The iterator does not support removal. * * @param pProvider the provider instance * @return an {@code Iterator} containing all categories in this registry * the given {@code pProvider} may be registered with */ protected Iterator<Class<?>> compatibleCategories(final Object pProvider) { return new FilterIterator<Class<?>>(categories(), new FilterIterator.Filter<Class<?>>() { public boolean accept(Class<?> pElement) { return pElement.isInstance(pProvider); } }); }
@Test public void testContainingCategoriesNewInstanceRegisteredToo() { // NOTE: Currently we match based on type, rather than instance, but it does make sense... Iterator<Class<?>> categories = registry.containingCategories(new DummySPIToo()); assertTrue(categories.hasNext()); assertEquals(DummySPI.class, categories.next()); assertFalse(categories.hasNext()); }
@Test public void testContainingCategoriesNewInstanceRegisteredImpl() { // NOTE: Currently we match based on type, rather than instance, but it does make sense... Iterator<Class<?>> categories = registry.containingCategories(new DummySPIImpl()); assertTrue(categories.hasNext()); assertEquals(DummySPI.class, categories.next()); assertFalse(categories.hasNext()); }
@Test public void testCompatibleCategoriesImpl() { Iterator<Class<?>> categories = registry.compatibleCategories(new DummySPIImpl()); assertTrue(categories.hasNext()); assertEquals(DummySPI.class, categories.next()); assertFalse(categories.hasNext()); }
@Test public void testCompatibleCategoriesToo() { Iterator<Class<?>> categories = registry.compatibleCategories(new DummySPIToo()); assertTrue(categories.hasNext()); assertEquals(DummySPI.class, categories.next()); assertFalse(categories.hasNext()); }
/** * Registers the given provider for the given category. * * @param pProvider the provider instance * @param pCategory the category class * @return {@code true} if {@code pProvider} is now registered in * the given category */ public <T> boolean register(final T pProvider, final Class<? super T> pCategory) { return registerImpl(pProvider, pCategory); }
public void remove() { iterator.remove(); processDeregistration(current); } };
private <T> void putCategory(Map<Class<?>, CategoryRegistry> pMap, Class<T> pCategory) { CategoryRegistry<T> registry = new CategoryRegistry<T>(pCategory); pMap.put(pCategory, registry); }
@Test public void testDeregisterCompatibleNonRegistered() { DummySPI dummy = new DummySPI() {}; assertFalse(registry.deregister(dummy)); }
@SuppressWarnings("unchecked") public TestRegistry() { super(Arrays.asList(DummySPI.class).iterator()); registerApplicationClasspathSPIs(); } }
@Test public void testRegisterNull() { assertFalse(registry.register(null)); }
@Test public void testDeregisterNull() { assertFalse(registry.deregister(null)); }