private final StringValueObjectFactory<A6> address6Factory;
private final StringValueObjectFactory<A6NZ> address6NoZoneFactory;
private final StringValueObjectFactory<P6> prefix6Factory;
+ private final Class<A4NZ> addr4nzClass;
+ private final Class<A6NZ> addr6nzClass;
protected AbstractIetfInetUtil(final Class<A4> addr4Class, final Class<A4NZ> addr4nzClass,
final Class<P4> prefix4Class, final Class<A6> addr6Class, final Class<A6NZ> addr6nzClass,
final Class<P6> prefix6Class) {
+ this.addr4nzClass = requireNonNull(addr4nzClass);
+ this.addr6nzClass = requireNonNull(addr6nzClass);
this.address4Factory = StringValueObjectFactory.create(addr4Class, "0.0.0.0");
this.address4NoZoneFactory = StringValueObjectFactory.create(addr4nzClass, "0.0.0.0");
this.prefix4Factory = StringValueObjectFactory.create(prefix4Class, "0.0.0.0/0");
* @throws NullPointerException if addr is null
*/
public final @NonNull A4 ipv4AddressFor(final @NonNull InetAddress addr) {
- requireNonNull(addr, "Address must not be null");
- checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
- return address4Factory.newInstance(addr.getHostAddress());
+ return address4Factory.newInstance(addressStringV4(addr));
}
/**
* @return An Ipv4Address object
*/
public final @NonNull A4 ipv4AddressFor(final int bits) {
- return address4Factory.newInstance(addressString(bits));
+ return address4Factory.newInstance(Ipv4Utils.addressString(bits));
}
/**
* @throws NullPointerException if addr is null
*/
public final @NonNull A4NZ ipv4AddressNoZoneFor(final @NonNull InetAddress addr) {
- requireNonNull(addr, "Address must not be null");
- checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
- return address4NoZoneFactory.newInstance(addr.getHostAddress());
+ return address4NoZoneFactory.newInstance(addressStringV4(addr));
}
/**
* @return An Ipv4AddressNoZone object
*/
public final @NonNull A4NZ ipv4AddressNoZoneFor(final int bits) {
- return address4NoZoneFactory.newInstance(addressString(bits));
+ return address4NoZoneFactory.newInstance(Ipv4Utils.addressString(bits));
+ }
+
+ /**
+ * Create an Ipv4AddressNoZone by interpreting an Ipv4Address.
+ *
+ * @param addr An Ipv4Address
+ * @return An Ipv4AddressNoZone object
+ * @throws NullPointerException if addr is null
+ */
+ public final @NonNull A4NZ ipv4AddressNoZoneFor(final @NonNull A4 addr) {
+ requireNonNull(addr, "Address must not be null");
+ return addr4nzClass.isInstance(addr) ? addr4nzClass.cast(addr)
+ : address4NoZoneFactory.newInstance(stripZone(ipv4AddressString(addr)));
}
public final @NonNull A4 ipv4AddressFrom(final @NonNull P4 prefix) {
*/
final String str = ipv4AddressString(addr);
final int percent = str.indexOf('%');
- return ipv4StringBytes(str, percent == -1 ? str.length() : percent);
+ return Ipv4Utils.addressBytes(str, percent == -1 ? str.length() : percent);
}
public final int ipv4AddressBits(final @NonNull A4 addr) {
final String str = ipv4AddressString(addr);
final int percent = str.indexOf('%');
- return ipv4StringBits(str, percent == -1 ? str.length() : percent);
+ return Ipv4Utils.addressBits(str, percent == -1 ? str.length() : percent);
}
public final byte @NonNull[] ipv4AddressNoZoneBytes(final @NonNull A4NZ addr) {
* the Ipv4AddressNoZone pattern, which must not include a zone index.
*/
final String str = ipv4AddressString(addr);
- return ipv4StringBytes(str, str.length());
+ return Ipv4Utils.addressBytes(str, str.length());
}
public final int ipv4AddressNoZoneBits(final @NonNull A4NZ addr) {
final String str = ipv4AddressString(addr);
- return ipv4StringBits(str, str.length());
- }
-
- private static byte @NonNull[] ipv4StringBytes(final String str, final int limit) {
- final byte[] bytes = new byte[INET4_LENGTH];
- Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, limit);
- return bytes;
- }
-
- private static int ipv4StringBits(final String str, final int limit) {
- int prev = 0;
- int current = 0;
- for (int i = 0, shift = 24; i < limit; ++i) {
- final char c = str.charAt(i);
- if (c == '.') {
- prev |= current << shift;
- shift -= 8;
- current = 0;
- } else {
- current = 10 * current + c - '0';
- }
- }
- return prev | current;
+ return Ipv4Utils.addressBits(str, str.length());
}
/**
* @throws NullPointerException if addr is null
*/
public final @NonNull P4 ipv4PrefixFor(final @NonNull InetAddress addr) {
- requireNonNull(addr, "Address must not be null");
- checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
- return prefix4Factory.newInstance(addr.getHostAddress() + "/32");
+ return prefix4Factory.newInstance(addressStringV4(addr) + "/32");
}
/**
* @throws NullPointerException if addr is null
*/
public final @NonNull P4 ipv4PrefixFor(final @NonNull InetAddress addr, final int mask) {
- requireNonNull(addr, "Address must not be null");
- checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
- return newIpv4Prefix(addr.getHostAddress(), mask);
+ return newIpv4Prefix(addressStringV4(addr), mask);
}
public final @NonNull P4 ipv4PrefixFor(final @NonNull A4 addr) {
* @throws NullPointerException if addr is null
*/
public final @NonNull A6 ipv6AddressFor(final @NonNull InetAddress addr) {
- requireNonNull(addr, "Address must not be null");
- checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
return address6Factory.newInstance(addressStringV6(addr));
}
* @throws NullPointerException if addr is null
*/
public final @NonNull A6NZ ipv6AddressNoZoneFor(final @NonNull InetAddress addr) {
- requireNonNull(addr, "Address must not be null");
- checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
return address6NoZoneFactory.newInstance(addressStringV6(addr));
}
+ /**
+ * Create an Ipv6AddressNoZone by interpreting an Ipv6Address.
+ *
+ * @param addr An Ipv6Address
+ * @return An Ipv6AddressNoZone object
+ * @throws NullPointerException if addr is null
+ */
+ public final @NonNull A6NZ ipv6AddressNoZoneFor(final @NonNull A6 addr) {
+ requireNonNull(addr, "Address must not be null");
+ return addr6nzClass.isInstance(addr) ? addr6nzClass.cast(addr)
+ : address6NoZoneFactory.newInstance(stripZone(ipv6AddressString(addr)));
+ }
+
public final @NonNull A6 ipv6AddressFrom(final @NonNull P6 prefix) {
return prefixToAddress(address6Factory, ipv6PrefixString(prefix));
}
}
/**
- * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv4 address.
+ * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv6 address.
*
* @param addr an {@link Inet6Address}
* @return An Ipv6Prefix object
- * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
+ * @throws IllegalArgumentException if addr is not an Inet6Address
* @throws NullPointerException if addr is null
*/
public final @NonNull P6 ipv6PrefixFor(final @NonNull InetAddress addr) {
* @throws NullPointerException if addr is null
*/
public final @NonNull P6 ipv6PrefixFor(final @NonNull InetAddress addr, final int mask) {
- requireNonNull(addr, "Address must not be null");
- checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
return prefix6Factory.newInstance(addressStringV6(addr) + '/' + mask);
}
}
}
- private static String addressString(final int bits) {
- return new StringBuilder(15)
- .append(bits >>> 24).append('.')
- .append(bits >>> 16 & 0xFF).append('.')
- .append(bits >>> 8 & 0xFF).append('.')
- .append(bits & 0xFF)
- .toString();
- }
-
static String addressStringV4(final byte @NonNull[] bytes) {
final StringBuilder sb = new StringBuilder(15);
appendIpv4String(sb, bytes);
return sb.toString();
}
+ private static @NonNull String addressStringV4(final InetAddress addr) {
+ requireNonNull(addr, "Address must not be null");
+ checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
+ return addr.getHostAddress();
+ }
+
private static String addressStringV6(final byte @NonNull[] bytes) {
checkArgument(bytes.length == INET6_LENGTH, "IPv6 address length is 16 bytes");
try {
- return addressStringV6(Inet6Address.getByAddress(bytes));
+ return addressStringV6(Inet6Address.getByAddress(null, bytes, null));
} catch (UnknownHostException e) {
throw new IllegalArgumentException(String.format("Invalid input %s", bytes), e);
}
}
private static String addressStringV6(final InetAddress addr) {
+ requireNonNull(addr, "Address must not be null");
+ checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
+ return addressStringV6((Inet6Address) addr);
+ }
+
+ private static String addressStringV6(final Inet6Address addr) {
return InetAddresses.toAddrString(addr);
}