svn commit: r1862664 - /ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1862664 - /ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java

mthl
Author: mthl
Date: Sat Jul  6 14:33:19 2019
New Revision: 1862664

URL: http://svn.apache.org/viewvc?rev=1862664&view=rev
Log:
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.

Modified:
    ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java

Modified: ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java?rev=1862664&r1=1862663&r2=1862664&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java Sat Jul  6 14:33:19 2019
@@ -20,7 +20,9 @@ package org.apache.ofbiz.base.start;
 
 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 final class Start {
             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 final class Start {
         }
     }
 
-    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;
+            }
+        }
     }
 }