From 7be5373f355b3184e02a61b39238a89be177c040 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 16 Feb 2020 13:27:47 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20nice=20name=20comparato?= =?utf8?q?r=20with=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/data/Sone.java | 15 ----------- .../net/pterodactylus/sone/data/impl/SoneImpl.java | 3 ++- .../pterodactylus/sone/fcp/GetSonesCommand.java | 6 +++-- .../sone/template/CollectionAccessor.java | 7 ++--- .../kotlin/net/pterodactylus/sone/data/Sone.kt | 31 ++++++++++++++++++++++ .../pterodactylus/sone/web/pages/CreateSonePage.kt | 2 +- .../pterodactylus/sone/web/pages/KnownSonesPage.kt | 2 +- .../net/pterodactylus/sone/web/pages/LoginPage.kt | 2 +- 8 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/data/Sone.kt diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index 90d0355..1143501 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -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 { downloading, } - /** comparator that sorts Sones by their nice name. */ - public static final Comparator NICE_NAME_COMPARATOR = new Comparator() { - - @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 LAST_ACTIVITY_COMPARATOR = new Comparator() { diff --git a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java index ceb05b2..093b038 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java @@ -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); } // diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetSonesCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetSonesCommand.java index 83167cd..eedf120 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetSonesCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetSonesCommand.java @@ -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. 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.")); } diff --git a/src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java b/src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java index f170701..f960b08 100644 --- a/src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java @@ -17,12 +17,13 @@ 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 index 0000000..eb67dff --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/data/Sone.kt @@ -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 . + */ + +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 = + comparing(SoneAccessor::getNiceName, caseInsensitiveCompare).thenComparing(Sone::id) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateSonePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateSonePage.kt index 0b690da..959788b 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateSonePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateSonePage.kt @@ -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) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt index ae0d7d1..260f35b 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt @@ -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 -> diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt index 5be34ac..6244325 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt @@ -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}" } } -- 2.7.4