+ /**
+ * Create an IpPrefix by combining the address with a mask. The address
+ * bytes are interpreted as an address and the specified mask is concatenated to
+ * it. The address bytes are not masked.
+ *
+ * @param bytes Input address as a 4-byte (IPv4) or 16-byte (IPv6) array
+ * @param mask Prefix mask
+ * @return An IpPrefix object
+ * @throws IllegalArgumentException if bytes has length different from 4 or 16 or if mask is not
+ * in range 0-32 or 0-128 respectively
+ * @throws NullPointerException if bytes is null
+ */
+
+ @Nonnull public final P ipPrefixFor(@Nonnull final byte[] bytes, final int mask) {
+ switch (bytes.length) {
+ case INET4_LENGTH:
+ return ipv4Prefix(ipv4PrefixFor(bytes, mask));
+ case INET6_LENGTH:
+ return ipv6Prefix(ipv6PrefixFor(bytes, mask));
+ default:
+ throw new IllegalArgumentException("Invalid array length " + bytes.length);
+ }
+ }
+
+ @Nonnull public final P ipPrefixFor(@Nonnull final InetAddress addr, final int mask) {
+ Preconditions.checkNotNull(addr, "Address must not be null");
+ if (addr instanceof Inet4Address) {
+ return ipv4Prefix(ipv4PrefixFor(addr, mask));
+ } else if (addr instanceof Inet6Address) {
+ return ipv6Prefix(ipv6PrefixFor(addr, mask));
+ } else {
+ throw new IllegalArgumentException("Unhandled address " + addr);
+ }
+ }
+