import javax.annotation.Nullable;
import net.pterodactylus.sone.freenet.wot.Identity;
-import net.pterodactylus.sone.template.SoneAccessor;
import freenet.keys.FreenetURI;
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>() {
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;
/** {@inheritDoc} */
@Override
public int compareTo(Sone sone) {
- return NICE_NAME_COMPARATOR.compare(this, sone);
+ return niceNameComparator().compare(this, sone);
}
//
package net.pterodactylus.sone.fcp;
+import static net.pterodactylus.sone.data.SoneKt.*;
import static net.pterodactylus.sone.fcp.AbstractSoneCommandKt.encodeSones;
import java.util.ArrayList;
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;
/**
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."));
}
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;
}
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) {
--- /dev/null
+/**
+ * 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)
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)
.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 ->
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}" }
}