@Test public void ipv4() { assertEquals("1.1.1.1", Hosts.parse("1.1.1.1").name()); assertEquals("0.0.0.1", Hosts.parse("0.0.0.1").name()); assertEquals("0.0.0.0", Hosts.parse("0.0.0.0").name()); assertEquals("255.255.255.255", Hosts.parse("255.255.255.255").name()); }
@Nullable static Ip4 parse(String hostname) { String[] segments = hostname.split("\\."); if (segments.length != 4) { return null; } byte[] addr = new byte[4]; for (int i = 0; i < segments.length; i++) { int val; String segment = segments[i]; if (segment.length() > 1 && segment.startsWith("0")) { return null; } try { val = Integer.parseInt(segment); } catch (NumberFormatException e) { return null; } if (val < 0 || val > 255) { return null; } addr[i] = (byte) val; } return fromAddress(addr); }
Ip4 implements Host { @Nullable static Ip4 parse(String hostname) { String[] segments = hostname.split("\\."); if (segments.length != 4) { return null; } byte[] addr = new byte[4]; for (int i = 0; i < segments.length; i++) { int val; String segment = segments[i]; if (segment.length() > 1 && segment.startsWith("0")) { return null; } try { val = Integer.parseInt(segment); } catch (NumberFormatException e) { return null; } if (val < 0 || val > 255) { return null; } addr[i] = (byte) val; } return fromAddress(addr); } }
Ip4 implements Host { @Nullable static Ip4 parse(String hostname) { String[] segments = hostname.split("\\."); if (segments.length != 4) { return null; } byte[] addr = new byte[4]; for (int i = 0; i < segments.length; i++) { int val; String segment = segments[i]; if (segment.length() > 1 && segment.startsWith("0")) { return null; } try { val = Integer.parseInt(segment); } catch (NumberFormatException e) { return null; } if (val < 0 || val > 255) { return null; } addr[i] = (byte) val; } return fromAddress(addr); }  }
Ip4 implements Host { @Nullable static Ip4 parse(String hostname) { String[] segments = hostname.split("\\."); if (segments.length != 4) { return null; } byte[] addr = new byte[4]; for (int i = 0; i < segments.length; i++) { int val; String segment = segments[i]; if (segment.length() > 1 && segment.startsWith("0")) { return null; } try { val = Integer.parseInt(segment); } catch (NumberFormatException e) { return null; } if (val < 0 || val > 255) { return null; } addr[i] = (byte) val; } return fromAddress(addr); }   }
Ip4 implements Host { @Nullable static Ip4 parse(String hostname) { String[] segments = hostname.split("\\."); if (segments.length != 4) { return null; } byte[] addr = new byte[4]; for (int i = 0; i < segments.length; i++) { int val; String segment = segments[i]; if (segment.length() > 1 && segment.startsWith("0")) { return null; } try { val = Integer.parseInt(segment); } catch (NumberFormatException e) { return null; } if (val < 0 || val > 255) { return null; } addr[i] = (byte) val; } return fromAddress(addr); }    }