Pavlin Radoslavov

Added methods to test whether an IP address/prefix is IPv4 or IPv6:

  IpAddress.isIp4()
  IpAddress.isIp6()
  IpPrefix.isIp4()
  IpPrefix.isIp6()

Also, added the corresponding unit tests.

Change-Id: I2b1f08501c94d61f75b15f2c6977c0349e313ebd
......@@ -80,6 +80,24 @@ public class IpAddress implements Comparable<IpAddress> {
}
/**
* Tests whether the IP version of this address is IPv4.
*
* @return true if the IP version of this address is IPv4, otherwise false.
*/
public boolean isIp4() {
return (version() == Ip4Address.VERSION);
}
/**
* Tests whether the IP version of this address is IPv6.
*
* @return true if the IP version of this address is IPv6, otherwise false.
*/
public boolean isIp6() {
return (version() == Ip6Address.VERSION);
}
/**
* Gets the {@link Ip4Address} view of the IP address.
*
* @return the {@link Ip4Address} view of the IP address if it is IPv4,
......
......@@ -57,6 +57,24 @@ public class IpPrefix {
}
/**
* Tests whether the IP version of this prefix is IPv4.
*
* @return true if the IP version of this prefix is IPv4, otherwise false.
*/
public boolean isIp4() {
return address.isIp4();
}
/**
* Tests whether the IP version of this prefix is IPv6.
*
* @return true if the IP version of this prefix is IPv6, otherwise false.
*/
public boolean isIp6() {
return address.isIp6();
}
/**
* Returns the IP address value of the prefix.
*
* @return the IP address value of the prefix
......
......@@ -78,6 +78,38 @@ public class IpAddressTest {
}
/**
* Tests whether the IP version of an address is IPv4.
*/
@Test
public void testIsIp4() {
IpAddress ipAddress;
// IPv4
ipAddress = IpAddress.valueOf("0.0.0.0");
assertTrue(ipAddress.isIp4());
// IPv6
ipAddress = IpAddress.valueOf("::");
assertFalse(ipAddress.isIp4());
}
/**
* Tests whether the IP version of an address is IPv6.
*/
@Test
public void testIsIp6() {
IpAddress ipAddress;
// IPv4
ipAddress = IpAddress.valueOf("0.0.0.0");
assertFalse(ipAddress.isIp6());
// IPv6
ipAddress = IpAddress.valueOf("::");
assertTrue(ipAddress.isIp6());
}
/**
* Tests getting the Ip4Address and Ip6Address view of the IP address.
*/
@Test
......
......@@ -65,6 +65,38 @@ public class IpPrefixTest {
}
/**
* Tests whether the IP version of a prefix is IPv4.
*/
@Test
public void testIsIp4() {
IpPrefix ipPrefix;
// IPv4
ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
assertTrue(ipPrefix.isIp4());
// IPv6
ipPrefix = IpPrefix.valueOf("::/0");
assertFalse(ipPrefix.isIp4());
}
/**
* Tests whether the IP version of a prefix is IPv6.
*/
@Test
public void testIsIp6() {
IpPrefix ipPrefix;
// IPv4
ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
assertFalse(ipPrefix.isIp6());
// IPv6
ipPrefix = IpPrefix.valueOf("::/0");
assertTrue(ipPrefix.isIp6());
}
/**
* Tests returning the IP address value and IP address prefix length of
* an IPv4 prefix.
*/
......