🎨 Replace nice name comparator with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 16 Feb 2020 12:27:47 +0000 (13:27 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 16 Feb 2020 19:48:26 +0000 (20:48 +0100)
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java
src/main/java/net/pterodactylus/sone/fcp/GetSonesCommand.java
src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java
src/main/kotlin/net/pterodactylus/sone/data/Sone.kt [new file with mode: 0644]
src/main/kotlin/net/pterodactylus/sone/web/pages/CreateSonePage.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt

index 90d0355..1143501 100644 (file)
@@ -32,7 +32,6 @@ import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 import net.pterodactylus.sone.freenet.wot.Identity;
-import net.pterodactylus.sone.template.SoneAccessor;
 
 import freenet.keys.FreenetURI;
 
@@ -63,20 +62,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
                downloading,
        }
 
-       /** comparator that sorts Sones by their nice name. */
-       public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
-
-               @Override
-               public int compare(Sone leftSone, Sone rightSone) {
-                       int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
-                       if (diff != 0) {
-                               return diff;
-                       }
-                       return leftSone.getId().compareToIgnoreCase(rightSone.getId());
-               }
-
-       };
-
        /** Comparator that sorts Sones by last activity (least recent active first). */
        public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
 
index ceb05b2..093b038 100644 (file)
@@ -21,6 +21,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
 import static java.lang.String.format;
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static java.util.logging.Logger.getLogger;
+import static net.pterodactylus.sone.data.SoneKt.*;
 
 import java.net.MalformedURLException;
 import java.util.ArrayList;
@@ -665,7 +666,7 @@ public class SoneImpl implements Sone {
        /** {@inheritDoc} */
        @Override
        public int compareTo(Sone sone) {
-               return NICE_NAME_COMPARATOR.compare(this, sone);
+               return niceNameComparator().compare(this, sone);
        }
 
        //
index 83167cd..eedf120 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.fcp;
 
+import static net.pterodactylus.sone.data.SoneKt.*;
 import static net.pterodactylus.sone.fcp.AbstractSoneCommandKt.encodeSones;
 
 import java.util.ArrayList;
@@ -24,7 +25,8 @@ import java.util.Collections;
 import java.util.List;
 
 import net.pterodactylus.sone.core.Core;
-import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.data.*;
+
 import freenet.support.SimpleFieldSet;
 
 /**
@@ -53,7 +55,7 @@ public class GetSonesCommand extends AbstractSoneCommand {
                if (sones.size() < startSone) {
                        return new Response("Sones", encodeSones(Collections.<Sone> emptyList(), "Sones."));
                }
-               Collections.sort(sones, Sone.NICE_NAME_COMPARATOR);
+               sones.sort(niceNameComparator());
                return new Response("Sones", encodeSones(sones.subList(startSone, (maxSones == -1) ? sones.size() : Math.min(startSone + maxSones, sones.size())), "Sones."));
        }
 
index f170701..f960b08 100644 (file)
 
 package net.pterodactylus.sone.template;
 
+import static net.pterodactylus.sone.data.SoneKt.*;
+
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
 
-import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.data.*;
 import net.pterodactylus.util.template.Accessor;
 import net.pterodactylus.util.template.ReflectionAccessor;
 import net.pterodactylus.util.template.TemplateContext;
@@ -52,7 +53,7 @@ public class CollectionAccessor extends ReflectionAccessor {
                                }
                                sones.add((Sone) sone);
                        }
-                       Collections.sort(sones, Sone.NICE_NAME_COMPARATOR);
+                       sones.sort(niceNameComparator());
                        StringBuilder soneNames = new StringBuilder();
                        for (Sone sone : sones) {
                                if (soneNames.length() > 0) {
diff --git a/src/main/kotlin/net/pterodactylus/sone/data/Sone.kt b/src/main/kotlin/net/pterodactylus/sone/data/Sone.kt
new file mode 100644 (file)
index 0000000..eb67dff
--- /dev/null
@@ -0,0 +1,31 @@
+/**
+ * Sone - Sone.kt - Copyright Â© 2020 David â€˜Bombe’ 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.data
+
+import net.pterodactylus.sone.template.*
+import java.util.Comparator.*
+
+private val caseInsensitiveCompare = { left: String, right: String -> left.compareTo(right, true) }
+
+/**
+ * Comparator that sorts Sones by their [nice name][SoneAccessor.getNiceName]
+ * and, failing that, by [ID][Sone.id].
+ */
+@get:JvmName("niceNameComparator") // TODO: remove once Sone is 100% Kotlin
+val niceNameComparator: Comparator<Sone> =
+               comparing(SoneAccessor::getNiceName, caseInsensitiveCompare).thenComparing(Sone::id)
index 0b690da..959788b 100644 (file)
@@ -21,7 +21,7 @@ class CreateSonePage @Inject constructor(webInterface: WebInterface, loaders: Lo
        private val logger = Logger.getLogger(CreateSonePage::class.java.name)
 
        override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
-               templateContext["sones"] = soneRequest.core.localSones.sortedWith(Sone.NICE_NAME_COMPARATOR)
+               templateContext["sones"] = soneRequest.core.localSones.sortedWith(niceNameComparator)
                templateContext["identitiesWithoutSone"] = soneRequest.core.identityManager.allOwnIdentities.filterNot { "Sone" in it.contexts }.sortedBy { "${it.nickname}@${it.id}".toLowerCase() }
                if (soneRequest.isPOST) {
                        val identity = soneRequest.httpRequest.getPartAsStringFailsafe("identity", 43)
index ae0d7d1..260f35b 100644 (file)
@@ -29,7 +29,7 @@ class KnownSonesPage @Inject constructor(webInterface: WebInterface, loaders: Lo
                                        .sortedWith(
                                                        when (soneRequest.parameters["sort"]) {
                                                                "images" -> Sone.IMAGE_COUNT_COMPARATOR
-                                                               "name" -> Sone.NICE_NAME_COMPARATOR.reversed()
+                                                               "name" -> niceNameComparator.reversed()
                                                                "posts" -> Sone.POST_COUNT_COMPARATOR
                                                                else -> Sone.LAST_ACTIVITY_COMPARATOR
                                                        }.let { comparator ->
index 5be34ac..6244325 100644 (file)
@@ -26,7 +26,7 @@ class LoginPage @Inject constructor(webInterface: WebInterface, loaders: Loaders
                                redirectTo(target)
                        }
                }
-               templateContext["sones"] = soneRequest.core.localSones.sortedWith(Sone.NICE_NAME_COMPARATOR)
+               templateContext["sones"] = soneRequest.core.localSones.sortedWith(niceNameComparator)
                templateContext["identitiesWithoutSone"] = soneRequest.core.identityManager.allOwnIdentities.filterNot { "Sone" in it.contexts }.sortedBy { "${it.nickname}@${it.id}" }
        }