BUG-1276: fixed generated union constructor
[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 com.google.common.base.CharMatcher;
13 import com.google.common.base.Splitter;
14 import com.google.common.collect.ImmutableSet;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.QName;
17
18 public final class BindingMapping {
19
20     public static final String VERSION = "0.6";
21
22     public static final Set<String> JAVA_RESERVED_WORDS = ImmutableSet.of("abstract", "assert", "boolean", "break",
23             "byte", "case", "catch", "char", "class", "const", "continue", "default", "double", "do", "else", "enum",
24             "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof",
25             "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return",
26             "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient",
27             "true", "try", "void", "volatile", "while");
28
29     public static final String DATA_ROOT_SUFFIX = "Data";
30     public static final String RPC_SERVICE_SUFFIX = "Service";
31     public static final String NOTIFICATION_LISTENER_SUFFIX = "Listener";
32     public static final String QNAME_STATIC_FIELD_NAME = "QNAME";
33     public static final String PACKAGE_PREFIX = "org.opendaylight.yang.gen.v1";
34
35     private static final Splitter CAMEL_SPLITTER = Splitter.on(CharMatcher.anyOf(" _.-").precomputed())
36             .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 = CAMEL_SPLITTER.split(rawString);
79         StringBuilder builder = new StringBuilder();
80         for (String comp : components) {
81             builder.append(toFirstUpper(comp));
82         }
83         return checkNumericPrefix(builder.toString());
84     }
85
86     private static final String checkNumericPrefix(final String rawString) {
87         if (rawString == null || rawString.isEmpty()) {
88             return rawString;
89         }
90         char firstChar = rawString.charAt(0);
91         if (firstChar >= '0' && firstChar <= '9') {
92             return "_" + rawString;
93         } else {
94             return rawString;
95         }
96     }
97
98     /**
99      * Returns the {@link String} {@code s} with an
100      * {@link Character#isUpperCase(char) upper case} first character. This
101      * function is null-safe.
102      *
103      * @param s
104      *            the string that should get an upper case first character. May
105      *            be <code>null</code>.
106      * @return the {@link String} {@code s} with an upper case first character
107      *         or <code>null</code> if the input {@link String} {@code s} was
108      *         <code>null</code>.
109      */
110     public static String toFirstUpper(final String s) {
111         if (s == null || s.length() == 0) {
112             return s;
113         }
114         if (Character.isUpperCase(s.charAt(0))) {
115             return s;
116         }
117         if (s.length() == 1) {
118             return s.toUpperCase();
119         }
120         return s.substring(0, 1).toUpperCase() + s.substring(1);
121     }
122
123     /**
124      * Returns the {@link String} {@code s} with an
125      * {@link Character#isLowerCase(char) lower case} first character. This
126      * function is null-safe.
127      *
128      * @param s
129      *            the string that should get an lower case first character. May
130      *            be <code>null</code>.
131      * @return the {@link String} {@code s} with an lower case first character
132      *         or <code>null</code> if the input {@link String} {@code s} was
133      *         <code>null</code>.
134      */
135     private static String toFirstLower(final String s) {
136         if (s == null || s.length() == 0) {
137             return s;
138         }
139         if (Character.isLowerCase(s.charAt(0))) {
140             return s;
141         }
142         if (s.length() == 1) {
143             return s.toLowerCase();
144         }
145         return s.substring(0, 1).toLowerCase() + s.substring(1);
146     }
147 }