Showing a list of plugins to filter 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&lt;Plugin&gt; plugins;

public void load() {
    plugins = pluginManager.getPlugins();

    plugins = filterPlugins(searchParam);

    SortingUtil.sort(plugins, SortingUtil.SortType.ID_ASC);
    if (plugins.size() &gt; 0) {
        if (plugin != null &amp;&amp; plugins.contains(plugin)) {
            selectPlugin(plugin);
        } else {
            selectPlugin(plugins.get(0));
        }
    } else {
        plugin = null;
    }
}

@SuppressWarnings("unchecked")
private List&lt;Plugin&gt; filterPlugins(final String searchParam ){
      return (List)Iterables.filter(plugins, new Predicate&lt;Plugin&gt;() {
        @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:

&lt;a:support event="onRowClick" action="#{configPlugins.selectPlugin(p)}" reRender="pluginList"/&gt;
&lt;a:commandLink action="#{configPlugins.removePlugin(true)}" reRender="pluginList"/&gt;
Early Return

You can return early to reduce the nesting of your if statements:

if (plugins.isEmpty()) {
    plugin = null;
    return;
}


Also note that I changed plugins.size() &gt; 0 to !plugins.isEmpty(), I think it's more readable.

Plugins Field

Code like this:


plugins = pluginManager.getPlugins(); // assign field

plugins = filterPlugins(searchParam); // method expects plugins field to be assigned, 
                                      // but doesn't work on it but returns instead



Can be a bit confusing. I would change it to:

plugins = filterPlugins(pluginManager.getPlugins(), searchParam);


And then change filterPlugins to:

private List&lt;Plugin&gt; filterPlugins(final List&lt;Plugin&gt; plugins, final String searchParam )


plugin field

I would still like to see the declaration of plugin as well as the selectPlugin method. As mentioned in the comments, it looks a bit like it might be bad design.