Skip to content

Commit

Permalink
CommandFactoryImpl is now using JDK 8's features
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jan 12, 2016
1 parent f09b5ee commit 7451e8f
Showing 1 changed file with 10 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -87,7 +88,7 @@ public String getCommandName(UIContext context, UICommand cmd)
name = metadata.getName();
if (!context.getProvider().isGUI())
{
name = shellifyName(name);
name = Commands.shellifyCommandName(name);
}
}
catch (Exception e)
Expand Down Expand Up @@ -135,7 +136,8 @@ private UICommand findCommand(Iterable<UICommand> commands, UIContext context, S
{
provider.setGUI(false);
String commandName = getCommandName(delegatingContext, cmd);
if (Strings.compare(name, commandName) || Strings.compare(name, shellifyName(commandName)))
if (Strings.compare(name, commandName)
|| Strings.compare(name, Commands.shellifyCommandName(commandName)))
{
return cmd;
}
Expand All @@ -144,7 +146,8 @@ private UICommand findCommand(Iterable<UICommand> commands, UIContext context, S
{
provider.setGUI(true);
String commandName = getCommandName(delegatingContext, cmd);
if (Strings.compare(name, commandName) || Strings.compare(name, shellifyName(commandName)))
if (Strings.compare(name, commandName)
|| Strings.compare(name, Commands.shellifyCommandName(commandName)))
{
return cmd;
}
Expand All @@ -160,33 +163,19 @@ private Iterable<UICommand> getCachedCommands()
{
version = registry.getVersion();
cache.clear();
getCommands(new Operation()
{
@Override
public void execute(UICommand command)
{
cache.add(command);
}
});
getCommands((command) -> cache.add(command));
}
return cache;
}

private Iterable<UICommand> getCommandsFromSource()
{
final Set<UICommand> result = Sets.getConcurrentSet();
getCommands(new Operation()
{
@Override
public void execute(UICommand command)
{
result.add(command);
}
});
getCommands((command) -> result.add(command));
return result;
}

private void getCommands(Operation operation)
private void getCommands(Consumer<UICommand> operation)
{
Imported<CommandProvider> instances = registry.getServices(CommandProvider.class);
for (CommandProvider provider : instances)
Expand All @@ -200,7 +189,7 @@ private void getCommands(Operation operation)
UICommand command = iterator.next();
if (!(command instanceof UIWizardStep))
{
operation.execute(command);
operation.accept(command);
}
}
catch (Exception e)
Expand All @@ -212,24 +201,6 @@ private void getCommands(Operation operation)
}
}

/**
* "Shellifies" a name (that is, makes the name shell-friendly) by replacing spaces with "-" and removing colons
*/
private static String shellifyName(String name)
{
return name != null ? name.trim().toLowerCase().replaceAll("\\W+", "-").replaceAll("\\:", "") : null;
}

/**
*
* Strategy for operation to be performed when iterating through Commands
*
*/
interface Operation
{
void execute(UICommand command);
}

/**
* {@link UIProvider} implementation for querying the command name in GUI and non-GUI modes, which is a common use
* case for defining different names between GUI and CLI environments.
Expand Down

0 comments on commit 7451e8f

Please sign in to comment.