@Test public void shouldFailWhenEndingWithNonDigit() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Invalid IPv4 address: '10.1.1.1.'"); Ipv4.parse("10.1.1.1."); }
public static Ipv4 parse(String ipv4Address) { try { String ipv4String = Validate.notNull(ipv4Address).trim(); Validate.isTrue(!ipv4String.isEmpty() && Character.isDigit(ipv4String.charAt(0)) && Character.isDigit(ipv4String.charAt(ipv4String.length() - 1))); Validate.isTrue(!LEADING_ZERO_IN_IPV4.matcher(ipv4String).find(), "IPv4 cannot have leading zeros, as this may be mistaken for octal numbers."); long value = 0; int octet = 0; int octetCount = 1; for (int i = 0; i < ipv4String.length(); ++i) { char ch = ipv4String.charAt(i); if (Character.isDigit(ch)) { octet = octet * 10 + (ch - '0'); } else if (ch == '.') { Validate.isTrue(octetCount < TOTAL_OCTETS); octetCount++; value = addOctet(value, octet); octet = 0; } else { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv4Address)); } } Validate.isTrue(octetCount == TOTAL_OCTETS); return new Ipv4(addOctet(value, octet)); } catch (Exception e) { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv4Address), e); } }
Ipv4 extends AbstractIp<Ipv4, Ipv4Range> { public static Ipv4 parse(String ipv4Address) { try { String ipv4String = Validate.notNull(ipv4Address).trim(); Validate.isTrue(!ipv4String.isEmpty() && Character.isDigit(ipv4String.charAt(0)) && Character.isDigit(ipv4String.charAt(ipv4String.length() - 1))); Validate.isTrue(!LEADING_ZERO_IN_IPV4.matcher(ipv4String).find(), "IPv4 cannot have leading zeros, as this may be mistaken for octal numbers."); long value = 0; int octet = 0; int octetCount = 1; for (int i = 0; i < ipv4String.length(); ++i) { char ch = ipv4String.charAt(i); if (Character.isDigit(ch)) { octet = octet * 10 + (ch - '0'); } else if (ch == '.') { Validate.isTrue(octetCount < TOTAL_OCTETS); octetCount++; value = addOctet(value, octet); octet = 0; } else { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv4Address)); } } Validate.isTrue(octetCount == TOTAL_OCTETS); return new Ipv4(addOctet(value, octet)); } catch (Exception e) { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv4Address), e); } } }
Ipv4 extends AbstractIp<Ipv4, Ipv4Range> { public static Ipv4 parse(String ipv4Address) { try { String ipv4String = Validate.notNull(ipv4Address).trim(); Validate.isTrue(!ipv4String.isEmpty() && Character.isDigit(ipv4String.charAt(0)) && Character.isDigit(ipv4String.charAt(ipv4String.length() - 1))); Validate.isTrue(!LEADING_ZERO_IN_IPV4.matcher(ipv4String).find(), "IPv4 cannot have leading zeros, as this may be mistaken for octal numbers."); long value = 0; int octet = 0; int octetCount = 1; for (int i = 0; i < ipv4String.length(); ++i) { char ch = ipv4String.charAt(i); if (Character.isDigit(ch)) { octet = octet * 10 + (ch - '0'); } else if (ch == '.') { Validate.isTrue(octetCount < TOTAL_OCTETS); octetCount++; value = addOctet(value, octet); octet = 0; } else { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv4Address)); } } Validate.isTrue(octetCount == TOTAL_OCTETS); return new Ipv4(addOctet(value, octet)); } catch (Exception e) { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv4Address), e); } } protected  Ipv4(Long value); }
Ipv4 extends AbstractIp<Ipv4, Ipv4Range> { public static Ipv4 parse(String ipv4Address) { try { String ipv4String = Validate.notNull(ipv4Address).trim(); Validate.isTrue(!ipv4String.isEmpty() && Character.isDigit(ipv4String.charAt(0)) && Character.isDigit(ipv4String.charAt(ipv4String.length() - 1))); Validate.isTrue(!LEADING_ZERO_IN_IPV4.matcher(ipv4String).find(), "IPv4 cannot have leading zeros, as this may be mistaken for octal numbers."); long value = 0; int octet = 0; int octetCount = 1; for (int i = 0; i < ipv4String.length(); ++i) { char ch = ipv4String.charAt(i); if (Character.isDigit(ch)) { octet = octet * 10 + (ch - '0'); } else if (ch == '.') { Validate.isTrue(octetCount < TOTAL_OCTETS); octetCount++; value = addOctet(value, octet); octet = 0; } else { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv4Address)); } } Validate.isTrue(octetCount == TOTAL_OCTETS); return new Ipv4(addOctet(value, octet)); } catch (Exception e) { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv4Address), e); } } protected  Ipv4(Long value); static Ipv4 of(BigInteger from); static Ipv4 of(Long value); static Ipv4 of(String value); static Ipv4 parse(String ipv4Address); @Override int compareTo(Ipv4 other); @Override String toString(); @Override Ipv4 next(); @Override Ipv4 previous(); @Override boolean hasNext(); @Override boolean hasPrevious(); @Override Ipv4Range asRange(); @Override int bitSize(); @Override BigInteger asBigInteger(); @Override Ipv4 lowerBoundForPrefix(int prefixLength); @Override Ipv4 upperBoundForPrefix(int prefixLength); @Override int getCommonPrefixLength(Ipv4 other); @Override boolean equals(Object o); @Override int hashCode(); }
Ipv4 extends AbstractIp<Ipv4, Ipv4Range> { public static Ipv4 parse(String ipv4Address) { try { String ipv4String = Validate.notNull(ipv4Address).trim(); Validate.isTrue(!ipv4String.isEmpty() && Character.isDigit(ipv4String.charAt(0)) && Character.isDigit(ipv4String.charAt(ipv4String.length() - 1))); Validate.isTrue(!LEADING_ZERO_IN_IPV4.matcher(ipv4String).find(), "IPv4 cannot have leading zeros, as this may be mistaken for octal numbers."); long value = 0; int octet = 0; int octetCount = 1; for (int i = 0; i < ipv4String.length(); ++i) { char ch = ipv4String.charAt(i); if (Character.isDigit(ch)) { octet = octet * 10 + (ch - '0'); } else if (ch == '.') { Validate.isTrue(octetCount < TOTAL_OCTETS); octetCount++; value = addOctet(value, octet); octet = 0; } else { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv4Address)); } } Validate.isTrue(octetCount == TOTAL_OCTETS); return new Ipv4(addOctet(value, octet)); } catch (Exception e) { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv4Address), e); } } protected  Ipv4(Long value); static Ipv4 of(BigInteger from); static Ipv4 of(Long value); static Ipv4 of(String value); static Ipv4 parse(String ipv4Address); @Override int compareTo(Ipv4 other); @Override String toString(); @Override Ipv4 next(); @Override Ipv4 previous(); @Override boolean hasNext(); @Override boolean hasPrevious(); @Override Ipv4Range asRange(); @Override int bitSize(); @Override BigInteger asBigInteger(); @Override Ipv4 lowerBoundForPrefix(int prefixLength); @Override Ipv4 upperBoundForPrefix(int prefixLength); @Override int getCommonPrefixLength(Ipv4 other); @Override boolean equals(Object o); @Override int hashCode(); static final int BYTE_MASK; static final int NUMBER_OF_BITS; static final long MINIMUM_VALUE; static final long MAXIMUM_VALUE; static final Ipv4 FIRST_IPV4_ADDRESS; static final Ipv4 LAST_IPV4_ADDRESS; }