Skip to content

Commit

Permalink
Refactor more duplicate methods into abstract base class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew W. Samsonoff committed Mar 15, 2012
1 parent ec60bea commit 28a231c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 69 deletions.
56 changes: 56 additions & 0 deletions src/replicatorg/app/service/AbstractCommandFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// vim:cindent:cino=\:0:et:fenc=utf-8:ff=unix:sw=4:ts=4:

package replicatorg.app.service;

import java.util.Arrays;
import java.util.List;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import replicatorg.app.Base;

public abstract class AbstractCommandFactory implements CommandFactory
{
public Command createCommand(final List<String> arguments)
throws ParseException, MissingArgumentException,
ExtraArgumentsException
{
final String[] array = arguments.toArray(new String[0]);
final CommandLineParser commandLineParser = new GnuParser();
final Options options = createOptions();
final CommandLine commandLine = commandLineParser.parse(
options, array, false);
final Command command = createCommand(commandLine);
return command;
}

protected abstract Command createCommand(CommandLine commandLine)
throws MissingArgumentException, ExtraArgumentsException;

protected abstract Options createOptions();

protected List<String> getArgumentsAsList(final CommandLine commandLine)
{
final String[] array = commandLine.getArgs();
final List<String> list = Arrays.asList(array);
return list;
}

protected String handleBusName(final CommandLine commandLine)
{
final String busName;
if (commandLine.hasOption("bus-name"))
{
busName = commandLine.getOptionValue("bus-name");
}
else
{
busName = null;
}
return busName;
}
}
37 changes: 6 additions & 31 deletions src/replicatorg/app/service/PrinterCommandFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,18 @@

import replicatorg.app.Base;

public class PrinterCommandFactory implements CommandFactory
public class PrinterCommandFactory extends AbstractCommandFactory
{
public boolean isMatch(final String commandName)
{
final boolean result = "printer".equals(commandName);
return result;
}

public Command createCommand(final List<String> arguments)
throws ParseException, MissingArgumentException,
ExtraArgumentsException
@Override
protected Command createCommand(final CommandLine commandLine)
throws MissingArgumentException, ExtraArgumentsException
{
final String[] array = arguments.toArray(new String[0]);
final CommandLineParser commandLineParser = new GnuParser();
final Options options = createOptions();
final CommandLine commandLine = commandLineParser.parse(
options, array, false);
final List<String> extraArguments = getArgumentsAsList(commandLine);
if (0 != extraArguments.size())
{
Expand All @@ -46,7 +41,8 @@ public Command createCommand(final List<String> arguments)
}
}

private Options createOptions()
@Override
protected Options createOptions()
{
final Options options = new Options();
options.addOption(OptionBuilder
Expand All @@ -70,13 +66,6 @@ private Options createOptions()
return options;
}

private List<String> getArgumentsAsList(final CommandLine commandLine)
{
final String[] array = commandLine.getArgs();
final List<String> list = Arrays.asList(array);
return list;
}

private String handleMachineName(final CommandLine commandLine)
{
final String machineName;
Expand Down Expand Up @@ -104,18 +93,4 @@ private String handlePort(final CommandLine commandLine)
}
return port;
}

private String handleBusName(final CommandLine commandLine)
{
final String busName;
if (commandLine.hasOption("bus-name"))
{
busName = commandLine.getOptionValue("bus-name");
}
else
{
busName = null;
}
return busName;
}
}
40 changes: 2 additions & 38 deletions src/replicatorg/app/service/RemoteCommandFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,9 @@
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public abstract class RemoteCommandFactory implements CommandFactory
public abstract class RemoteCommandFactory extends AbstractCommandFactory
{
public Command createCommand(final List<String> arguments)
throws ParseException, MissingArgumentException,
ExtraArgumentsException
{
final String[] array = arguments.toArray(new String[0]);
final CommandLineParser commandLineParser = new GnuParser();
final Options options = createOptions();
final CommandLine commandLine = commandLineParser.parse(
options, array, false);
final Command command = createCommand(commandLine);
return command;
}

protected abstract Command createCommand(CommandLine commandLine)
throws MissingArgumentException, ExtraArgumentsException;

@Override
protected Options createOptions()
{
final Options options = new Options();
Expand All @@ -41,25 +26,4 @@ protected Options createOptions()
.create());
return options;
}

protected List<String> getArgumentsAsList(final CommandLine commandLine)
{
final String[] array = commandLine.getArgs();
final List<String> list = Arrays.asList(array);
return list;
}

protected String handleBusName(final CommandLine commandLine)
{
final String busName;
if (commandLine.hasOption("bus-name"))
{
busName = commandLine.getOptionValue("bus-name");
}
else
{
busName = null;
}
return busName;
}
}

0 comments on commit 28a231c

Please sign in to comment.