/** * Create an {@link IpNetwork} instance which represents the IP network * specified by a pair of IP address and CIDR prefix. * * @param iaddr An {@link InetAddress} instance. * @param prefix Prefix length that specifies network range. * Note that zero means "no mask". So zero is treated as if * the maximum prefix length is specified. * @return An {@link IpNetwork} instance which represents the IP network * specified by the given IP address and CIDR prefix. * Note that {@code null} is returned if {@code iaddr} is * {@code null}. * @throws IllegalArgumentException * The given IP address or prefix is invalid. */ public static final IpNetwork create(InetAddress iaddr, int prefix) { if (iaddr instanceof Inet4Address) { return new Ip4Network(iaddr, prefix); } if (iaddr == null) { return null; } throw getUnsupportedAddressException(iaddr); }
/** * Create an {@link IpNetwork} instance which represents the IP network * specified by the given {@link Address} instance. * * @param addr An {@link Address} instance which represents the * IP address. * @return An {@link IpNetwork} instance which represents the IP network * specified by {@code addr}. Note that {@code null} is returned * if {@code addr} is {@code null} or it does not contain valid * value. * @throws IllegalArgumentException * The given {@link Address} instance is invalid. */ public static final IpNetwork create(Address addr) { if (addr instanceof Ipv4) { Ipv4Prefix ipv4 = ((Ipv4)addr).getIpv4Address(); return (ipv4 == null) ? null : create(ipv4.getValue()); } else if (addr == null) { return null; } throw getUnsupportedAddressException(addr); }
/** * Create an {@link IpNetwork} instance which represents the IP address * specified by the given {@link IpAddress} instance. * * @param ip An {@link IpAddress} instance which represents the IP * address. * @return An {@link IpNetwork} instance which represents the given * IP address. Note that {@code null} is returned if {@code ip} * is {@code null} or it does not contain valid value. * @throws IllegalArgumentException * The given {@link IpAddress} instance is invalid. * @since Beryllium */ public static final IpNetwork create(IpAddress ip) { if (ip != null) { Ip4Network ip4 = Ip4Network.create(ip.getIpv4Address()); if (ip4 != null) { return ip4; } if (ip.getIpv6Address() != null) { throw getUnsupportedAddressException(ip); } } return null; }