* opened up factory methods to return the concrete type created

* added @reverse@ method to create @OrderAwarePluginRegistry@ with the reverse order of the plugins

git-svn-id: svn+ssh://svn.synyx.de/var/svn/synyx/opensource/hera/trunk@8471 5a64d73e-33d6-4ccc-9058-23f8668ecac9
This commit is contained in:
Oliver Gierke
2009-12-28 20:39:53 +00:00
parent 456575717f
commit fde7b55dee
2 changed files with 60 additions and 10 deletions

View File

@@ -18,7 +18,25 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends
SimplePluginRegistry<T, S> {
@SuppressWarnings("unchecked")
private Comparator<T> comparator = new AnnotationAwareOrderComparator();
private static final Comparator<Object> DEFAULT_COMPARATOR =
new AnnotationAwareOrderComparator();
private static final Comparator<Object> REVERSE_COMPARATOR =
new Comparator<Object>() {
public int compare(Object o1, Object o2) {
return -1 * DEFAULT_COMPARATOR.compare(o1, o2);
}
};
private Comparator<Object> comparator;
protected OrderAwarePluginRegistry(Comparator<Object> comparator) {
this.comparator = comparator;
}
/**
@@ -28,9 +46,9 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends
* @param <S>
* @return
*/
public static <S, T extends Plugin<S>> PluginRegistry<T, S> create() {
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create() {
return new OrderAwarePluginRegistry<T, S>();
return new OrderAwarePluginRegistry<T, S>(DEFAULT_COMPARATOR);
}
@@ -42,10 +60,30 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends
* @param plugins
* @return
*/
public static <S, T extends Plugin<S>> PluginRegistry<T, S> create(
List<T> plugins) {
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(
List<? extends T> plugins) {
PluginRegistry<T, S> registry = create();
OrderAwarePluginRegistry<T, S> registry = create();
registry.setPlugins(plugins);
return registry;
}
/**
* Creates a new {@link OrderAwarePluginRegistry} with the given plugins and
* the order of the plugins reverted.
*
* @param <S>
* @param <T>
* @param plugins
* @return
*/
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> createReverse(
List<? extends T> plugins) {
OrderAwarePluginRegistry<T, S> registry =
new OrderAwarePluginRegistry<T, S>(REVERSE_COMPARATOR);
registry.setPlugins(plugins);
return registry;
@@ -77,4 +115,16 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends
super.addPlugin(plugin);
Collections.sort(getPlugins(), comparator);
}
/**
* Returns a new {@link OrderAwarePluginRegistry} with the order of the
* plugins reverted.
*
* @return
*/
public OrderAwarePluginRegistry<T, S> reverse() {
return createReverse(getPlugins());
}
}

View File

@@ -52,7 +52,7 @@ public class SimplePluginRegistry<T extends Plugin<S>, S> implements
* @param <S>
* @return
*/
public static <S, T extends Plugin<S>> PluginRegistry<T, S> create() {
public static <S, T extends Plugin<S>> SimplePluginRegistry<T, S> create() {
return new SimplePluginRegistry<T, S>();
}
@@ -65,10 +65,10 @@ public class SimplePluginRegistry<T extends Plugin<S>, S> implements
* @param <S>
* @return
*/
public static <S, T extends Plugin<S>> PluginRegistry<T, S> create(
List<T> plugins) {
public static <S, T extends Plugin<S>> SimplePluginRegistry<T, S> create(
List<? extends T> plugins) {
PluginRegistry<T, S> registry = create();
SimplePluginRegistry<T, S> registry = create();
registry.setPlugins(plugins);
return registry;