IPAnonymizationUtils { public static String anonymize(String ip) { InetAddress inetAddress; try { inetAddress = InetAddresses.forString(ip); } catch (IllegalArgumentException e) { logger.warn(e.getMessage(), e); inetAddress = null; } InetAddress anonymized = null; if (inetAddress instanceof Inet4Address) { anonymized = anonymizeIpV4Address((Inet4Address) inetAddress); } else if (inetAddress instanceof Inet6Address) { anonymized = anonymizeIpV6Address((Inet6Address) inetAddress); } if (anonymized != null) { return anonymized.getHostAddress(); } else { return null; } } private  IPAnonymizationUtils(); static String anonymize(String ip); static Inet4Address anonymizeIpV4Address(Inet4Address inetAddress); static Inet6Address anonymizeIpV6Address(Inet6Address inet6Address);  }
@Test public void testAnonymizeIpv4() throws Exception { Assert.assertEquals("192.123.123.0", IPAnonymizationUtils.anonymize("192.123.123.123")); }
@Test public void testAnonymizeIpv6Full() throws Exception { Assert.assertEquals("fe80:fe1e:b3ff:0:0:0:0:0", IPAnonymizationUtils.anonymize("FE80:FE1E:B3FF:0000:0202:B3FF:FE1E:8329")); }
@Test public void testAnonymizeIpv6Collapsed() throws Exception { Assert.assertEquals("fe80:202:0:0:0:0:0:0", IPAnonymizationUtils.anonymize("FE80:0202::B3FF:FE1E:8329")); }
@Test public void testAnonymizeInvalid() throws Exception { Assert.assertNull(IPAnonymizationUtils.anonymize("java.sun.com")); Assert.assertNull(IPAnonymizationUtils.anonymize("foo.java.sun.com")); Assert.assertNull(IPAnonymizationUtils.anonymize("foo:bar")); Assert.assertNull(IPAnonymizationUtils.anonymize("foo:bar:baz:foo:bar:baz:foo:bar")); Assert.assertNull(IPAnonymizationUtils.anonymize("foo.bar")); Assert.assertNull(IPAnonymizationUtils.anonymize("foobar")); }