class NodeInfoComparator extends Object implements Comparator<NodeInfo>, Serializable
This comparator implements the Kademlia distance metric to determine which of two nodes is closer to a target key (either an info hash or node ID). The distance is calculated using XOR distance, which is the standard metric used in Kademlia-based DHTs.
The XOR distance metric has the important property that it forms a metric space, meaning it satisfies the triangle inequality and other mathematical properties that make it suitable for routing in distributed hash tables.
This comparator is primarily used for:
- Finding the k closest nodes to a target info hash
- Maintaining sorted lists of nodes by distance
- Selecting the best nodes for DHT queries and responses
| Constructor and Description |
|---|
NodeInfoComparator(SHA1Hash h)
Creates a new comparator for measuring distance to the specified target key.
|
| Modifier and Type | Method and Description |
|---|---|
int |
compare(NodeInfo lhs,
NodeInfo rhs)
Compares two nodes based on their XOR distance to the target key.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcomparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongpublic NodeInfoComparator(SHA1Hash h)
h - the target key (info hash or node ID) to compare distances againstpublic int compare(NodeInfo lhs, NodeInfo rhs)
The comparison calculates the XOR distance between each node's ID and the target key, then compares these distances lexicographically. The node with the smaller XOR distance is considered "closer" to the target.
This method implements the standard Kademlia distance metric used throughout the DHT for routing and node selection.
compare in interface Comparator<NodeInfo>lhs - the first node to comparerhs - the second node to compare