2 package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715;
4 import java.util.regex.Matcher;
5 import java.util.regex.Pattern;
8 * * The purpose of generated class in src/main/java for Union types is to
9 * create * new instances of unions from a string representation. In some cases
10 * it is * very difficult to automate it since there can be unions such as
11 * (uint32 - * uint16), or (string - uint32). * * The reason behind putting it
12 * under src/main/java is: This class is generated * in form of a stub and needs
13 * to be finished by the user. This class is * generated only once to prevent
14 * loss of user code. *
16 public class IpAddressBuilder {
17 private static final Pattern IPV4_PATTERN = Pattern.compile(
18 "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(%[\\p{N}\\p{L}]+)?");
19 private static final Pattern IPV6_PATTERN1 = Pattern.compile(
20 "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))(%[\\p{N}\\p{L}]+)?");
21 private static final Pattern IPV6_PATTERN2 = Pattern
22 .compile("(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)(%.+)?");
24 private IpAddressBuilder() {
28 * * public static IpAddress getDefaultInstance(java.lang.String defaultValue) {
29 * * throw new java.lang.UnsupportedOperationException("Not yet implemented"); }
30 */ public static IpAddress getDefaultInstance(String defaultValue) {
31 final Matcher ipv4Matcher = IPV4_PATTERN.matcher(defaultValue);
32 if (ipv4Matcher.matches()) {
33 if (IPV6_PATTERN1.matcher(defaultValue).matches() && IPV6_PATTERN2.matcher(defaultValue).matches()) {
34 throw new IllegalArgumentException(
35 String.format("Cannot create IpAddress from \"%s\", matches both %s and %s", defaultValue,
36 Ipv4Address.class.getSimpleName(), Ipv6Address.class.getSimpleName()));
38 return new IpAddress(new Ipv4Address(defaultValue));
39 } else if (IPV6_PATTERN1.matcher(defaultValue).matches() && IPV6_PATTERN2.matcher(defaultValue).matches()) {
40 return new IpAddress(new Ipv6Address(defaultValue));
42 throw new IllegalArgumentException("Cannot create IpAddress from " + defaultValue);