How can I refactor this function? I show a list of plugins in JSF, and I should filter them. I added function filterPlugins
but I have some questions:
- Should we simplify block with conditional expression?
- I reassign global variable "plugins" to introduce my filter function (is it not bad practice?)
load()
@NotNull
@DataModel("plugins")
List<Plugin> plugins;
public void load() {
plugins = pluginManager.getPlugins();
plugins = filterPlugins(searchParam);
SortingUtil.sort(plugins, SortingUtil.SortType.ID_ASC);
if (plugins.size() > 0) {
if (plugin != null && plugins.contains(plugin)) {
selectPlugin(plugin);
} else {
selectPlugin(plugins.get(0));
}
} else {
plugin = null;
}
}
@SuppressWarnings("unchecked")
private List<Plugin> filterPlugins(final String searchParam ){
return (List)Iterables.filter(plugins, new Predicate<Plugin>() {
@Override
public boolean apply(@Nullable Plugin plugin) {
return plugin.getName().contains(searchParam)
|| plugin.getNetworkClasses().contains(searchParam)
|| plugin.getClassName().contains(searchParam);
}
});
}
@Override
public void selectPlugin(Plugin p) {
plugin = p;
}
Plugin.java
public class Plugin {
private static final long serialVersionUID = -8424575107726996696L;
@NotNull
private Long id;
@NotNull
private String name;
@NotNull
private String networkClasses;
+ setters and getters
}
I want to pay your attention this code interacts with JSF page:
<a:support event="onRowClick" action="#{configPlugins.selectPlugin(p)}" reRender="pluginList"/>
<a:commandLink action="#{configPlugins.removePlugin(true)}" reRender="pluginList"/>
plugin
(singular), and theselectPlugin
method (theplugin = null;
vsselectPlugin
(no assignment toplugin
) looks a bit suspicious to me). \$\endgroup\$