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