java.lang.Object
net.i2p.router.tunnel.pool.TunnelPool.LeaseComparator
All Implemented Interfaces:
Serializable, Comparator<Lease>
Enclosing class:
TunnelPool

static class TunnelPool.LeaseComparatorextends Object implements Comparator<Lease>, Serializable
Always build a LeaseSet with Leases in sorted order, so that LeaseSet.equals(Object) and lease-by-lease equals() always work. The sort method is arbitrary, as far as the equals() tests are concerned, but we use latest expiration first, since we need to sort them by that anyway.
  • Constructor Details

    • LeaseComparator

      LeaseComparator()
      A stateless comparator: it reads nothing but the two leases, so a bare instance behaves exactly like any other.
  • Method Details

    • compare

      public int compare(Lease l, Lease r)
      Order leases by end time, breaking ties on tunnel identity.

      Ordering by end time is the point of this comparator: a tunnel set that has not changed has to serialise to the same LeaseSet, or the router republishes identical leases and the floodfill does needless work.

      The tie-break is not cosmetic, it is the whole correctness of the LeaseSet. A TreeSet discards any element its comparator calls equal, and end times collide constantly: TunnelPool.computeLeaseEndDate(long, long, long) clamps to now + maxLeaseMs and floors at now + 60s from a single now taken for the whole build, so every tunnel in a batch that outruns the lease cap lands on the very same instant. Comparing end time alone therefore collapsed a pool of eight tunnels to a one-lease LeaseSet, published as such, and a destination advertised through a single tunnel reads as unreachable to everyone else. Breaking ties on tunnel id and gateway keeps the ordering stable while making the comparator agree with Lease.equals(Object), so the set can no longer silently lose leases.

      Specified by:
      compare in interface Comparator<Lease>