Add @Nullable annotations on getters
[mdsal.git] / model / ietf / ietf-inet-types / src / main / java / org / opendaylight / yang / gen / v1 / urn / ietf / params / xml / ns / yang / ietf / inet / types / rev100924 / HostBuilder.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15 /**
16  **/
17 public final class HostBuilder {
18     private static final Pattern IPV4_PATTERN = Pattern.compile("(([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("((:|[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}]+)?");
20     private static final Pattern IPV6_PATTERN2 = Pattern.compile("(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)(%.+)?");
21     private static final Pattern DOMAIN_PATTERN = Pattern.compile("((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)|\\.");
22
23     private HostBuilder() {
24
25     }
26
27     public static Host getDefaultInstance(final String defaultValue) {
28         final Matcher ipv4Matcher = IPV4_PATTERN.matcher(defaultValue);
29         final Matcher ipv6Matcher1 = IPV6_PATTERN1.matcher(defaultValue);
30         final Matcher ipv6Matcher2 = IPV6_PATTERN2.matcher(defaultValue);
31         final Matcher domainMatcher = DOMAIN_PATTERN.matcher(defaultValue);
32
33         List<String> matchers = new ArrayList<>(3);
34         if (ipv6Matcher1.matches() || ipv6Matcher2.matches()) {
35             matchers.add(Ipv6Address.class.getSimpleName());
36         }
37
38         // Ipv4 and Domain Name patterns are not exclusive
39         // Address 127.0.0.1 matches both patterns
40         // This way Ipv4 address is preferred to domain name
41         if (ipv4Matcher.matches()) {
42             matchers.add(Ipv4Address.class.getSimpleName());
43         } else if (domainMatcher.matches()) {
44             matchers.add(DomainName.class.getSimpleName());
45         }
46
47         if (matchers.size() > 1) {
48             throw new IllegalArgumentException("Cannot create Host from " + defaultValue + ". Value is ambigious for "
49                     + matchers);
50         }
51
52         if (ipv4Matcher.matches()) {
53             Ipv4Address ipv4 = new Ipv4Address(defaultValue);
54             IpAddress ipAddress = new IpAddress(ipv4);
55             return new Host(ipAddress);
56         }
57         if (ipv6Matcher1.matches() || ipv6Matcher2.matches()) {
58             Ipv6Address ipv6 = new Ipv6Address(defaultValue);
59             IpAddress ipAddress = new IpAddress(ipv6);
60             return new Host(ipAddress);
61         }
62         if (domainMatcher.matches()) {
63             DomainName domainName = new DomainName(defaultValue);
64             return new Host(domainName);
65         }
66         throw new IllegalArgumentException("Cannot create Host from " + defaultValue);
67     }
68
69 }