Merge "BUG-648: add missing copy builders"
[yangtools.git] / yang / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / BindingMapping.java
1 /*
2  * Copyright (c) 2013 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.yangtools.yang.binding;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import java.util.Set;
13
14 import org.opendaylight.yangtools.yang.common.QName;
15
16 import com.google.common.base.Splitter;
17 import com.google.common.collect.ImmutableSet;
18
19 public final class BindingMapping {
20
21     public static final String VERSION = "0.6";
22
23     public static final Set<String> JAVA_RESERVED_WORDS = ImmutableSet.of("abstract", "assert", "boolean", "break",
24             "byte", "case", "catch", "char", "class", "const", "continue", "default", "double", "do", "else", "enum",
25             "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof",
26             "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return",
27             "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient",
28             "true", "try", "void", "volatile", "while");
29
30     public static final String DATA_ROOT_SUFFIX = "Data";
31     public static final String RPC_SERVICE_SUFFIX = "Service";
32     public static final String NOTIFICATION_LISTENER_SUFFIX = "Listener";
33     public static final String QNAME_STATIC_FIELD_NAME = "QNAME";
34     public static final String PACKAGE_PREFIX = "org.opendaylight.yang.gen.v1";
35
36     private static final Splitter SPACE_SPLITTER = Splitter.on(" ").omitEmptyStrings().trimResults();
37
38     public static final String MODULE_INFO_CLASS_NAME = "$YangModuleInfoImpl";
39     public static final String MODEL_BINDING_PROVIDER_CLASS_NAME = "$YangModelBindingProvider";
40
41     public static final String RPC_INPUT_SUFFIX = "Input";
42     public static final String RPC_OUTPUT_SUFFIX = "Output";
43
44     private BindingMapping() {
45         throw new UnsupportedOperationException("Utility class should not be instantiated");
46     }
47
48     public static final String getMethodName(final QName name) {
49         checkArgument(name != null, "Name should not be null.");
50         return getMethodName(name.getLocalName());
51     }
52
53     public static final String getClassName(final String localName) {
54         checkArgument(localName != null, "Name should not be null.");
55         return toFirstUpper(toCamelCase(localName));
56     }
57
58     public static final String getMethodName(final String yangIdentifier) {
59         checkArgument(yangIdentifier != null,"Identifier should not be null");
60         return toFirstLower(toCamelCase(yangIdentifier));
61     }
62
63     public static final String getClassName(final QName name) {
64         checkArgument(name != null, "Name should not be null.");
65         return toFirstUpper(toCamelCase(name.getLocalName()));
66     }
67
68     public static String getPropertyName(final String yangIdentifier) {
69         final String potential = toFirstLower(toCamelCase(yangIdentifier));
70         if("class".equals(potential)) {
71             return "xmlClass";
72         }
73         return potential;
74     }
75
76     private static final String toCamelCase(final String rawString) {
77         checkArgument(rawString != null, "String should not be null");
78         Iterable<String> components = SPACE_SPLITTER.split(rawString.replace('-', ' ').replace('_', ' ')
79                 .replace('.', ' '));
80         StringBuilder builder = new StringBuilder();
81         for (String comp : components) {
82             builder.append(toFirstUpper(comp));
83         }
84         return checkNumericPrefix(builder.toString());
85     }
86
87     private static final String checkNumericPrefix(final String rawString) {
88         if (rawString == null || rawString.isEmpty()) {
89             return rawString;
90         }
91         char firstChar = rawString.charAt(0);
92         if (firstChar >= '0' && firstChar <= '9') {
93             return "_" + rawString;
94         } else {
95             return rawString;
96         }
97     }
98
99     /**
100      * Returns the {@link String} {@code s} with an
101      * {@link Character#isUpperCase(char) upper case} first character. This
102      * function is null-safe.
103      *
104      * @param s
105      *            the string that should get an upper case first character. May
106      *            be <code>null</code>.
107      * @return the {@link String} {@code s} with an upper case first character
108      *         or <code>null</code> if the input {@link String} {@code s} was
109      *         <code>null</code>.
110      */
111     private static String toFirstUpper(final String s) {
112         if (s == null || s.length() == 0) {
113             return s;
114         }
115         if (Character.isUpperCase(s.charAt(0))) {
116             return s;
117         }
118         if (s.length() == 1) {
119             return s.toUpperCase();
120         }
121         return s.substring(0, 1).toUpperCase() + s.substring(1);
122     }
123
124     /**
125      * Returns the {@link String} {@code s} with an
126      * {@link Character#isLowerCase(char) lower case} first character. This
127      * function is null-safe.
128      *
129      * @param s
130      *            the string that should get an lower case first character. May
131      *            be <code>null</code>.
132      * @return the {@link String} {@code s} with an lower case first character
133      *         or <code>null</code> if the input {@link String} {@code s} was
134      *         <code>null</code>.
135      */
136     private static String toFirstLower(final String s) {
137         if (s == null || s.length() == 0) {
138             return s;
139         }
140         if (Character.isLowerCase(s.charAt(0))) {
141             return s;
142         }
143         if (s.length() == 1) {
144             return s.toLowerCase();
145         }
146         return s.substring(0, 1).toLowerCase() + s.substring(1);
147     }
148 }