org.klomp.snark.dht

## Class NodeInfoComparator

- java.lang.Object
- - org.klomp.snark.dht.NodeInfoComparator

- All Implemented Interfaces:
    Serializable, Comparator<NodeInfo>

      class NodeInfoComparator
      extends Object
      implements Comparator<NodeInfo>, Serializable

  Comparator for finding nodes closest to a target key in the DHT keyspace.

  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 Summary

    Constructors
    Constructor and Description
    `NodeInfoComparator(SHA1Hash h)` Creates a new comparator for measuring distance to the specified target key.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `compare(NodeInfo lhs, NodeInfo rhs)` Compares two nodes based on their XOR distance to the target key.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface java.util.Comparator

      `comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong`

- - ### Constructor Detail

    - #### NodeInfoComparator

          public NodeInfoComparator(SHA1Hash h)

      Creates a new comparator for measuring distance to the specified target key.
      Parameters:
        `h` - the target key (info hash or node ID) to compare distances against

  - ### Method Detail

    - #### compare

          public int compare(NodeInfo lhs,
                             NodeInfo rhs)

      Compares two nodes based on their XOR distance to the target key.

      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.

      Specified by:
        `compare` in interface `Comparator<NodeInfo>`
      Parameters:
        `lhs` - the first node to compare
        `rhs` - the second node to compare
      Returns:
        negative value if lhs is closer to target, positive if rhs is closer, 0 if equal distance