From: "Enrico Weigelt, metux IT consult" <enrico.weig...@gr13.net>

2DO: check whether we can cache the result of getValidDifficulties(),
or even create it on class initialization. (depends on whether locales
are already loaded at that time or can change later)
---
 src/net/sf/freecol/FreeCol.java              | 33 ++++++++++++++++++----------
 src/net/sf/freecol/common/i18n/Messages.java | 10 ---------
 2 files changed, 21 insertions(+), 22 deletions(-)

diff --git a/src/net/sf/freecol/FreeCol.java b/src/net/sf/freecol/FreeCol.java
index b746b8ccd9a..39a5d4565cf 100644
--- a/src/net/sf/freecol/FreeCol.java
+++ b/src/net/sf/freecol/FreeCol.java
@@ -37,7 +37,6 @@ import java.util.jar.Manifest;
 import java.util.logging.Handler;
 import java.util.logging.Level;
 import java.util.logging.Logger;
-import java.util.stream.Collectors;
 import java.util.zip.ZipEntry;
 
 import net.sf.freecol.client.ClientOptions;
@@ -86,9 +85,14 @@ public final class FreeCol {
     /** The FreeCol protocol version number. */
     private static final String FREECOL_PROTOCOL_VERSION = "0.1.6";
 
-    /** The difficulty levels. */
-    private static final String[] DIFFICULTIES = {
-        "veryEasy", "easy", "medium", "hard", "veryHard"
+    /** The difficulty levels w/ prefix -- save CPU cycles and code complexity
+        by not transforming over and over again. **/
+    private static final String[] DIFFICULTIES_PREFIXED = {
+        "model.difficulty.veryEasy",
+        "model.difficulty.easy",
+        "model.difficulty.medium",
+        "model.difficulty.hard",
+        "model.difficulty.veryHard"
     };
 
     /** The extension for FreeCol saved games. */
@@ -1018,11 +1022,14 @@ public final class FreeCol {
      * @return The name of the selected difficulty, or null if none.
      */
     public static String selectDifficulty(String arg) {
-        String difficulty
-            = find(map(DIFFICULTIES, d -> "model.difficulty." + d),
-                   Messages.matchesName(arg));
-        if (difficulty != null) setDifficulty(difficulty);
-        return difficulty;
+        if (arg != null)
+            for (String difficulty : DIFFICULTIES_PREFIXED)
+                if (arg.equals(Messages.getName(difficulty))) {
+                    setDifficulty(difficulty);
+                    return difficulty;
+                }
+
+        return null;
     }
 
     /**
@@ -1050,9 +1057,11 @@ public final class FreeCol {
      * @return The valid difficulty levels, comma separated.
      */
     public static String getValidDifficulties() {
-        return transform(DIFFICULTIES, alwaysTrue(),
-                         d -> Messages.getName("model.difficulty." + d),
-                         Collectors.joining(","));
+        StrCat cat = new StrCat(",");
+        for (String s : DIFFICULTIES_PREFIXED)
+            cat.add(s);
+
+        return cat.toString();
     }
 
     /**
diff --git a/src/net/sf/freecol/common/i18n/Messages.java 
b/src/net/sf/freecol/common/i18n/Messages.java
index e0d5978bea8..857609fa30c 100644
--- a/src/net/sf/freecol/common/i18n/Messages.java
+++ b/src/net/sf/freecol/common/i18n/Messages.java
@@ -468,16 +468,6 @@ public class Messages {
     }
 
     /**
-     * A predicate maker to match by message name.
-     *
-     * @param key The name of the message.
-     * @return A suitable {@code Predicate}.
-     */
-    public static final Predicate<String> matchesName(String key) {
-        return matchKeyEquals(key, (String k) -> Messages.getName(k));
-    }
-
-    /**
      * A predicate maker to match named types.
      *
      * @param key The name of the type.
-- 
2.11.0.rc0.7.gbe5a750


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Freecol-developers mailing list
Freecol-developers@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/freecol-developers

Reply via email to