+ @Nonnull public final ANZ ipAddressNoZoneFor(@Nonnull final InetAddress addr) {
+ Preconditions.checkNotNull(addr, "Address must not be null");
+ if (addr instanceof Inet4Address) {
+ return ipv4AddressNoZone(ipv4AddressFor(addr));
+ } else if (addr instanceof Inet6Address) {
+ return ipv6AddressNoZone(ipv6AddressFor(addr));
+ } else {
+ throw new IllegalArgumentException("Unhandled address " + addr);
+ }
+ }
+
+ /**
+ * 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);
+ }
+ }
+
+ @Nonnull public final InetAddress inetAddressFor(@Nonnull final A addr) {
+ final A4 v4 = maybeIpv4Address(addr);
+ if (v4 != null) {
+ return inet4AddressFor(v4);
+ }
+ final A6 v6 = maybeIpv6Address(addr);
+ Preconditions.checkArgument(v6 != null, "Address %s is neither IPv4 nor IPv6", addr);
+ return inet6AddressFor(v6);
+ }
+
+ @Nonnull public final Inet4Address inet4AddressFor(@Nonnull final A4 addr) {
+ try {
+ return (Inet4Address) InetAddress.getByAddress(ipv4AddressBytes(addr));
+ } catch (UnknownHostException e) {
+ throw new IllegalArgumentException("Invalid address " + addr, e);
+ }
+ }
+
+ @Nonnull public final Inet6Address inet6AddressFor(@Nonnull final A6 addr) {
+ try {
+ return (Inet6Address) InetAddress.getByAddress(ipv6AddressBytes(addr));
+ } catch (UnknownHostException e) {
+ throw new IllegalArgumentException("Invalid address " + addr, e);
+ }
+ }
+