From 660653cd1ed66cf61f3ff1e1f254e0800102655d Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 11 Jan 2010 14:23:12 +0000 Subject: [PATCH] * opened up @OrderAwarePluginRegistry@ to use any @Comparator@ implementation but falling back on Spring's @AnnotationAwareOrderComparator@ by default * refactored factory methods accordingly * polished JavaDoc git-svn-id: svn+ssh://svn.synyx.de/var/svn/synyx/opensource/hera/trunk@8538 5a64d73e-33d6-4ccc-9058-23f8668ecac9 --- .../hera/core/OrderAwarePluginRegistry.java | 127 ++++++++++++++---- 1 file changed, 102 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/org/synyx/hera/core/OrderAwarePluginRegistry.java b/core/src/main/java/org/synyx/hera/core/OrderAwarePluginRegistry.java index d18b14c..a176647 100644 --- a/core/src/main/java/org/synyx/hera/core/OrderAwarePluginRegistry.java +++ b/core/src/main/java/org/synyx/hera/core/OrderAwarePluginRegistry.java @@ -24,39 +24,60 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator; /** - * {@link PluginRegistry} implementation that can handle {@link Plugin}s using - * the {@link org.springframework.core.Ordered} interface or - * {@link org.springframework.core.annotation.Order} annotation. + * {@link PluginRegistry} implementation that be made aware of a certain + * ordering of {@link Plugin}s. By default it orders {@link Plugin}s by + * regarding {@link org.springframework.core.Ordered} interface or + * {@link org.springframework.core.annotation.Order} annotation. To alter + * ordering behaviour use one of the factory methods accepting a + * {@link Comparator} as parameter. * * @author Oliver Gierke - gierke@synyx.de */ public class OrderAwarePluginRegistry, S> extends SimplePluginRegistry { + /** + * Comparator regarding {@link org.springframework.core.Ordered} interface + * or {@link org.springframework.core.annotation.Order} annotation. + */ @SuppressWarnings("unchecked") private static final Comparator DEFAULT_COMPARATOR = new AnnotationAwareOrderComparator(); - private static final Comparator REVERSE_COMPARATOR = - new Comparator() { + /** + * Comparator reverting the {@value #DEFAULT_COMPARATOR}. + */ + private static final Comparator DEFAULT_REVERSE_COMPARATOR = + new RevertingComparator(DEFAULT_COMPARATOR); - public int compare(Object o1, Object o2) { - - return -1 * DEFAULT_COMPARATOR.compare(o1, o2); - } - }; - - private Comparator comparator; + private Comparator comparator; - protected OrderAwarePluginRegistry(Comparator comparator) { + /** + * Creates a new {@link OrderAwarePluginRegistry} with the given + * {@link Plugin}s and {@link Comparator}. + * + * @param plugins the {@link Plugin}s to be contained in the registry or + * {@literal null} if the registry shall be empty initally. + * @param comparator the {@link Comparator} to be used for ordering the + * {@link Plugin}s or {@literal null} if the {@code + * #DEFAULT_COMPARATOR} shall be used. + */ + protected OrderAwarePluginRegistry(List plugins, + Comparator comparator) { - this.comparator = comparator; + super(plugins); + + this.comparator = DEFAULT_COMPARATOR; + if (comparator != null) { + this.comparator = comparator; + } } /** - * Creates a new {@link SimplePluginRegistry}. + * Creates a new {@link OrderAwarePluginRegistry} using the {@code + * #DEFAULT_COMPARATOR}. * * @param * @param @@ -64,7 +85,22 @@ public class OrderAwarePluginRegistry, S> extends */ public static > OrderAwarePluginRegistry create() { - return new OrderAwarePluginRegistry(DEFAULT_COMPARATOR); + return create(null, null); + } + + + /** + * Creates a new {@link OrderAwarePluginRegistry} using the given + * {@link Comparator} for ordering contained {@link Plugin}s. + * + * @param + * @param + * @return + */ + public static > OrderAwarePluginRegistry create( + Comparator comparator) { + + return create(null, comparator); } @@ -79,10 +115,7 @@ public class OrderAwarePluginRegistry, S> extends public static > OrderAwarePluginRegistry create( List plugins) { - OrderAwarePluginRegistry registry = create(); - registry.setPlugins(plugins); - - return registry; + return create(plugins, DEFAULT_COMPARATOR); } @@ -98,11 +131,22 @@ public class OrderAwarePluginRegistry, S> extends public static > OrderAwarePluginRegistry createReverse( List plugins) { - OrderAwarePluginRegistry registry = - new OrderAwarePluginRegistry(REVERSE_COMPARATOR); - registry.setPlugins(plugins); + return create(plugins, DEFAULT_REVERSE_COMPARATOR); + } - return registry; + + /** + * Creates a new {@link OrderAwarePluginRegistry} with the given plugins. + * + * @param + * @param + * @param plugins + * @return + */ + public static > OrderAwarePluginRegistry create( + List plugins, Comparator comparator) { + + return new OrderAwarePluginRegistry(plugins, comparator); } @@ -139,8 +183,41 @@ public class OrderAwarePluginRegistry, S> extends * * @return */ + @SuppressWarnings("unchecked") public OrderAwarePluginRegistry reverse() { - return createReverse(getPlugins()); + return create(getPlugins(), new RevertingComparator(comparator)); + } + + /** + * Comparator to revert the ordering of the given delegate. + * + * @author Oliver Gierke - gierke@synyx.de + */ + private static final class RevertingComparator implements Comparator { + + private final Comparator delegate; + + + /** + * Creates a new {@link RevertingComparator}. + * + * @param delegate + */ + public RevertingComparator(Comparator delegate) { + + this.delegate = delegate; + } + + + /* + * (non-Javadoc) + * + * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) + */ + public int compare(T first, T second) { + + return -1 * delegate.compare(first, second); + } } }