In a custom Ant task, I have the following constants:
private static final String _REVISION = ".revision";
private static final String _CODEBASE_INFO = ".codebase";
private static final String _DIFF = ".diff";
private static final String _TYPE_GIT = "git";
private static final String _TYPE_SVN = "svn";
private static final String[] _COMMAND_REV_GIT =
{"git", "rev-parse", "HEAD"};
private static final String[] _COMMAND_REV_SVN =
{"svn", "info"};
private static final String[][] _COMMAND_CODEBASEINFO_GIT = {
{"git", "branch", "-v"},
{"git", "remote", "-v"},
{"git", "log", "--max-count=1"},
{"git", "status"}};
private static final String[][] _COMMAND_CODEBASEINFO_SVN = {
_COMMAND_REV_SVN,
{"svn", "status"}};
private static final String[][] _COMMAND_DIFF_GIT = {
{"git", "diff", "HEAD"},
{"git", "diff", "HEAD", "--no-prefix"}};
private static final String[][] _COMMAND_DIFF_SVN = {
{"svn", "diff"}};
private static final Map<String, Object> _COMMANDS;
static {
Map<String, Object> map = new HashMap<>();
map.put(_TYPE_GIT + _REVISION, _COMMAND_REV_GIT);
map.put(_TYPE_SVN + _REVISION, _COMMAND_REV_SVN);
map.put(_TYPE_GIT + _DIFF, _COMMAND_DIFF_GIT);
map.put(_TYPE_SVN + _DIFF, _COMMAND_DIFF_SVN);
map.put(_TYPE_GIT + _CODEBASE_INFO, _COMMAND_CODEBASEINFO_GIT);
map.put(_TYPE_SVN + _CODEBASE_INFO, _COMMAND_CODEBASEINFO_SVN);
_COMMANDS = Collections.unmodifiableMap(map);
}
What the task should do is to run commands according to a given version control type. For example if I give it git
then it will run commands in _COMMAND_REV_GIT
, _COMMAND_CODEBASEINFO_GIT
and _COMMAND_DIFF_GIT
.
The reason I separate them into three constants is that the outputs of them have to be handled in different ways.
The reason I use a map is that I don't want to use a lot of "if" or "switch-case" in my code. In the map there are keys like git.revision
, git.codebase
, etc. If git
is given when running this task, _COMMAND_REV_GIT
will be fetched from the map using the key git.revision
in the method handling running the revision command:
String[] command = _COMMANDS.get(_givenType + _REVISION);
But these constants looks really complicated, and I think the types of version control should be Enum
rather than String
, so is the suffixes indicating the types of the commands. So how should I modify these codes to make it clear, safe and elegant?