Console: Consolidate hash comparators

This commit is contained in:
zzz
2024-08-31 16:03:11 +00:00
parent 8955397bc0
commit 43d68d45ad
7 changed files with 82 additions and 26 deletions

View File

@ -11,7 +11,6 @@ package net.i2p.router.web.helpers;
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
@ -31,19 +30,10 @@ class BanlistRenderer {
public BanlistRenderer(RouterContext context) {
_context = context;
}
/**
* As of 0.9.29, sorts in true binary order, not base64 string
*/
private static class HashComparator implements Comparator<Hash>, Serializable {
public int compare(Hash l, Hash r) {
return DataHelper.compareTo(l.getData(), r.getData());
}
}
public void renderStatusHTML(Writer out) throws IOException {
StringBuilder buf = new StringBuilder(1024);
Map<Hash, Banlist.Entry> entries = new TreeMap<Hash, Banlist.Entry>(new HashComparator());
Map<Hash, Banlist.Entry> entries = new TreeMap<Hash, Banlist.Entry>(HashComparator.getInstance());
entries.putAll(_context.banlist().getEntries());
buf.append("<h3 id=\"bannedpeers\">").append(_t("Banned Peers"));

View File

@ -0,0 +1,30 @@
package net.i2p.router.web.helpers;
import java.io.Serializable;
import java.util.Comparator;
import net.i2p.data.DataHelper;
import net.i2p.data.Hash;
/**
* Sorts in true binary order, not Base64 string order.
* A-Z a-z 0-9 -~
*
* @since 0.9.64 moved from BanlistRenderer
*/
class HashComparator implements Comparator<Hash>, Serializable {
public static final HashComparator _instance = new HashComparator();
/**
* Thread safe, no state
*/
public static HashComparator getInstance() { return _instance; }
public int compare(Hash l, Hash r) {
return DataHelper.compareTo(l.getData(), r.getData());
}
public static int comp(Hash l, Hash r) {
return DataHelper.compareTo(l.getData(), r.getData());
}
}

View File

@ -92,12 +92,6 @@ class NetDbRenderer {
}
}
private static class RouterInfoComparator implements Comparator<RouterInfo>, Serializable {
public int compare(RouterInfo l, RouterInfo r) {
return l.getIdentity().getHash().toBase64().compareTo(r.getIdentity().getHash().toBase64());
}
}
/**
* One String must be non-null
*
@ -930,7 +924,7 @@ class NetDbRenderer {
boolean showStats = full || shortStats; // this means show the router infos
Hash us = _context.routerHash();
Set<RouterInfo> routers = new TreeSet<RouterInfo>(new RouterInfoComparator());
Set<RouterInfo> routers = new TreeSet<RouterInfo>(RouterInfoComparator.getInstance());
routers.addAll(_context.netDb().getRouters());
int toSkip = pageSize * page;
boolean nextpg = routers.size() > toSkip + pageSize;

View File

@ -549,9 +549,7 @@ public class PeerHelper extends HelperBase {
public int compare(NTCPConnection l, NTCPConnection r) {
if (l == null || r == null)
throw new IllegalArgumentException();
// base64 retains binary ordering
// UM, no it doesn't, but close enough
return l.getRemotePeer().calculateHash().toBase64().compareTo(r.getRemotePeer().calculateHash().toBase64());
return HashComparator.comp(l.getRemotePeer().calculateHash(), r.getRemotePeer().calculateHash());
}
}

View File

@ -46,7 +46,7 @@ class ProfileOrganizerRenderer {
long now = _context.clock().now();
long hideBefore = now - 45*60*1000;
Set<PeerProfile> order = new TreeSet<PeerProfile>(mode == 2 ? new HashComparator() : new ProfileComparator());
Set<PeerProfile> order = new TreeSet<PeerProfile>(mode == 2 ? new ProfComparator() : new ProfileComparator());
int older = 0;
int standard = 0;
for (Hash peer : peers) {
@ -362,7 +362,7 @@ class ProfileOrganizerRenderer {
out.flush();
}
private class ProfileComparator extends HashComparator {
private class ProfileComparator extends ProfComparator {
public int compare(PeerProfile left, PeerProfile right) {
if (_context.profileOrganizer().isFast(left.getPeer())) {
if (_context.profileOrganizer().isFast(right.getPeer())) {
@ -395,9 +395,9 @@ class ProfileOrganizerRenderer {
* As of 0.9.29, sorts in true binary order, not base64 string
* @since 0.9.8
*/
private static class HashComparator implements Comparator<PeerProfile>, Serializable {
private static class ProfComparator implements Comparator<PeerProfile>, Serializable {
public int compare(PeerProfile left, PeerProfile right) {
return DataHelper.compareTo(left.getPeer().getData(), right.getPeer().getData());
return HashComparator.comp(left.getPeer(), right.getPeer());
}
}

View File

@ -0,0 +1,44 @@
package net.i2p.router.web.helpers;
import java.io.Serializable;
import java.util.Comparator;
import net.i2p.data.DataHelper;
import net.i2p.data.Hash;
import net.i2p.data.router.RouterInfo;
/**
* Sorts in true binary order, not Base64 string order.
* A-Z a-z 0-9 -~
*
* @since 0.9.64
*/
class RouterInfoComparator implements Comparator<RouterInfo>, Serializable {
private static final long serialVersionUID = 1;
public static final RouterInfoComparator _instance = new RouterInfoComparator();
/**
* Thread safe, no state
*/
public static RouterInfoComparator getInstance() { return _instance; }
/**
* @param l non-null
* @param r non-null
*/
public int compare(RouterInfo l, RouterInfo r) {
Hash lh = l.getIdentity().getHash();
Hash rh = r.getIdentity().getHash();
return HashComparator.comp(lh, rh);
}
/**
* @param l non-null
* @param r non-null
*/
public static int comp(RouterInfo l, RouterInfo r) {
Hash lh = l.getIdentity().getHash();
Hash rh = r.getIdentity().getHash();
return HashComparator.comp(lh, rh);
}
}

View File

@ -287,7 +287,7 @@ class UDPSorters {
static class PeerComparator implements Comparator<PeerState>, Serializable {
public int compare(PeerState l, PeerState r) {
return DataHelper.compareTo(l.getRemotePeer().getData(), r.getRemotePeer().getData());
return HashComparator.comp(l.getRemotePeer(), r.getRemotePeer());
}
}