📄 serviceregistry.java
字号:
{ for (int i = 0; i < categories.length; i++) if (categories[i] == category) return registerServiceProvider(provider, i); throw new IllegalArgumentException(); } /** * Registers a provider under all service categories it * implements. * * <p>If <code>provider</code> implements the {@link * RegisterableService} interface, its {@link * RegisterableService#onRegistration onRegistration} method is * invoked in order to inform the provider about the addition to * this registry. If <code>provider</code> implements several * service categories, <code>onRegistration</code> gets called * multiple times. * * @param provider the service provider to be registered. * * @throws IllegalArgumentException if <code>provider</code> is * <code>null</code>, or if <code>provider</code> does not implement * any of the service categories passed to the {@linkplain * #ServiceRegistry(Iterator) constructor} of this ServiceRegistry. */ public synchronized void registerServiceProvider(Object provider) { boolean ok = false; if (provider == null) throw new IllegalArgumentException(); for (int i = 0; i < categories.length; i++) if (categories[i].isInstance(provider)) { ok = true; registerServiceProvider(provider, i); } if (!ok) throw new IllegalArgumentException(); } /** * Registers a number of providers under all service categories they * implement. * * <p>If a provider implements the {@link RegisterableService} * interface, its {@link RegisterableService#onRegistration * onRegistration} method is invoked in order to inform the provider * about the addition to this registry. If <code>provider</code> * implements several service categories, * <code>onRegistration</code> gets called multiple times. * * @throws IllegalArgumentException if <code>providers</code> is * <code>null</code>, if any iterated provider is <code>null</code>, * or if some iterated provider does not implement any of the * service categories passed to the {@linkplain * #ServiceRegistry(Iterator) constructor} of this * <code>ServiceRegistry</code>. */ public synchronized void registerServiceProviders(Iterator providers) { if (providers == null) throw new IllegalArgumentException(); while (providers.hasNext()) registerServiceProvider(providers.next()); } /** * De-registers a provider for a service category which is specified * by the class-internal category ID. * * @param provider the service provider to be registered. * * @param cat the service category, which is identified by an index * into the {@link #categories} array. * * @return <code>true</code> if <code>provider</code> was previously * registered for the specified service category; <code>false</code> * if if the provider had not been registered. * * @throws IllegalArgumentException if <code>provider</code> is * <code>null</code>. * * @throws ClassCastException if <code>provider</code> does not * implement the specified service provider interface. */ private synchronized boolean deregisterServiceProvider(Object provider, int cat) { LinkedList provs; boolean result; Class category; if (provider == null) throw new IllegalArgumentException(); category = categories[cat]; if (!category.isInstance(provider)) throw new ClassCastException(category.getName()); provs = providers[cat]; if (provs == null) return false; result = provs.remove(provider); if (provs.isEmpty()) providers[cat] = null; if (result && (provider instanceof RegisterableService)) ((RegisterableService) provider).onDeregistration(this, category); return result; } /** * De-registers a provider for the specified service category. * * <p>If <code>provider</code> implements the {@link * RegisterableService} interface, its {@link * RegisterableService#onDeregistration onDeregistration} method is * invoked in order to inform the provider about the removal from * this registry. * * @param provider the service provider to be de-registered. * * @param category the service category from which * <code>provider</code> shall be de-registered. * * @return <code>true</code> if <code>provider</code> was previously * registered for the specified service category; <code>false</code> * if if the provider had not been registered. * * @throws IllegalArgumentException if <code>provider</code> is * <code>null</code>, or if <code>category</code> is not among the * categories passed to the {@linkplain #ServiceRegistry(Iterator) * constructor} of this ServiceRegistry. * * @throws ClassCastException if <code>provider</code> does not * implement <code>category</code>. */ public synchronized boolean deregisterServiceProvider(Object provider, Class category) { for (int i = 0; i < categories.length; i++) if (categories[i] == category) return deregisterServiceProvider(provider, i); throw new IllegalArgumentException(); } /** * De-registers a provider from all service categories it * implements. * * <p>If <code>provider</code> implements the {@link * RegisterableService} interface, its {@link * RegisterableService#onDeregistration onDeregistration} method is * invoked in order to inform the provider about the removal from * this registry. If <code>provider</code> implements several * service categories, <code>onDeregistration</code> gets called * multiple times.</p> * * @param provider the service provider to be de-registered. * * @throws IllegalArgumentException if <code>provider</code> is * <code>null</code>, or if <code>provider</code> does not implement * any of the service categories passed to the {@linkplain * #ServiceRegistry(Iterator) constructor} of this * <code>ServiceRegistry</code>. */ public synchronized void deregisterServiceProvider(Object provider) { boolean ok = false; if (provider == null) throw new IllegalArgumentException(); for (int i = 0; i < categories.length; i++) if (categories[i].isInstance(provider)) { ok = true; deregisterServiceProvider(provider, i); } if (!ok) throw new IllegalArgumentException(); } /** * De-registers all providers which have been registered for the * specified service category. * * <p>If a provider implements the {@link RegisterableService} * interface, its {@link RegisterableService#onDeregistration * onDeregistration} method is invoked in order to inform the * provider about the removal from this registry. If the provider * implements several service categories, * <code>onDeregistration</code> gets called multiple times. * * @param category the category whose registered providers will be * de-registered. * * @throws IllegalArgumentException if <code>category</code> is not * among the categories passed to the {@linkplain * #ServiceRegistry(Iterator) constructor} of this * <code>ServiceRegistry</code>. */ public synchronized void deregisterAll(Class category) { boolean ok = false; for (int i = 0; i < categories.length; i++) { if (categories[i] != category) continue; ok = true; while (providers[i] != null) deregisterServiceProvider(providers[i].get(0), i); } if (!ok) throw new IllegalArgumentException(); } /** * De-registers all service providers. * * <p>If a provider implements the {@link RegisterableService} * interface, its {@link RegisterableService#onDeregistration * onDeregistration} method is invoked in order to inform the * provider about the removal from this registry. If the provider * implements several service categories, * <code>onDeregistration</code> gets called multiple times. */ public synchronized void deregisterAll() { for (int i = 0; i < categories.length; i++) while (providers[i] != null) deregisterServiceProvider(providers[i].get(0), i); } /** * Called by the Virtual Machine when it detects that this * <code>ServiceRegistry</code> has become garbage. De-registers all * service providers, which will cause those that implement {@link * RegisterableService} to receive a {@link * RegisterableService#onDeregistration onDeregistration} * notification. */ public void finalize() throws Throwable { super.finalize(); deregisterAll(); } /** * Determines whether a provider has been registered with this * registry. * * @return <code>true</code> if <code>provider</code> has been * registered under any service category; <code>false</code> if * it is not registered. * * @throws IllegalArgumentException if <code>provider</code> is * <code>null</code>. */ public synchronized boolean contains(Object provider) { if (provider == null) throw new IllegalArgumentException(); // Note that contains is rather unlikely to be ever called, // so it would be wasteful to keep a special data structure // (such as a HashSet) for making it a fast operation. for (int i = 0; i < providers.length; i++) { // If provider does not implement categories[i], // it would not have been possible to register it there. // In that case, it would be pointless to look there. if (!categories[i].isInstance(provider)) continue; // But if the list of registered providers contains provider, // we have found it. LinkedList p = providers[i]; if (p != null && p.contains(provider)) return true; } return false; } /** * Returns the index in {@link #categories} occupied by the * specified service category. * * @throws IllegalArgumentException if <code>category</code> is not * among the categories passed to the {@linkplain * #ServiceRegistry(Iterator) constructor} of this ServiceRegistry. */ private int getCategoryID(Class category) { for (int i = 0; i < categories.length; i++) if (categories[i] == category) return i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -