@Test public void shouldFailOnInternalSpace() { try { Ipv6.parse("2001:0000:1234: 0000:0000:C1C0:ABCD:0876"); fail(); } catch (IllegalArgumentException ignored) { } try { Ipv6.parse("2001:0000:1234:0000 :0000:C1C0:ABCD:0876"); fail(); } catch (IllegalArgumentException ignored) { } }
public static Ipv6 parse(final String ipv6Address) { try { String ipv6String = Validate.notNull(ipv6Address).trim(); Validate.isTrue(!ipv6String.isEmpty()); final boolean isIpv6AddressWithEmbeddedIpv4 = ipv6String.contains("."); if (isIpv6AddressWithEmbeddedIpv4) { ipv6String = getIpv6AddressWithIpv4SectionInIpv6Notation(ipv6String); } final int indexOfDoubleColons = ipv6String.indexOf("::"); final boolean isShortened = indexOfDoubleColons != -1; if (isShortened) { Validate.isTrue(indexOfDoubleColons == ipv6String.lastIndexOf("::")); ipv6String = expandMissingColons(ipv6String, indexOfDoubleColons); } final String[] split = ipv6String.split(COLON, TOTAL_OCTETS); Validate.isTrue(split.length == TOTAL_OCTETS); BigInteger ipv6value = BigInteger.ZERO; for (String part : split) { Validate.isTrue(part.length() <= MAX_PART_LENGTH); Validate.checkRange(Integer.parseInt(part, BITS_PER_PART), MIN_PART_VALUE, MAX_PART_VALUE); ipv6value = ipv6value.shiftLeft(BITS_PER_PART).add(new BigInteger(part, BITS_PER_PART)); } return new Ipv6(ipv6value); } catch (Exception e) { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv6Address), e); } }
Ipv6 extends AbstractIp<Ipv6, Ipv6Range> { public static Ipv6 parse(final String ipv6Address) { try { String ipv6String = Validate.notNull(ipv6Address).trim(); Validate.isTrue(!ipv6String.isEmpty()); final boolean isIpv6AddressWithEmbeddedIpv4 = ipv6String.contains("."); if (isIpv6AddressWithEmbeddedIpv4) { ipv6String = getIpv6AddressWithIpv4SectionInIpv6Notation(ipv6String); } final int indexOfDoubleColons = ipv6String.indexOf("::"); final boolean isShortened = indexOfDoubleColons != -1; if (isShortened) { Validate.isTrue(indexOfDoubleColons == ipv6String.lastIndexOf("::")); ipv6String = expandMissingColons(ipv6String, indexOfDoubleColons); } final String[] split = ipv6String.split(COLON, TOTAL_OCTETS); Validate.isTrue(split.length == TOTAL_OCTETS); BigInteger ipv6value = BigInteger.ZERO; for (String part : split) { Validate.isTrue(part.length() <= MAX_PART_LENGTH); Validate.checkRange(Integer.parseInt(part, BITS_PER_PART), MIN_PART_VALUE, MAX_PART_VALUE); ipv6value = ipv6value.shiftLeft(BITS_PER_PART).add(new BigInteger(part, BITS_PER_PART)); } return new Ipv6(ipv6value); } catch (Exception e) { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv6Address), e); } } }
Ipv6 extends AbstractIp<Ipv6, Ipv6Range> { public static Ipv6 parse(final String ipv6Address) { try { String ipv6String = Validate.notNull(ipv6Address).trim(); Validate.isTrue(!ipv6String.isEmpty()); final boolean isIpv6AddressWithEmbeddedIpv4 = ipv6String.contains("."); if (isIpv6AddressWithEmbeddedIpv4) { ipv6String = getIpv6AddressWithIpv4SectionInIpv6Notation(ipv6String); } final int indexOfDoubleColons = ipv6String.indexOf("::"); final boolean isShortened = indexOfDoubleColons != -1; if (isShortened) { Validate.isTrue(indexOfDoubleColons == ipv6String.lastIndexOf("::")); ipv6String = expandMissingColons(ipv6String, indexOfDoubleColons); } final String[] split = ipv6String.split(COLON, TOTAL_OCTETS); Validate.isTrue(split.length == TOTAL_OCTETS); BigInteger ipv6value = BigInteger.ZERO; for (String part : split) { Validate.isTrue(part.length() <= MAX_PART_LENGTH); Validate.checkRange(Integer.parseInt(part, BITS_PER_PART), MIN_PART_VALUE, MAX_PART_VALUE); ipv6value = ipv6value.shiftLeft(BITS_PER_PART).add(new BigInteger(part, BITS_PER_PART)); } return new Ipv6(ipv6value); } catch (Exception e) { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv6Address), e); } } protected  Ipv6(BigInteger value); }
Ipv6 extends AbstractIp<Ipv6, Ipv6Range> { public static Ipv6 parse(final String ipv6Address) { try { String ipv6String = Validate.notNull(ipv6Address).trim(); Validate.isTrue(!ipv6String.isEmpty()); final boolean isIpv6AddressWithEmbeddedIpv4 = ipv6String.contains("."); if (isIpv6AddressWithEmbeddedIpv4) { ipv6String = getIpv6AddressWithIpv4SectionInIpv6Notation(ipv6String); } final int indexOfDoubleColons = ipv6String.indexOf("::"); final boolean isShortened = indexOfDoubleColons != -1; if (isShortened) { Validate.isTrue(indexOfDoubleColons == ipv6String.lastIndexOf("::")); ipv6String = expandMissingColons(ipv6String, indexOfDoubleColons); } final String[] split = ipv6String.split(COLON, TOTAL_OCTETS); Validate.isTrue(split.length == TOTAL_OCTETS); BigInteger ipv6value = BigInteger.ZERO; for (String part : split) { Validate.isTrue(part.length() <= MAX_PART_LENGTH); Validate.checkRange(Integer.parseInt(part, BITS_PER_PART), MIN_PART_VALUE, MAX_PART_VALUE); ipv6value = ipv6value.shiftLeft(BITS_PER_PART).add(new BigInteger(part, BITS_PER_PART)); } return new Ipv6(ipv6value); } catch (Exception e) { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv6Address), e); } } protected  Ipv6(BigInteger value); static Ipv6 of(BigInteger value); static Ipv6 of(String value); @Override int compareTo(Ipv6 other); @Override Ipv6 next(); @Override Ipv6 previous(); @Override boolean hasNext(); @Override boolean hasPrevious(); @Override Ipv6Range asRange(); @Override String toString(); static Ipv6 parse(final String ipv6Address); @Override int bitSize(); @Override BigInteger asBigInteger(); @Override Ipv6 lowerBoundForPrefix(int prefixLength); @Override Ipv6 upperBoundForPrefix(int prefixLength); @Override int getCommonPrefixLength(Ipv6 other); @Override boolean equals(Object o); @Override int hashCode(); }
Ipv6 extends AbstractIp<Ipv6, Ipv6Range> { public static Ipv6 parse(final String ipv6Address) { try { String ipv6String = Validate.notNull(ipv6Address).trim(); Validate.isTrue(!ipv6String.isEmpty()); final boolean isIpv6AddressWithEmbeddedIpv4 = ipv6String.contains("."); if (isIpv6AddressWithEmbeddedIpv4) { ipv6String = getIpv6AddressWithIpv4SectionInIpv6Notation(ipv6String); } final int indexOfDoubleColons = ipv6String.indexOf("::"); final boolean isShortened = indexOfDoubleColons != -1; if (isShortened) { Validate.isTrue(indexOfDoubleColons == ipv6String.lastIndexOf("::")); ipv6String = expandMissingColons(ipv6String, indexOfDoubleColons); } final String[] split = ipv6String.split(COLON, TOTAL_OCTETS); Validate.isTrue(split.length == TOTAL_OCTETS); BigInteger ipv6value = BigInteger.ZERO; for (String part : split) { Validate.isTrue(part.length() <= MAX_PART_LENGTH); Validate.checkRange(Integer.parseInt(part, BITS_PER_PART), MIN_PART_VALUE, MAX_PART_VALUE); ipv6value = ipv6value.shiftLeft(BITS_PER_PART).add(new BigInteger(part, BITS_PER_PART)); } return new Ipv6(ipv6value); } catch (Exception e) { throw new IllegalArgumentException(String.format(DEFAULT_PARSING_ERROR_MESSAGE, ipv6Address), e); } } protected  Ipv6(BigInteger value); static Ipv6 of(BigInteger value); static Ipv6 of(String value); @Override int compareTo(Ipv6 other); @Override Ipv6 next(); @Override Ipv6 previous(); @Override boolean hasNext(); @Override boolean hasPrevious(); @Override Ipv6Range asRange(); @Override String toString(); static Ipv6 parse(final String ipv6Address); @Override int bitSize(); @Override BigInteger asBigInteger(); @Override Ipv6 lowerBoundForPrefix(int prefixLength); @Override Ipv6 upperBoundForPrefix(int prefixLength); @Override int getCommonPrefixLength(Ipv6 other); @Override boolean equals(Object o); @Override int hashCode(); static final BigInteger FOUR_OCTECT_MASK; static final int NUMBER_OF_BITS; static final BigInteger MINIMUM_VALUE; static final BigInteger MAXIMUM_VALUE; static final Ipv6 FIRST_IPV6_ADDRESS; static final Ipv6 LAST_IPV6_ADDRESS; }