📄 serviceregistry.java
字号:
throw new IllegalArgumentException(); } /** * Retrieves all providers that have been registered for the * specified service category. * * @param category the service category whose providers are * to be retrieved. * * @param useOrdering <code>true</code> in order to retrieve the * providers in an order imposed by the {@linkplain #setOrdering * ordering constraints}; <code>false</code> in order to retrieve * the providers in any order. * * @throws IllegalArgumentException if <code>category</code> is not * among the categories passed to the {@linkplain * #ServiceRegistry(Iterator) constructor} of this * <code>ServiceRegistry</code>. * * @see #getServiceProviders(Class, Filter, boolean) */ public Iterator getServiceProviders(Class category, boolean useOrdering) { return getServiceProviders(category, null, useOrdering); } /** * Retrieves all providers that have been registered for the * specified service category and that satisfy the criteria * of a custom filter. * * @param category the service category whose providers are * to be retrieved. * * @param filter a custom filter, or <code>null</code> to * retrieve all registered providers for the specified * category. * * @param useOrdering <code>true</code> in order to retrieve the * providers in an order imposed by the {@linkplain #setOrdering * ordering constraints}; <code>false</code> in order to retrieve * the providers in any order. * * @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 Iterator getServiceProviders(Class category, Filter filter, boolean useOrdering) { int catid; LinkedList provs; ArrayList result; catid = getCategoryID(category); provs = providers[catid]; if (provs == null) return Collections.EMPTY_LIST.iterator(); result = new ArrayList(provs.size()); for (Iterator iter = provs.iterator(); iter.hasNext();) { Object provider = iter.next(); if (filter == null || filter.filter(provider)) result.add(provider); } // If we are supposed to obey ordering constraints, and // if any constraints have been imposed on the specified // service category, sort the result. if (useOrdering && constraints != null) { final Map cons = constraints[catid]; if (cons != null) Collections.sort(result, new Comparator() { public int compare(Object o1, Object o2) { Set s; if (o1 == o2) return 0; s = (Set) cons.get(o1); if (s != null && s.contains(o2)) return -1; // o1 < o2 s = (Set) cons.get(o2); if (s != null && s.contains(o1)) return 1; // o1 > o2 return 0; // o1 == o2 } }); } return result.iterator(); } /** * Returns one of the service providers that is a subclass of the * specified class. * * @param providerClass a class to search for. */ public synchronized Object getServiceProviderByClass(Class providerClass) { if (providerClass == null) throw new IllegalArgumentException(); // Note that the method getServiceProviderByClass is rather // unlikely to be ever called, so it would be wasteful to keep a // special data structure for making it a fast operation. for (int cat = 0; cat < categories.length; cat++) { if (!categories[cat].isAssignableFrom(providerClass)) continue; LinkedList provs = providers[cat]; if (provs == null) continue; for (Iterator iter = provs.iterator(); iter.hasNext();) { Object provider = iter.next(); if (providerClass.isInstance(provider)) return provider; } } return null; } /** * Adds an ordering constraint on service providers. * * @param category the service category to which an ordering * constraint is to be added. * * @param first the provider which is supposed to come before * <code>second</code>. * * @param second the provider which is supposed to come after * <code>first</code>. * * @throws IllegalArgumentException if <code>first</code> and * <code>second</code> are referring to the same object, or if one * of them is <code>null</code>. * * @see #unsetOrdering * @see #getServiceProviders(Class, Filter, boolean) */ public synchronized boolean setOrdering(Class category, Object firstProvider, Object secondProvider) { return addConstraint(getCategoryID(category), firstProvider, secondProvider); } /** * Removes an ordering constraint on service providers. * * @param category the service category from which an ordering * constraint is to be removed. * * @param first the provider which is supposed to come before * <code>second</code>. * * @param second the provider which is supposed to come after * <code>first</code>. * * @throws IllegalArgumentException if <code>first</code> and * <code>second</code> are referring to the same object, or if one * of them is <code>null</code>. * * @see #setOrdering */ public synchronized boolean unsetOrdering(Class category, Object firstProvider, Object secondProvider) { return removeConstraint(getCategoryID(category), firstProvider, secondProvider); } /** * Adds an ordering constraint on service providers. * * @param catid the service category ID, which is the * category’s index into the {@link #categories} array. * * @param first the provider which is supposed to come before * <code>second</code>. * * @param second the provider which is supposed to come after * <code>first</code>. * * @throws IllegalArgumentException if <code>first</code> and * <code>second</code> are referring to the same object, or if one * of them is <code>null</code>. */ private boolean addConstraint(int catid, Object first, Object second) { Set s; IdentityHashMap cons; // Also checks argument validity. removeConstraint(catid, second, first); if (constraints == null) constraints = new IdentityHashMap[categories.length]; cons = constraints[catid]; if (cons == null) cons = constraints[catid] = new IdentityHashMap(); s = (Set) cons.get(first); if (s == null) cons.put(first, s = new HashSet()); return s.add(second); } /** * Removes an ordering constraint on service providers. * * @param catid the service category ID, which is the * category’s index into the {@link #categories} array. * * @param first the provider which is supposed to come before * <code>second</code>. * * @param second the provider which is supposed to come after * <code>first</code>. * * @throws IllegalArgumentException if <code>first</code> and * <code>second</code> are referring to the same object, or if one * of them is <code>null</code>. */ private boolean removeConstraint(int catid, Object first, Object second) { Collection s; IdentityHashMap cons; if (first == null || second == null || first == second) throw new IllegalArgumentException(); if (constraints == null) return false; cons = constraints[catid]; if (cons == null) return false; s = (Collection) cons.get(first); if (s == null) return false; if (!s.remove(second)) return false; // If we removed the last constraint for a service category, // we can get free some memory. if (cons.isEmpty()) { constraints[catid] = null; boolean anyConstraints = false; for (int i = 0; i < constraints.length; i++) { if (constraints[i] != null) { anyConstraints = true; break; } } if (!anyConstraints) constraints = null; } return true; } /** * A filter for selecting service providers that match custom * criteria. * * @see ServiceRegistry#getServiceProviders(Class, Filter, * boolean) * * @since 1.4 * * @author Michael Koch (konqueror@gmx.de) * @author Sascha Brawer (brawer@dandelis.ch) */ public static interface Filter { /** * Checks whether the specified service provider matches the * constraints of this Filter. * * @param provider the service provider in question. * * @return <code>true</code> if <code>provider</code> matches the * criteria; <code>false</code> if it does not match. */ boolean filter(Object provider); };}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -