commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1720415 - /commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/utils/Providers.java
Date Wed, 16 Dec 2015 18:58:23 GMT
Author: mbenson
Date: Wed Dec 16 18:58:23 2015
New Revision: 1720415

URL: http://svn.apache.org/viewvc?rev=1720415&view=rev
Log:
eliminate raw type references and associated warnings; extract Provider sort logic into a
worker class to eliminate repetition of the type parameter among methods

Modified:
    commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/utils/Providers.java

Modified: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/utils/Providers.java
URL: http://svn.apache.org/viewvc/commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/utils/Providers.java?rev=1720415&r1=1720414&r2=1720415&view=diff
==============================================================================
--- commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/utils/Providers.java
(original)
+++ commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/utils/Providers.java
Wed Dec 16 18:58:23 2015
@@ -44,167 +44,174 @@ public final class Providers {
         VISITING, VISITED;
     }
 
-    /**
-     * Sort the specified providers with respect to declared {@link Consumes} and {@link
Produces} annotations.
-     *
-     * @param providers
-     * @return {@link Iterable} of {@code P}
-     */
-    @SuppressWarnings("rawtypes")
-    public static <P extends WeaveLifecycleProvider<?>> Iterable<P> sort(final
Iterable<P> providers) {
-        Validate.noNullElements(providers);
-
-        final Map<Class<? extends WeaveLifecycleProvider>, Set<Class<? extends
WeaveLifecycleProvider>>> dependencyMap =
-            toDependencyMap(providers);
-
-        final Collection<Class<? extends WeaveLifecycleProvider>> order =
-            new LinkedHashSet<Class<? extends WeaveLifecycleProvider>>();
-
-        final Map<Class<? extends WeaveLifecycleProvider>, State> stateMap =
-            new HashMap<Class<? extends WeaveLifecycleProvider>, Providers.State>();
-        final Deque<Class<? extends WeaveLifecycleProvider>> visiting =
-            new ArrayDeque<Class<? extends WeaveLifecycleProvider>>();
-
-        for (Class<? extends WeaveLifecycleProvider> type : dependencyMap.keySet())
{
-            final State state = stateMap.get(type);
-
-            if (state == null) {
-                tsort(type, dependencyMap, stateMap, visiting, order);
-            } else if (state == State.VISITING) {
-                throw new RuntimeException("Unexpected node in visiting state: " + type);
+    private static class SortWorker<P extends WeaveLifecycleProvider<?>> {
+        /**
+         * Implement {@link Providers#sort(Iterable)}
+         *
+         * @param providers to sort
+         * @return {@link Iterable} of {@code P}
+         */
+        Iterable<P> sort(final Iterable<P> providers) {
+            Validate.noNullElements(providers);
+
+            final Map<Class<? extends P>, Set<Class<? extends P>>>
dependencyMap = toDependencyMap(providers);
+
+            final Collection<Class<? extends P>> order = new LinkedHashSet<Class<?
extends P>>();
+
+            final Map<Class<? extends P>, State> stateMap = new HashMap<Class<?
extends P>, Providers.State>();
+            final Deque<Class<? extends P>> visiting = new ArrayDeque<Class<?
extends P>>();
+
+            for (Class<? extends P> type : dependencyMap.keySet()) {
+                final State state = stateMap.get(type);
+
+                if (state == null) {
+                    tsort(type, dependencyMap, stateMap, visiting, order);
+                } else if (state == State.VISITING) {
+                    throw new RuntimeException("Unexpected node in visiting state: " + type);
+                }
             }
+            return imposeOrder(providers, order);
         }
 
-        return imposeOrder(providers, order);
-    }
-
-    /**
-     * Adapted from Apache Ant's target sorting mechanism.
-     *
-     * @param root current provider type
-     * @param dependencyMap {@link Map} of provider type to dependencies
-     * @param stateMap {@link Map} of current visitation state
-     * @param visiting {@link Deque} used as a stack
-     * @param target destination {@link Collection} which must preserve order
-     */
-    @SuppressWarnings("rawtypes")
-    private static <P extends WeaveLifecycleProvider<?>> void tsort(final Class<?
extends WeaveLifecycleProvider> root,
-        final Map<Class<? extends WeaveLifecycleProvider>, Set<Class<? extends
WeaveLifecycleProvider>>> dependencyMap,
-        final Map<Class<? extends WeaveLifecycleProvider>, State> stateMap,
-        final Deque<Class<? extends WeaveLifecycleProvider>> visiting,
-        final Collection<Class<? extends WeaveLifecycleProvider>> target) {
-
-        stateMap.put(root, State.VISITING);
-        visiting.push(root);
-
-        for (Class<? extends WeaveLifecycleProvider> dependency : dependencyMap.get(root))
{
-            final State state = stateMap.get(dependency);
-            if (state == State.VISITED) {
-                continue;
+        /**
+         * Adapted from Apache Ant's target sorting mechanism.
+         *
+         * @param root current provider type
+         * @param dependencyMap {@link Map} of provider type to dependencies
+         * @param stateMap {@link Map} of current visitation state
+         * @param visiting {@link Deque} used as a stack
+         * @param target destination {@link Collection} which must preserve order
+         */
+        private void tsort(final Class<? extends P> root,
+            final Map<Class<? extends P>, Set<Class<? extends P>>>
dependencyMap,
+            final Map<Class<? extends P>, State> stateMap, final Deque<Class<?
extends P>> visiting,
+            final Collection<Class<? extends P>> target) {
+
+            stateMap.put(root, State.VISITING);
+            visiting.push(root);
+
+            for (Class<? extends P> dependency : dependencyMap.get(root)) {
+                final State state = stateMap.get(dependency);
+                if (state == State.VISITED) {
+                    continue;
+                }
+                Validate.validState(state == null, "Circular dependency: %s of %s", dependency.getName(),
+                    root.getName());
+                tsort(dependency, dependencyMap, stateMap, visiting, target);
+            }
+            final Class<? extends P> top = visiting.pop();
+            Validate.validState(top == root, "Stack out of balance: expected %s, found %s",
root.getName(),
+                top.getName());
+
+            stateMap.put(root, State.VISITED);
+            target.add(root);
+        }
+
+        /**
+         * Read any {@link Produces} annotation associated with {@code providerClass}, designating
types before which it
+         * should be invoked.
+         *
+         * @param providerClass
+         * @return {@link Class}[]
+         */
+        private Class<? extends P>[] producedBy(final Class<? extends P> providerClass)
{
+            Validate.notNull(providerClass);
+            final Produces produces = providerClass.getAnnotation(Produces.class);
+            if (produces == null || produces.value().length == 0) {
+                @SuppressWarnings("unchecked")
+                final Class<? extends P>[] empty = (Class<? extends P>[]) ArrayUtils.EMPTY_CLASS_ARRAY;
+                return empty;
             }
-            Validate.validState(state == null, "Circular dependency: %s of %s", dependency.getName(),
root.getName());
-            tsort(dependency, dependencyMap, stateMap, visiting, target);
-        }
-        final Class<? extends WeaveLifecycleProvider> top = visiting.pop();
-        Validate.validState(top == root, "Stack out of balance: expected %s, found %s", root.getName(),
top.getName());
-
-        stateMap.put(root, State.VISITED);
-        target.add(root);
-    }
-
-    /**
-     * Read any {@link Produces} annotation associated with {@code providerClass}, designating
types before which it
-     * should be invoked.
-     *
-     * @param providerClass
-     * @return {@link Class}[]
-     */
-    private static <P extends WeaveLifecycleProvider<?>> Class<? extends P>[]
producedBy(
-        final Class<? extends P> providerClass) {
-        Validate.notNull(providerClass);
-        final Produces produces = providerClass.getAnnotation(Produces.class);
-        if (produces == null || produces.value().length == 0) {
             @SuppressWarnings("unchecked")
-            final Class<? extends P>[] empty = (Class<? extends P>[]) ArrayUtils.EMPTY_CLASS_ARRAY;
-            return empty;
+            final Class<? extends P>[] result = (Class<? extends P>[]) produces.value();
+            return result;
         }
-        @SuppressWarnings("unchecked")
-        final Class<? extends P>[] result = (Class<? extends P>[]) produces.value();
-        return result;
-    }
 
-    /**
-     * Read any {@link Consumes} annotation associated with {@code providerClass} as dependencies.
-     *
-     * @param providerClass
-     * @return {@link Class}[]
-     */
-    private static <P extends WeaveLifecycleProvider<?>> Class<? extends P>[]
consumedBy(
-        final Class<? extends P> providerClass) {
-        Validate.notNull(providerClass);
-        final Consumes consumes = providerClass.getAnnotation(Consumes.class);
-        if (consumes == null || consumes.value().length == 0) {
+        /**
+         * Read any {@link Consumes} annotation associated with {@code providerClass} as
dependencies.
+         *
+         * @param providerClass
+         * @return {@link Class}[]
+         */
+        private Class<? extends P>[] consumedBy(final Class<? extends P> providerClass)
{
+            Validate.notNull(providerClass);
+            final Consumes consumes = providerClass.getAnnotation(Consumes.class);
+            if (consumes == null || consumes.value().length == 0) {
+                @SuppressWarnings("unchecked")
+                final Class<? extends P>[] empty = (Class<? extends P>[]) ArrayUtils.EMPTY_CLASS_ARRAY;
+                return empty;
+            }
             @SuppressWarnings("unchecked")
-            final Class<? extends P>[] empty = (Class<? extends P>[]) ArrayUtils.EMPTY_CLASS_ARRAY;
-            return empty;
+            final Class<? extends P>[] result = (Class<? extends P>[]) consumes.value();
+            return result;
         }
-        @SuppressWarnings("unchecked")
-        final Class<? extends P>[] result = (Class<? extends P>[]) consumes.value();
-        return result;
-    }
 
-    /**
-     * Create a {@link Map} of provider type to dependency types.
-     *
-     * @param providers to inspect
-     * @return {@link Map}
-     */
-    @SuppressWarnings("rawtypes")
-    private static Map<Class<? extends WeaveLifecycleProvider>, Set<Class<?
extends WeaveLifecycleProvider>>> toDependencyMap(
-        final Iterable<? extends WeaveLifecycleProvider<?>> providers) {
-
-        final Map<Class<? extends WeaveLifecycleProvider>, Set<Class<? extends
WeaveLifecycleProvider>>> result =
-            LazyMap.lazyMap(
-                new HashMap<Class<? extends WeaveLifecycleProvider>, Set<Class<?
extends WeaveLifecycleProvider>>>(),
-                new Factory<Set<Class<? extends WeaveLifecycleProvider>>>()
{
+        /**
+         * Create a {@link Map} of provider type to dependency types.
+         *
+         * @param providers to inspect
+         * @return {@link Map}
+         */
+        private Map<Class<? extends P>, Set<Class<? extends P>>>
toDependencyMap(final Iterable<P> providers) {
+
+            final Map<Class<? extends P>, Set<Class<? extends P>>>
result = LazyMap.lazyMap(
+                new HashMap<Class<? extends P>, Set<Class<? extends P>>>(),
new Factory<Set<Class<? extends P>>>() {
 
                     @Override
-                    public Set<Class<? extends WeaveLifecycleProvider>> create()
{
-                        return new HashSet<Class<? extends WeaveLifecycleProvider>>();
+                    public Set<Class<? extends P>> create() {
+                        return new HashSet<Class<? extends P>>();
                     }
                 });
 
-        for (WeaveLifecycleProvider<?> provider : providers) {
-            final Class<? extends WeaveLifecycleProvider> type = provider.getClass();
-            Collections.addAll(result.get(type), consumedBy(type));
+            for (WeaveLifecycleProvider<?> provider : providers) {
+                @SuppressWarnings("unchecked")
+                final Class<? extends P> type = (Class<? extends P>) provider.getClass();
+                Collections.addAll(result.get(type), consumedBy(type));
+
+                for (Class<? extends P> dependent : producedBy(type)) {
+                    result.get(dependent).add(type);
+                }
+            }
+            return result;
+        }
 
-            for (Class<? extends WeaveLifecycleProvider> dependent : producedBy(type))
{
-                result.get(dependent).add(type);
+        /**
+         * Order providers.
+         *
+         * @param providers to sort
+         * @param order to respect
+         * @return reordered providers
+         */
+        private Iterable<P> imposeOrder(final Iterable<P> providers, final Iterable<Class<?
extends P>> order) {
+
+            final Set<P> result = new LinkedHashSet<P>();
+
+            for (Class<? extends P> type : order) {
+                for (P provider : providers) {
+                    if (type.isInstance(provider)) {
+                        result.add(provider);
+                    }
+                }
             }
+            return Collections.unmodifiableSet(result);
         }
-        return result;
+
+    }
+
+    private Providers() {
     }
 
     /**
-     * Order providers.
+     * Sort the specified providers with respect to declared {@link Consumes} and {@link
Produces} annotations.
      *
+     * @param
+     *            <P>
+     *            the {@link WeaveLifecycleProvider} type
      * @param providers to sort
-     * @param order to respect
-     * @return reordered providers
+     * @return {@link Iterable} of {@code P}
      */
-    @SuppressWarnings("rawtypes")
-    private static <P extends WeaveLifecycleProvider> Iterable<P> imposeOrder(final
Iterable<P> providers,
-        final Iterable<Class<? extends WeaveLifecycleProvider>> order) {
-
-        final Set<P> result = new LinkedHashSet<P>();
-
-        for (Class<? extends WeaveLifecycleProvider> type : order) {
-            for (P provider : providers) {
-                if (type.isInstance(provider)) {
-                    result.add(provider);
-                }
-            }
-        }
-        return Collections.unmodifiableSet(result);
+    public static <P extends WeaveLifecycleProvider<?>> Iterable<P> sort(final
Iterable<P> providers) {
+        return new SortWorker<P>().sort(providers);
     }
+
 }



Mime
View raw message