Skip to content
Permalink
Browse files

Improved: Rewrite ‘Start#determineCommandType’

(OFBIZ-11137)

Rename it to ‘Start.CommandType#valueOf’ and use a set to determine
the type of command instead of repetitive stream iterations.

Assuming ‘Set#contains’ is a constant operation, The algorithmic
complexity in worth case has dropped from O(n*m) to O(n+m) where n is
the number of command types and m is the number of startup commands.
In this particular case where n and m are small, the theorical gain
is not significative in term of actual performance.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1862664 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jul 6, 2019
1 parent 2810af2 commit 6925f7961bd9271d0e37d7e5105489bae2ae401d
Showing with 26 additions and 17 deletions.
  1. +26 −17 framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java
@@ -20,7 +20,9 @@

import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
* OFBiz startup class.
@@ -66,7 +68,7 @@ public static void main(String[] args) {
System.exit(1);
}

CommandType commandType = determineCommandType(ofbizCommands);
CommandType commandType = CommandType.valueOf(ofbizCommands);
if(!commandType.equals(CommandType.HELP)) {
instance.config = StartupControlPanel.init(ofbizCommands);
}
@@ -123,22 +125,29 @@ public String toString() {
}
}

private static CommandType determineCommandType(List<StartupCommand> ofbizCommands) {
if (ofbizCommands.stream().anyMatch(
command -> command.getName().equals(StartupCommandUtil.StartupOption.HELP.getName()))) {
return CommandType.HELP;
} else if (ofbizCommands.stream().anyMatch(
command -> command.getName().equals(StartupCommandUtil.StartupOption.STATUS.getName()))) {
return CommandType.STATUS;
} else if (ofbizCommands.stream().anyMatch(
command -> command.getName().equals(StartupCommandUtil.StartupOption.SHUTDOWN.getName()))) {
return CommandType.SHUTDOWN;
} else {
return CommandType.START;
}
}

/**
* The type of command that allow dispatching to various startup behavior.
*/
private enum CommandType {
HELP, STATUS, SHUTDOWN, START
HELP, STATUS, SHUTDOWN, START;

/**
* Determines the type of command from a list of command-line commands
*
* @param ofbizCommands the list of parsed command-line arguments which cannot be {@code null}
* @return the corresponding command type.
*/
static CommandType valueOf(List<StartupCommand> ofbizCommands) {
Set<String> commandNames = ofbizCommands.stream().map(StartupCommand::getName).collect(Collectors.toSet());
if (commandNames.contains(StartupCommandUtil.StartupOption.HELP.getName())) {
return CommandType.HELP;
} else if (commandNames.contains(StartupCommandUtil.StartupOption.STATUS.getName())) {
return CommandType.STATUS;
} else if (commandNames.contains(StartupCommandUtil.StartupOption.SHUTDOWN.getName())) {
return CommandType.SHUTDOWN;
} else {
return CommandType.START;
}
}
}
}

0 comments on commit 6925f79

Please sign in to comment.
You can’t perform that action at this time.