From 64984c6ea8e41bc7acdeaba6673eb90f79852544 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 30 Jul 2018 09:14:13 +0300 Subject: [PATCH] Fixed some inconsistencies in Locale implementation --- .../djrapitops/plan/system/locale/Locale.java | 25 ++++++--- .../plan/system/locale/LocaleFileWriter.java | 53 ++++++++++--------- .../system/locale/LocaleFileWriterTest.java | 34 ++++++++++++ 3 files changed, 78 insertions(+), 34 deletions(-) create mode 100644 Plan/src/test/java/com/djrapitops/plan/system/locale/LocaleFileWriterTest.java diff --git a/Plan/src/main/java/com/djrapitops/plan/system/locale/Locale.java b/Plan/src/main/java/com/djrapitops/plan/system/locale/Locale.java index 8a72998a0..12ea0e423 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/locale/Locale.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/locale/Locale.java @@ -8,13 +8,14 @@ import java.io.File; import java.io.IOException; import java.io.Serializable; import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; /** * Represents loaded language information. * * @author Rsl1122 */ -// TODO Turn into TreeMap that sorts longest first public class Locale extends HashMap { public Locale() { @@ -42,10 +43,11 @@ public class Locale extends HashMap { @Override public Message get(Object key) { - if (key instanceof Lang) { - return getOrDefault(key, new Message(((Lang) key).getDefault())); + Message storedValue = super.get(key); + if (key instanceof Lang && storedValue == null) { + return new Message(((Lang) key).getDefault()); } else { - return super.get(key); + return storedValue; } } @@ -69,13 +71,20 @@ public class Locale extends HashMap { if (isEmpty()) { return from; } - for (Entry entry : entrySet()) { + + String replaced = from; + + // Longest first so that entries that contain each other don't partially replace. + List> entries = entrySet().stream().sorted( + (one, two) -> Integer.compare(two.getKey().getIdentifier().length(), one.getKey().getIdentifier().length()) + ).collect(Collectors.toList()); + + for (Entry entry : entries) { String defaultValue = entry.getKey().getDefault(); String replacement = entry.getValue().toString(); - from = from.replace(defaultValue, replacement); + replaced = replaced.replace(defaultValue, replacement); } - - return from; + return replaced; } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/locale/LocaleFileWriter.java b/Plan/src/main/java/com/djrapitops/plan/system/locale/LocaleFileWriter.java index a6c613942..8b08ff0a5 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/locale/LocaleFileWriter.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/locale/LocaleFileWriter.java @@ -1,18 +1,14 @@ package com.djrapitops.plan.system.locale; import com.djrapitops.plan.system.locale.lang.Lang; -import com.djrapitops.plan.system.settings.Settings; -import com.djrapitops.plan.system.settings.config.ConfigSystem; import com.djrapitops.plan.utilities.comparators.LocaleEntryComparator; import com.djrapitops.plan.utilities.comparators.StringLengthComparator; -import com.djrapitops.plugin.api.config.Config; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.List; -import java.util.Optional; import java.util.stream.Collectors; /** @@ -22,43 +18,48 @@ import java.util.stream.Collectors; */ public class LocaleFileWriter { - private final Locale locale; + private Locale locale; public LocaleFileWriter(Locale locale) { this.locale = locale; } public void writeToFile(File file) throws IOException { - // Find longest identifier - Optional key = LocaleSystem.getIdentifiers().keySet().stream() - .min(new StringLengthComparator()); - if (!key.isPresent()) { - throw new IllegalStateException("LocaleSystem defines no Identifiers."); - } + // Find longest identifier length for spacing + int length = LocaleSystem.getIdentifiers().keySet().stream() + .min(new StringLengthComparator()) + .map(String::length).orElse(0) + 2; - for (Lang lang : LocaleSystem.getIdentifiers().values()) { - if (!locale.containsKey(lang)) { - locale.put(lang, new Message(lang.getDefault())); - } - } + addMissingLang(); - int length = key.get().length() + 2; - List lines = locale.entrySet().stream() + List lines = createLines(length); + + write(file, lines); + } + + private void write(File file, List lines) throws IOException { + if (!file.exists()) { + Files.createFile(file.toPath()); + } + Files.write(file.toPath(), lines, StandardCharsets.UTF_8); + } + + private List createLines(int length) { + return locale.entrySet().stream() .sorted(new LocaleEntryComparator()) .map(entry -> { String value = entry.getValue() != null ? entry.getValue().toString() : entry.getKey().getDefault(); return getSpacedIdentifier(entry.getKey().getIdentifier(), length) + "|| " + value; }) .collect(Collectors.toList()); - if (!file.exists()) { - Files.createFile(file.toPath()); + } + + private void addMissingLang() { + for (Lang lang : LocaleSystem.getIdentifiers().values()) { + if (!locale.containsKey(lang)) { + locale.put(lang, new Message(lang.getDefault())); + } } - - Files.write(file.toPath(), lines, StandardCharsets.UTF_8); - - Config config = ConfigSystem.getConfig(); - config.set(Settings.WRITE_NEW_LOCALE.getPath(), false); - config.save(); } private String getSpacedIdentifier(String identifier, int length) { diff --git a/Plan/src/test/java/com/djrapitops/plan/system/locale/LocaleFileWriterTest.java b/Plan/src/test/java/com/djrapitops/plan/system/locale/LocaleFileWriterTest.java new file mode 100644 index 000000000..8ce7cf4c6 --- /dev/null +++ b/Plan/src/test/java/com/djrapitops/plan/system/locale/LocaleFileWriterTest.java @@ -0,0 +1,34 @@ +package com.djrapitops.plan.system.locale; + +import com.djrapitops.plan.utilities.file.FileUtil; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; + +import static org.junit.Assert.assertEquals; + +/** + * Test class for {@link LocaleFileWriter}. + * + * @author Rsl1122 + */ +public class LocaleFileWriterTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test + public void writesAllIdentifiers() throws IOException { + File file = temporaryFolder.newFile(); + new LocaleFileWriter(new Locale()).writeToFile(file); + + long expected = LocaleSystem.getIdentifiers().size(); + int result = FileUtil.lines(file, Charset.forName("UTF-8")).size(); + assertEquals(expected, result); + } + +} \ No newline at end of file