From 27d436ce7f87f9d32aaaf123ad7d196b81d08b67 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Fri, 30 Aug 2019 14:44:00 +0300 Subject: [PATCH] Optimized Locale replacement --- .../plan/system/settings/locale/Locale.java | 45 ++++++++++--------- .../plan/system/settings/locale/Message.java | 5 ++- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/settings/locale/Locale.java b/Plan/common/src/main/java/com/djrapitops/plan/system/settings/locale/Locale.java index c9adeab41..9d94a3eef 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/settings/locale/Locale.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/settings/locale/Locale.java @@ -19,16 +19,12 @@ package com.djrapitops.plan.system.settings.locale; import com.djrapitops.plan.system.settings.locale.lang.*; import com.djrapitops.plan.system.storage.file.FileResource; import com.djrapitops.plan.system.storage.file.PlanFiles; +import org.apache.commons.lang3.StringUtils; import java.io.File; import java.io.IOException; import java.io.Serializable; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Objects; -import java.util.function.Function; -import java.util.stream.Collectors; +import java.util.*; /** * Represents loaded language information. @@ -73,6 +69,15 @@ public class Locale extends HashMap { } } + public Optional getNonDefault(Object key) { + Message storedValue = super.get(key); + if (key instanceof Lang && storedValue == null) { + return Optional.empty(); + } else { + return Optional.of(storedValue); + } + } + public String getString(Lang key) { return get(key).toString(); } @@ -99,8 +104,6 @@ public class Locale extends HashMap { return from; } - String replaced = from; - Lang[][] langs = new Lang[][]{ NetworkPageLang.values(), PlayerPageLang.values(), @@ -108,23 +111,21 @@ public class Locale extends HashMap { CommonHtmlLang.values() }; - List> entries = Arrays.stream(langs) - .flatMap(Arrays::stream) - .collect(Collectors.toMap(Function.identity(), this::get)) - .entrySet().stream() + List replace = new ArrayList<>(); + List with = new ArrayList<>(); + + Arrays.stream(langs).flatMap(Arrays::stream) // Longest first so that entries that contain each other don't partially replace. .sorted((one, two) -> Integer.compare( - two.getKey().getIdentifier().length(), - one.getKey().getIdentifier().length() - )).collect(Collectors.toList()); + two.getIdentifier().length(), + one.getIdentifier().length() + )) + .forEach(lang -> getNonDefault(lang).ifPresent(replacement -> { + replace.add(lang.getDefault()); + with.add(replacement.toString()); + })); - for (Entry entry : entries) { - String defaultValue = entry.getKey().getDefault(); - String replacement = entry.getValue().toString(); - - replaced = replaced.replace(defaultValue, replacement); - } - return replaced; + return StringUtils.replaceEach(from, replace.toArray(new String[0]), with.toArray(new String[0])); } @Override diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/settings/locale/Message.java b/Plan/common/src/main/java/com/djrapitops/plan/system/settings/locale/Message.java index 14a98555c..0984afceb 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/settings/locale/Message.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/settings/locale/Message.java @@ -17,6 +17,7 @@ package com.djrapitops.plan.system.settings.locale; import com.djrapitops.plugin.utilities.Verify; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringSubstitutor; import java.io.Serializable; @@ -51,11 +52,11 @@ public class Message { } public String[] toArray() { - return content.split("\\\\"); + return StringUtils.split(content, '\\'); } public String[] toArray(Serializable... p) { - return parse(p).split("\\\\"); + return parse(p).split(content, '\\'); } @Override