Skip to main content
just formatting
Source Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418

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:

  1. Should we simplify block with conditional expression?
  2. 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.javaPlugin.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 viwithwith JSF page:

<a:support event="onRowClick" action="#{configPlugins.selectPlugin(p)}" reRender="pluginList"/>
<a:commandLink action="#{configPlugins.removePlugin(true)}" reRender="pluginList"/>
<a:support event="onRowClick" action="#{configPlugins.selectPlugin(p)}" reRender="pluginList"/>
<a:commandLink action="#{configPlugins.removePlugin(true)}" reRender="pluginList"/>

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:

  1. Should we simplify block with conditional expression?
  2. I reassign global variable "plugins" to introduce my filter function (is it not bad practice?)
 
@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 viwith JSF page:

<a:support event="onRowClick" action="#{configPlugins.selectPlugin(p)}" reRender="pluginList"/>
<a:commandLink action="#{configPlugins.removePlugin(true)}" reRender="pluginList"/>

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:

  1. Should we simplify block with conditional expression?
  2. 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"/>
added 665 characters in body
Source Link

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:

  1. Should we simplify block with conditional expression?
  2. I reassign global variable "plugins" to introduce my filter function (is it not bad practice?)
@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 viwith JSF page:

<a:support event="onRowClick" action="#{configPlugins.selectPlugin(p)}" reRender="pluginList"/>
<a:commandLink action="#{configPlugins.removePlugin(true)}" reRender="pluginList"/>

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:

  1. Should we simplify block with conditional expression?
  2. I reassign global variable "plugins" to introduce my filter function (is it not bad practice?)
@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);
        }
    });
}

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:

  1. Should we simplify block with conditional expression?
  2. I reassign global variable "plugins" to introduce my filter function (is it not bad practice?)
@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 viwith JSF page:

<a:support event="onRowClick" action="#{configPlugins.selectPlugin(p)}" reRender="pluginList"/>
<a:commandLink action="#{configPlugins.removePlugin(true)}" reRender="pluginList"/>
deleted 61 characters in body; edited title; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Refactoring Showing a small functionlist of plugins to filter

Good day guys! I have a question: How bettercan I refactor this function? I I show a list of plugins in JSF, and I should filter them. I added function "filterPlugins"filterPlugins but I have some questions:

  1. Should we simplify block with conditional expression?
  2. I reassign global variable "plugins" foto introduce my filter function (is it not bad practice?)

Here some piece of my code:

 
@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);
        }
    });
}

Refactoring a small function

Good day guys! I have a question: How better 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:

  1. Should we simplify block with conditional expression?
  2. I reassign global variable "plugins" fo introduce my filter function (is it not bad practice?)

Here some piece of my code:

@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);
        }
    });
}

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:

  1. Should we simplify block with conditional expression?
  2. I reassign global variable "plugins" to introduce my filter function (is it not bad practice?)
 
@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);
        }
    });
}
added 950 characters in body
Source Link
Loading
Source Link
Loading