@Test public void testSamePrefix_NullInput () { Ipv4Prefix prefix = new Ipv4Prefix(IPV4_ADDRESS_1 + MASK_24); boolean result = NetUtils.samePrefix(new IpPrefix(prefix), null); Assert.assertFalse(result); result = NetUtils.samePrefix(null, new IpPrefix(prefix)); Assert.assertFalse(result); }
public static boolean samePrefix(@Nullable IpPrefix prefix1, @Nullable IpPrefix prefix2) { if (prefix1 == null || prefix2 == null) { return false; } if (prefix1.getIpv4Prefix() != null && prefix2.getIpv4Prefix() != null) { SubnetUtils fromPrefix1 = new SubnetUtils(prefix1.getIpv4Prefix().getValue()); SubnetUtils fromPrefix2 = new SubnetUtils(prefix2.getIpv4Prefix().getValue()); if (fromPrefix1.getInfo().getNetworkAddress().equals(fromPrefix2.getInfo().getNetworkAddress()) && fromPrefix1.getInfo().getNetmask().equals(fromPrefix2.getInfo().getNetmask())) { return true; } } else if (prefix1.getIpv6Prefix() != null && prefix2.getIpv6Prefix() != null) { int ipv6MaskLength1 = getMaskFromPrefix(prefix1.getIpv6Prefix().getValue()); int ipv6MaskLength2 = getMaskFromPrefix(prefix2.getIpv6Prefix().getValue()); if (ipv6MaskLength1 != ipv6MaskLength2) { return false; } Ipv6Address ipv6Network = applyMaskOnIpv6Prefix(prefix1.getIpv6Prefix(), ipv6MaskLength1); Ipv6Address hostv6Network = applyMaskOnIpv6Prefix(prefix2.getIpv6Prefix(), ipv6MaskLength1); if (ipv6Network.getValue().equals(hostv6Network.getValue())) { return true; } } return false; }
NetUtils { public static boolean samePrefix(@Nullable IpPrefix prefix1, @Nullable IpPrefix prefix2) { if (prefix1 == null || prefix2 == null) { return false; } if (prefix1.getIpv4Prefix() != null && prefix2.getIpv4Prefix() != null) { SubnetUtils fromPrefix1 = new SubnetUtils(prefix1.getIpv4Prefix().getValue()); SubnetUtils fromPrefix2 = new SubnetUtils(prefix2.getIpv4Prefix().getValue()); if (fromPrefix1.getInfo().getNetworkAddress().equals(fromPrefix2.getInfo().getNetworkAddress()) && fromPrefix1.getInfo().getNetmask().equals(fromPrefix2.getInfo().getNetmask())) { return true; } } else if (prefix1.getIpv6Prefix() != null && prefix2.getIpv6Prefix() != null) { int ipv6MaskLength1 = getMaskFromPrefix(prefix1.getIpv6Prefix().getValue()); int ipv6MaskLength2 = getMaskFromPrefix(prefix2.getIpv6Prefix().getValue()); if (ipv6MaskLength1 != ipv6MaskLength2) { return false; } Ipv6Address ipv6Network = applyMaskOnIpv6Prefix(prefix1.getIpv6Prefix(), ipv6MaskLength1); Ipv6Address hostv6Network = applyMaskOnIpv6Prefix(prefix2.getIpv6Prefix(), ipv6MaskLength1); if (ipv6Network.getValue().equals(hostv6Network.getValue())) { return true; } } return false; } }
NetUtils { public static boolean samePrefix(@Nullable IpPrefix prefix1, @Nullable IpPrefix prefix2) { if (prefix1 == null || prefix2 == null) { return false; } if (prefix1.getIpv4Prefix() != null && prefix2.getIpv4Prefix() != null) { SubnetUtils fromPrefix1 = new SubnetUtils(prefix1.getIpv4Prefix().getValue()); SubnetUtils fromPrefix2 = new SubnetUtils(prefix2.getIpv4Prefix().getValue()); if (fromPrefix1.getInfo().getNetworkAddress().equals(fromPrefix2.getInfo().getNetworkAddress()) && fromPrefix1.getInfo().getNetmask().equals(fromPrefix2.getInfo().getNetmask())) { return true; } } else if (prefix1.getIpv6Prefix() != null && prefix2.getIpv6Prefix() != null) { int ipv6MaskLength1 = getMaskFromPrefix(prefix1.getIpv6Prefix().getValue()); int ipv6MaskLength2 = getMaskFromPrefix(prefix2.getIpv6Prefix().getValue()); if (ipv6MaskLength1 != ipv6MaskLength2) { return false; } Ipv6Address ipv6Network = applyMaskOnIpv6Prefix(prefix1.getIpv6Prefix(), ipv6MaskLength1); Ipv6Address hostv6Network = applyMaskOnIpv6Prefix(prefix2.getIpv6Prefix(), ipv6MaskLength1); if (ipv6Network.getValue().equals(hostv6Network.getValue())) { return true; } } return false; }  }
NetUtils { public static boolean samePrefix(@Nullable IpPrefix prefix1, @Nullable IpPrefix prefix2) { if (prefix1 == null || prefix2 == null) { return false; } if (prefix1.getIpv4Prefix() != null && prefix2.getIpv4Prefix() != null) { SubnetUtils fromPrefix1 = new SubnetUtils(prefix1.getIpv4Prefix().getValue()); SubnetUtils fromPrefix2 = new SubnetUtils(prefix2.getIpv4Prefix().getValue()); if (fromPrefix1.getInfo().getNetworkAddress().equals(fromPrefix2.getInfo().getNetworkAddress()) && fromPrefix1.getInfo().getNetmask().equals(fromPrefix2.getInfo().getNetmask())) { return true; } } else if (prefix1.getIpv6Prefix() != null && prefix2.getIpv6Prefix() != null) { int ipv6MaskLength1 = getMaskFromPrefix(prefix1.getIpv6Prefix().getValue()); int ipv6MaskLength2 = getMaskFromPrefix(prefix2.getIpv6Prefix().getValue()); if (ipv6MaskLength1 != ipv6MaskLength2) { return false; } Ipv6Address ipv6Network = applyMaskOnIpv6Prefix(prefix1.getIpv6Prefix(), ipv6MaskLength1); Ipv6Address hostv6Network = applyMaskOnIpv6Prefix(prefix2.getIpv6Prefix(), ipv6MaskLength1); if (ipv6Network.getValue().equals(hostv6Network.getValue())) { return true; } } return false; }  static int getMaskFromPrefix(@Nullable String prefix); static @Nonnull String getIpAddrFromPrefix(@Nullable String prefix); static @Nullable Ipv6Address applyMaskOnIpv6Prefix(@Nullable Ipv6Prefix ipv6Prefix, int ipv6MaskLength); static @Nullable Ipv6Address applyMaskOnIpv6Address(@Nullable Ipv6Address ipv6Address, int ipv6MaskLength); static boolean samePrefix(@Nullable IpPrefix prefix1, @Nullable IpPrefix prefix2); static boolean isInRange(@Nullable IpPrefix prefix, @Nullable String ipAddress); }
NetUtils { public static boolean samePrefix(@Nullable IpPrefix prefix1, @Nullable IpPrefix prefix2) { if (prefix1 == null || prefix2 == null) { return false; } if (prefix1.getIpv4Prefix() != null && prefix2.getIpv4Prefix() != null) { SubnetUtils fromPrefix1 = new SubnetUtils(prefix1.getIpv4Prefix().getValue()); SubnetUtils fromPrefix2 = new SubnetUtils(prefix2.getIpv4Prefix().getValue()); if (fromPrefix1.getInfo().getNetworkAddress().equals(fromPrefix2.getInfo().getNetworkAddress()) && fromPrefix1.getInfo().getNetmask().equals(fromPrefix2.getInfo().getNetmask())) { return true; } } else if (prefix1.getIpv6Prefix() != null && prefix2.getIpv6Prefix() != null) { int ipv6MaskLength1 = getMaskFromPrefix(prefix1.getIpv6Prefix().getValue()); int ipv6MaskLength2 = getMaskFromPrefix(prefix2.getIpv6Prefix().getValue()); if (ipv6MaskLength1 != ipv6MaskLength2) { return false; } Ipv6Address ipv6Network = applyMaskOnIpv6Prefix(prefix1.getIpv6Prefix(), ipv6MaskLength1); Ipv6Address hostv6Network = applyMaskOnIpv6Prefix(prefix2.getIpv6Prefix(), ipv6MaskLength1); if (ipv6Network.getValue().equals(hostv6Network.getValue())) { return true; } } return false; }  static int getMaskFromPrefix(@Nullable String prefix); static @Nonnull String getIpAddrFromPrefix(@Nullable String prefix); static @Nullable Ipv6Address applyMaskOnIpv6Prefix(@Nullable Ipv6Prefix ipv6Prefix, int ipv6MaskLength); static @Nullable Ipv6Address applyMaskOnIpv6Address(@Nullable Ipv6Address ipv6Address, int ipv6MaskLength); static boolean samePrefix(@Nullable IpPrefix prefix1, @Nullable IpPrefix prefix2); static boolean isInRange(@Nullable IpPrefix prefix, @Nullable String ipAddress);  }