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
Showing
4 changed files
with
100 additions
and
0 deletions
| ... | @@ -80,6 +80,24 @@ public class IpAddress implements Comparable<IpAddress> { | ... | @@ -80,6 +80,24 @@ public class IpAddress implements Comparable<IpAddress> { |
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | /** | 82 | /** |
| 83 | + * Tests whether the IP version of this address is IPv4. | ||
| 84 | + * | ||
| 85 | + * @return true if the IP version of this address is IPv4, otherwise false. | ||
| 86 | + */ | ||
| 87 | + public boolean isIp4() { | ||
| 88 | + return (version() == Ip4Address.VERSION); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + /** | ||
| 92 | + * Tests whether the IP version of this address is IPv6. | ||
| 93 | + * | ||
| 94 | + * @return true if the IP version of this address is IPv6, otherwise false. | ||
| 95 | + */ | ||
| 96 | + public boolean isIp6() { | ||
| 97 | + return (version() == Ip6Address.VERSION); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + /** | ||
| 83 | * Gets the {@link Ip4Address} view of the IP address. | 101 | * Gets the {@link Ip4Address} view of the IP address. |
| 84 | * | 102 | * |
| 85 | * @return the {@link Ip4Address} view of the IP address if it is IPv4, | 103 | * @return the {@link Ip4Address} view of the IP address if it is IPv4, | ... | ... |
| ... | @@ -57,6 +57,24 @@ public class IpPrefix { | ... | @@ -57,6 +57,24 @@ public class IpPrefix { |
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | /** | 59 | /** |
| 60 | + * Tests whether the IP version of this prefix is IPv4. | ||
| 61 | + * | ||
| 62 | + * @return true if the IP version of this prefix is IPv4, otherwise false. | ||
| 63 | + */ | ||
| 64 | + public boolean isIp4() { | ||
| 65 | + return address.isIp4(); | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + /** | ||
| 69 | + * Tests whether the IP version of this prefix is IPv6. | ||
| 70 | + * | ||
| 71 | + * @return true if the IP version of this prefix is IPv6, otherwise false. | ||
| 72 | + */ | ||
| 73 | + public boolean isIp6() { | ||
| 74 | + return address.isIp6(); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + /** | ||
| 60 | * Returns the IP address value of the prefix. | 78 | * Returns the IP address value of the prefix. |
| 61 | * | 79 | * |
| 62 | * @return the IP address value of the prefix | 80 | * @return the IP address value of the prefix | ... | ... |
| ... | @@ -78,6 +78,38 @@ public class IpAddressTest { | ... | @@ -78,6 +78,38 @@ public class IpAddressTest { |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | /** | 80 | /** |
| 81 | + * Tests whether the IP version of an address is IPv4. | ||
| 82 | + */ | ||
| 83 | + @Test | ||
| 84 | + public void testIsIp4() { | ||
| 85 | + IpAddress ipAddress; | ||
| 86 | + | ||
| 87 | + // IPv4 | ||
| 88 | + ipAddress = IpAddress.valueOf("0.0.0.0"); | ||
| 89 | + assertTrue(ipAddress.isIp4()); | ||
| 90 | + | ||
| 91 | + // IPv6 | ||
| 92 | + ipAddress = IpAddress.valueOf("::"); | ||
| 93 | + assertFalse(ipAddress.isIp4()); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + /** | ||
| 97 | + * Tests whether the IP version of an address is IPv6. | ||
| 98 | + */ | ||
| 99 | + @Test | ||
| 100 | + public void testIsIp6() { | ||
| 101 | + IpAddress ipAddress; | ||
| 102 | + | ||
| 103 | + // IPv4 | ||
| 104 | + ipAddress = IpAddress.valueOf("0.0.0.0"); | ||
| 105 | + assertFalse(ipAddress.isIp6()); | ||
| 106 | + | ||
| 107 | + // IPv6 | ||
| 108 | + ipAddress = IpAddress.valueOf("::"); | ||
| 109 | + assertTrue(ipAddress.isIp6()); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + /** | ||
| 81 | * Tests getting the Ip4Address and Ip6Address view of the IP address. | 113 | * Tests getting the Ip4Address and Ip6Address view of the IP address. |
| 82 | */ | 114 | */ |
| 83 | @Test | 115 | @Test | ... | ... |
| ... | @@ -65,6 +65,38 @@ public class IpPrefixTest { | ... | @@ -65,6 +65,38 @@ public class IpPrefixTest { |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | /** | 67 | /** |
| 68 | + * Tests whether the IP version of a prefix is IPv4. | ||
| 69 | + */ | ||
| 70 | + @Test | ||
| 71 | + public void testIsIp4() { | ||
| 72 | + IpPrefix ipPrefix; | ||
| 73 | + | ||
| 74 | + // IPv4 | ||
| 75 | + ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); | ||
| 76 | + assertTrue(ipPrefix.isIp4()); | ||
| 77 | + | ||
| 78 | + // IPv6 | ||
| 79 | + ipPrefix = IpPrefix.valueOf("::/0"); | ||
| 80 | + assertFalse(ipPrefix.isIp4()); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + /** | ||
| 84 | + * Tests whether the IP version of a prefix is IPv6. | ||
| 85 | + */ | ||
| 86 | + @Test | ||
| 87 | + public void testIsIp6() { | ||
| 88 | + IpPrefix ipPrefix; | ||
| 89 | + | ||
| 90 | + // IPv4 | ||
| 91 | + ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); | ||
| 92 | + assertFalse(ipPrefix.isIp6()); | ||
| 93 | + | ||
| 94 | + // IPv6 | ||
| 95 | + ipPrefix = IpPrefix.valueOf("::/0"); | ||
| 96 | + assertTrue(ipPrefix.isIp6()); | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + /** | ||
| 68 | * Tests returning the IP address value and IP address prefix length of | 100 | * Tests returning the IP address value and IP address prefix length of |
| 69 | * an IPv4 prefix. | 101 | * an IPv4 prefix. |
| 70 | */ | 102 | */ | ... | ... |
-
Please register or login to post a comment