Module inet.ipaddr

Class AddressTrie.AddressComparator<E extends Address>

java.lang.Object
inet.ipaddr.format.util.AddressTrie.AddressComparator<E>
Type Parameters:
E -
All Implemented Interfaces:
Serializable, Comparator<E>
Enclosing class:
AddressTrie<E extends Address>

public static class AddressTrie.AddressComparator<E extends Address> extends Object implements Comparator<E>, Serializable
A comparator that provides the same ordering used by the trie, an ordering that works with prefix block subnets and individual addresses. The comparator is consistent with the equality and hashcode of address instances and can be used in other contexts. However, it only works with prefix blocks and individual addresses, not with addresses like 1-2.3.4.5-6 which cannot be differentiated using this comparator from 1.3.4.5 and is thus not consistent with equals and hashcode for subnets that are not CIDR prefix blocks.

The comparator first compares the prefix of addresses, with the full address value considered the prefix when there is no prefix length, ie when it is a single address. It takes the minimum m of the two prefix lengths and compares those m prefix bits in both addresses. The ordering is determined by which of those two values is smaller or larger.

If those two values match, then it looks at the address with longer prefix. If both prefix lengths match then both addresses are equal. Otherwise it looks at bit m in the address with larger prefix. If 1 it is larger and if 0 it is smaller than the other.

When comparing an address with a prefix p and an address without, the first p bits in both are compared, and if equal, the bit at index p in the non-prefixed address determines the ordering, if 1 it is larger and if 0 it is smaller than the other.

When comparing an address with prefix length matching the bit count to an address with no prefix, they are considered equal if the bits match. For instance, 1.2.3.4/32 is equal to 1.2.3.4, and thus the trie does not allow 1.2.3.4/32 in the trie since it is indistinguishable from 1.2.3.4, instead 1.2.3.4/32 is converted to 1.2.3.4 when inserted into the trie.

When comparing 0.0.0.0/0, which has no prefix, to other addresses, the first bit in the other address determines the ordering. If 1 it is larger and if 0 it is smaller than 0.0.0.0/0.

Author:
scfoley
See Also:
  • Constructor Details

    • AddressComparator

      public AddressComparator()
  • Method Details