Skip to content

Commit

Permalink
Performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Oct 15, 2013
1 parent 72bfb7d commit 4cf9b79
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.jboss.forge.addon.shell;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Set;

import javax.enterprise.event.Observes;
import javax.inject.Inject;
Expand All @@ -15,6 +15,7 @@
import org.jboss.forge.addon.shell.aesh.ShellSingleCommand;
import org.jboss.forge.addon.shell.aesh.ShellWizard;
import org.jboss.forge.addon.shell.ui.ShellContext;
import org.jboss.forge.addon.shell.util.ShellUtil;
import org.jboss.forge.addon.ui.UICommand;
import org.jboss.forge.addon.ui.util.Commands;
import org.jboss.forge.addon.ui.wizard.UIWizard;
Expand Down Expand Up @@ -60,24 +61,41 @@ public UICommand lookup(Class<? extends UICommand> type)
return addonRegistry.getServices(type).get();
}

public Map<String, AbstractShellInteraction> getEnabledShellCommands(ShellContext shellContext)
public Set<String> getAllCommandNames(ShellContext shellContext)
{
Map<String, AbstractShellInteraction> commands = new TreeMap<String, AbstractShellInteraction>();
Set<String> commands = new HashSet<String>();
for (UICommand cmd : Commands.getEnabledCommands(getAllCommands(), shellContext))
{
commands.add(getCommandName(shellContext, cmd));
}
return commands;
}

public AbstractShellInteraction findCommand(ShellContext shellContext, String commandName)
{
AbstractShellInteraction result = null;
CommandLineUtil cmdLineUtil = getCommandLineUtil();
for (UICommand cmd : Commands.getEnabledCommands(getAllCommands(), shellContext))
{
AbstractShellInteraction shellCommand;
if (cmd instanceof UIWizard)
{
shellCommand = new ShellWizard((UIWizard) cmd, shellContext, cmdLineUtil, this);
}
else
if (commandName.equals(getCommandName(shellContext, cmd)))
{
shellCommand = new ShellSingleCommand(cmd, shellContext, cmdLineUtil);
if (cmd instanceof UIWizard)
{
result = new ShellWizard((UIWizard) cmd, shellContext, cmdLineUtil, this);
}
else
{
result = new ShellSingleCommand(cmd, shellContext, cmdLineUtil);
}
break;
}
commands.put(shellCommand.getName(), shellCommand);
}
return commands;
return result;
}

private String getCommandName(ShellContext shellContext, UICommand cmd)
{
return ShellUtil.shellifyName(cmd.getMetadata(shellContext).getName());
}

public Iterable<UICommand> getAllCommands()
Expand Down Expand Up @@ -114,17 +132,4 @@ ConverterFactory getConverterFactory()
}
return converterFactory;
}

/**
* Used in ShellImpl
*/
public AbstractShellInteraction findCommand(ShellContext shellContext, String line)
{
String[] tokens = line.split(" ");
if (tokens.length >= 1)
{
return getEnabledShellCommands(shellContext).get(tokens[0]);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public Set<String> getAllCommandNames()
ShellContextImpl newShellContext = newShellContext();
try
{
return commandManager.getEnabledShellCommands(newShellContext).keySet();
return commandManager.getAllCommandNames(newShellContext);
}
finally
{
Expand Down

0 comments on commit 4cf9b79

Please sign in to comment.