🎨 Replace L10nFilter with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 7 Aug 2019 19:32:11 +0000 (21:32 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 7 Aug 2019 19:32:11 +0000 (21:32 +0200)
src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java [deleted file]
src/main/kotlin/net/pterodactylus/sone/freenet/L10nFilter.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt

diff --git a/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java b/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java
deleted file mode 100644 (file)
index d667811..0000000
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * Sone - L10nFilter.java - Copyright Â© 2010–2019 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.freenet;
-
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-
-import javax.annotation.Nonnull;
-
-import net.pterodactylus.util.template.Filter;
-import net.pterodactylus.util.template.TemplateContext;
-
-import freenet.l10n.BaseL10n;
-
-/**
- * {@link Filter} implementation replaces {@link String} values with their
- * translated equivalents.
- */
-public class L10nFilter implements Filter {
-
-       private final BaseL10n l10n;
-
-       public L10nFilter(BaseL10n l10n) {
-               this.l10n = l10n;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public String format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
-               List<Object> parameterValues = getParameters(data, parameters);
-               String text = getText(data);
-               if (parameterValues.isEmpty()) {
-                       return l10n.getString(text);
-               }
-               return new MessageFormat(l10n.getString(text), new Locale(l10n.getSelectedLanguage().shortCode)).format(parameterValues.toArray());
-       }
-
-       @Nonnull
-       private String getText(Object data) {
-               return (data instanceof L10nText) ? ((L10nText) data).getText() : String.valueOf(data);
-       }
-
-       @Nonnull
-       private List<Object> getParameters(Object data, Map<String, Object> parameters) {
-               if (data instanceof L10nText) {
-                       return ((L10nText) data).getParameters();
-               }
-               List<Object> parameterValues = new ArrayList<>();
-               int parameterIndex = 0;
-               while (parameters.containsKey(String.valueOf(parameterIndex))) {
-                       Object value = parameters.get(String.valueOf(parameterIndex));
-                       parameterValues.add(value);
-                       ++parameterIndex;
-               }
-               return parameterValues;
-       }
-
-}
diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/L10nFilter.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/L10nFilter.kt
new file mode 100644 (file)
index 0000000..4b0d90f
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Sone - L10nFilter.java - Copyright Â© 2010–2019 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet
+
+import freenet.l10n.*
+import net.pterodactylus.util.template.*
+import java.text.*
+import java.util.*
+
+/**
+ * [Filter] implementation replaces [String] values with their
+ * translated equivalents.
+ */
+class L10nFilter(private val l10n: BaseL10n) : Filter {
+
+       override fun format(templateContext: TemplateContext?, data: Any?, parameters: Map<String, Any>?): String {
+               val parameterValues = getParameters(data, parameters)
+               val text = getText(data)
+               return if (parameterValues.isEmpty()) {
+                       l10n.getString(text)
+               } else
+                       MessageFormat(l10n.getString(text), Locale(l10n.selectedLanguage.shortCode)).format(parameterValues.toTypedArray())
+       }
+
+       private fun getText(data: Any?) = (data as? L10nText)?.text ?: data.toString()
+
+       private fun getParameters(data: Any?, parameters: Map<String, Any>?) =
+                       if (data is L10nText)
+                               data.parameters
+                       else
+                               (parameters ?: emptyMap()).let { params ->
+                                       generateSequence(0) { it + 1 }
+                                                       .takeWhile { it.toString() in params }
+                                                       .map { params[it.toString()] }
+                                                       .toList()
+                               }
+
+}
index 66b1ab1..7024171 100644 (file)
@@ -1,15 +1,12 @@
 package net.pterodactylus.sone.freenet
 
-import freenet.l10n.BaseL10n
-import freenet.l10n.BaseL10n.LANGUAGE.ENGLISH
-import net.pterodactylus.sone.test.mock
-import net.pterodactylus.sone.test.whenever
-import net.pterodactylus.util.template.TemplateContext
-import org.hamcrest.MatcherAssert.assertThat
-import org.hamcrest.Matchers.equalTo
-import org.junit.Before
-import org.junit.Test
-import org.mockito.ArgumentMatchers.anyString
+import freenet.l10n.*
+import freenet.l10n.BaseL10n.LANGUAGE.*
+import net.pterodactylus.sone.test.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import org.junit.*
+import org.mockito.ArgumentMatchers.*
 
 /**
  * Unit test for [L10nFilter].
@@ -18,7 +15,6 @@ class L10nFilterTest {
 
        private val l10n = mock<BaseL10n>()
        private val filter = L10nFilter(l10n)
-       private val templateContext = mock<TemplateContext>()
        private val translations = mutableMapOf<String, String>()
 
        @Before
@@ -30,31 +26,31 @@ class L10nFilterTest {
        @Test
        fun `translation without parameters returns translated string`() {
                translations["data"] = "translated data"
-               assertThat(filter.format(templateContext, "data", emptyMap()), equalTo("translated data"))
+               assertThat(filter.format(null, "data", emptyMap()), equalTo("translated data"))
        }
 
        @Test
        fun `translation with parameters returned translated string`() {
                translations["data"] = "translated {0,number} {1}"
-               assertThat(filter.format(templateContext, "data", mapOf("0" to 4.5, "1" to "data")), equalTo("translated 4.5 data"))
+               assertThat(filter.format(null, "data", mapOf("0" to 4.5, "1" to "data")), equalTo("translated 4.5 data"))
        }
 
        @Test
        fun `filter processes l10n text without parameters correctly`() {
                translations["data"] = "translated data"
-               assertThat(filter.format(templateContext, L10nText("data"), emptyMap()), equalTo("translated data"))
+               assertThat(filter.format(null, L10nText("data"), emptyMap()), equalTo("translated data"))
        }
 
        @Test
        fun `filter processes l10n text with parameters correctly`() {
                translations["data"] = "translated {0,number} {1}"
-               assertThat(filter.format(templateContext, L10nText("data", listOf(4.5, "data")), emptyMap()), equalTo("translated 4.5 data"))
+               assertThat(filter.format(null, L10nText("data", listOf(4.5, "data")), emptyMap()), equalTo("translated 4.5 data"))
        }
 
        @Test
        fun `filter does not replace values if there are no parameters`() {
                translations["data"] = "{link}"
-               assertThat(filter.format(templateContext, "data", emptyMap()), equalTo("{link}"))
+               assertThat(filter.format(null, "data", emptyMap()), equalTo("{link}"))
        }
 
 }