@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "invalidCidr") public void testFromCidrInvalid(String cidr) { Inet4Network.fromCidr(cidr); }
@SuppressWarnings("StringSplitter") public static Inet4Network fromCidr(String cidr) { String[] parts = cidr.split("/"); if (parts.length != 2) { throw new IllegalArgumentException("invalid CIDR format: " + cidr); } Inet4Address address = (Inet4Address) InetAddresses.forString(parts[0]); int bits = Integer.parseInt(parts[1]); if ((bits < 0) || (bits > 32)) { throw new IllegalArgumentException("invalid prefix size: " + bits); } int mask = (bits == 0) ? 0 : (-1 << (32 - bits)); int ip = InetAddresses.coerceToInteger(address); if ((ip & mask) != ip) { throw new IllegalArgumentException("invalid prefix for prefix size: " + bits); } return new Inet4Network(address, bits); }
Inet4Network { @SuppressWarnings("StringSplitter") public static Inet4Network fromCidr(String cidr) { String[] parts = cidr.split("/"); if (parts.length != 2) { throw new IllegalArgumentException("invalid CIDR format: " + cidr); } Inet4Address address = (Inet4Address) InetAddresses.forString(parts[0]); int bits = Integer.parseInt(parts[1]); if ((bits < 0) || (bits > 32)) { throw new IllegalArgumentException("invalid prefix size: " + bits); } int mask = (bits == 0) ? 0 : (-1 << (32 - bits)); int ip = InetAddresses.coerceToInteger(address); if ((ip & mask) != ip) { throw new IllegalArgumentException("invalid prefix for prefix size: " + bits); } return new Inet4Network(address, bits); } }
Inet4Network { @SuppressWarnings("StringSplitter") public static Inet4Network fromCidr(String cidr) { String[] parts = cidr.split("/"); if (parts.length != 2) { throw new IllegalArgumentException("invalid CIDR format: " + cidr); } Inet4Address address = (Inet4Address) InetAddresses.forString(parts[0]); int bits = Integer.parseInt(parts[1]); if ((bits < 0) || (bits > 32)) { throw new IllegalArgumentException("invalid prefix size: " + bits); } int mask = (bits == 0) ? 0 : (-1 << (32 - bits)); int ip = InetAddresses.coerceToInteger(address); if ((ip & mask) != ip) { throw new IllegalArgumentException("invalid prefix for prefix size: " + bits); } return new Inet4Network(address, bits); } private  Inet4Network(Inet4Address address, int bits); }
Inet4Network { @SuppressWarnings("StringSplitter") public static Inet4Network fromCidr(String cidr) { String[] parts = cidr.split("/"); if (parts.length != 2) { throw new IllegalArgumentException("invalid CIDR format: " + cidr); } Inet4Address address = (Inet4Address) InetAddresses.forString(parts[0]); int bits = Integer.parseInt(parts[1]); if ((bits < 0) || (bits > 32)) { throw new IllegalArgumentException("invalid prefix size: " + bits); } int mask = (bits == 0) ? 0 : (-1 << (32 - bits)); int ip = InetAddresses.coerceToInteger(address); if ((ip & mask) != ip) { throw new IllegalArgumentException("invalid prefix for prefix size: " + bits); } return new Inet4Network(address, bits); } private  Inet4Network(Inet4Address address, int bits); boolean containsAddress(Inet4Address address); @Override String toString(); @Override @SuppressWarnings("RedundantIfStatement") boolean equals(Object o); @Override int hashCode(); @SuppressWarnings("StringSplitter") static Inet4Network fromCidr(String cidr); }
Inet4Network { @SuppressWarnings("StringSplitter") public static Inet4Network fromCidr(String cidr) { String[] parts = cidr.split("/"); if (parts.length != 2) { throw new IllegalArgumentException("invalid CIDR format: " + cidr); } Inet4Address address = (Inet4Address) InetAddresses.forString(parts[0]); int bits = Integer.parseInt(parts[1]); if ((bits < 0) || (bits > 32)) { throw new IllegalArgumentException("invalid prefix size: " + bits); } int mask = (bits == 0) ? 0 : (-1 << (32 - bits)); int ip = InetAddresses.coerceToInteger(address); if ((ip & mask) != ip) { throw new IllegalArgumentException("invalid prefix for prefix size: " + bits); } return new Inet4Network(address, bits); } private  Inet4Network(Inet4Address address, int bits); boolean containsAddress(Inet4Address address); @Override String toString(); @Override @SuppressWarnings("RedundantIfStatement") boolean equals(Object o); @Override int hashCode(); @SuppressWarnings("StringSplitter") static Inet4Network fromCidr(String cidr);  }