From 4fa39f3744a9483ef9053950c8561bdd20c94da2 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 11 Jan 2010 14:40:28 +0000 Subject: [PATCH] * simplified generic signatures * made @SimplePluginRegistry@ safe against @null@ values git-svn-id: svn+ssh://svn.synyx.de/var/svn/synyx/opensource/hera/trunk@8546 5a64d73e-33d6-4ccc-9058-23f8668ecac9 --- .../org/synyx/hera/core/PluginRegistry.java | 4 +- .../synyx/hera/core/SimplePluginRegistry.java | 45 +++++++++++++------ 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/org/synyx/hera/core/PluginRegistry.java b/core/src/main/java/org/synyx/hera/core/PluginRegistry.java index ad4a076..80b89e1 100644 --- a/core/src/main/java/org/synyx/hera/core/PluginRegistry.java +++ b/core/src/main/java/org/synyx/hera/core/PluginRegistry.java @@ -97,7 +97,7 @@ public interface PluginRegistry, S> extends Iterable { * @return all {@link Plugin}s supporting the given delimiter or the given * {@link Plugin}s if none found */ - List getPluginsFor(S delimiter, List plugins); + List getPluginsFor(S delimiter, List plugins); /** @@ -134,5 +134,5 @@ public interface PluginRegistry, S> extends Iterable { * * @return */ - List> getPlugins(); + List getPlugins(); } \ No newline at end of file diff --git a/core/src/main/java/org/synyx/hera/core/SimplePluginRegistry.java b/core/src/main/java/org/synyx/hera/core/SimplePluginRegistry.java index 121bba9..98ade56 100644 --- a/core/src/main/java/org/synyx/hera/core/SimplePluginRegistry.java +++ b/core/src/main/java/org/synyx/hera/core/SimplePluginRegistry.java @@ -24,7 +24,7 @@ import java.util.List; /** * Basic implementation of {@link PluginRegistry}. Simply holds all given - * plugins in a list. + * plugins in a list dropping {@literal null} values silently on adding. * * @param the concrete plugin interface * @param the delimiter type @@ -42,9 +42,7 @@ public class SimplePluginRegistry, S> implements */ protected SimplePluginRegistry(List plugins) { - this.plugins = - null == plugins ? new ArrayList() - : new ArrayList(plugins); + this.plugins = filterNulls(plugins); } @@ -83,11 +81,32 @@ public class SimplePluginRegistry, S> implements */ public void setPlugins(List plugins) { - this.plugins = new ArrayList(); + this.plugins = filterNulls(plugins); + } - if (plugins != null) { - this.plugins.addAll(plugins); + + /** + * Filters {@literal null} values from the given list. + * + * @param plugins + * @return an empty {@link List} if the given list is {@literal null} or + * empty, or the original list without its {@literal null} elements. + */ + private List filterNulls(List plugins) { + + if (plugins == null || plugins.isEmpty()) { + return new ArrayList(); } + + List result = new ArrayList(); + + for (T plugin : plugins) { + if (plugin != null) { + result.add(plugin); + } + } + + return result; } @@ -100,11 +119,10 @@ public class SimplePluginRegistry, S> implements */ public SimplePluginRegistry addPlugin(T plugin) { - if (null == plugin) { - throw new IllegalArgumentException("Plugin must not be null!"); + if (plugin != null) { + this.plugins.add(plugin); } - this.plugins.add(plugin); return this; } @@ -211,12 +229,11 @@ public class SimplePluginRegistry, S> implements * * @see org.synyx.hera.core.PluginRegistry#getPluginsFor(S, java.util.List) */ - public List getPluginsFor(S delimiter, - List plugins) { + public List getPluginsFor(S delimiter, List plugins) { List candidates = getPluginsFor(delimiter); - return candidates.size() == 0 ? plugins : candidates; + return candidates.size() == 0 ? new ArrayList(plugins) : candidates; } @@ -239,7 +256,7 @@ public class SimplePluginRegistry, S> implements * * @return all plugins of the registry */ - public List getPlugins() { + public List getPlugins() { return Collections.unmodifiableList(plugins); }