Fix various eclipse warnings
[mdsal.git] / binding / 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 import com.google.common.base.CharMatcher;
12 import com.google.common.base.Splitter;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.Interner;
15 import com.google.common.collect.Interners;
16 import java.text.SimpleDateFormat;
17 import java.util.Set;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22
23 public final class BindingMapping {
24
25     public static final String VERSION = "0.6";
26
27     public static final Set<String> JAVA_RESERVED_WORDS = ImmutableSet.of("abstract", "assert", "boolean", "break",
28             "byte", "case", "catch", "char", "class", "const", "continue", "default", "double", "do", "else", "enum",
29             "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof",
30             "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return",
31             "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient",
32             "true", "try", "void", "volatile", "while");
33
34     public static final String DATA_ROOT_SUFFIX = "Data";
35     public static final String RPC_SERVICE_SUFFIX = "Service";
36     public static final String NOTIFICATION_LISTENER_SUFFIX = "Listener";
37     public static final String QNAME_STATIC_FIELD_NAME = "QNAME";
38     public static final String PACKAGE_PREFIX = "org.opendaylight.yang.gen.v1";
39     public static final String AUGMENTATION_FIELD = "augmentation";
40
41     private static final Splitter CAMEL_SPLITTER = Splitter.on(CharMatcher.anyOf(" _.-").precomputed())
42             .omitEmptyStrings().trimResults();
43     private static final Pattern COLON_SLASH_SLASH = Pattern.compile("://", Pattern.LITERAL);
44     private static final String QUOTED_DOT = Matcher.quoteReplacement(".");
45     private static final Splitter DOT_SPLITTER = Splitter.on('.');
46
47     public static final String MODULE_INFO_CLASS_NAME = "$YangModuleInfoImpl";
48     public static final String MODEL_BINDING_PROVIDER_CLASS_NAME = "$YangModelBindingProvider";
49
50     public static final String RPC_INPUT_SUFFIX = "Input";
51     public static final String RPC_OUTPUT_SUFFIX = "Output";
52
53     private static final ThreadLocal<SimpleDateFormat> PACKAGE_DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
54
55         @Override
56         protected SimpleDateFormat initialValue() {
57             return new SimpleDateFormat("yyMMdd");
58         }
59
60         @Override
61         public void set(final SimpleDateFormat value) {
62             throw new UnsupportedOperationException();
63         }
64     };
65
66     private static final Interner<String> PACKAGE_INTERNER = Interners.newWeakInterner();
67
68     private BindingMapping() {
69         throw new UnsupportedOperationException("Utility class should not be instantiated");
70     }
71
72     public static String getRootPackageName(final QName module) {
73         return getRootPackageName(module.getModule());
74     }
75
76     public static String getRootPackageName(final QNameModule module) {
77         checkArgument(module != null, "Module must not be null");
78         checkArgument(module.getRevision() != null, "Revision must not be null");
79         checkArgument(module.getNamespace() != null, "Namespace must not be null");
80         final StringBuilder packageNameBuilder = new StringBuilder();
81
82         packageNameBuilder.append(BindingMapping.PACKAGE_PREFIX);
83         packageNameBuilder.append('.');
84
85         String namespace = module.getNamespace().toString();
86         namespace = COLON_SLASH_SLASH.matcher(namespace).replaceAll(QUOTED_DOT);
87
88         final char[] chars = namespace.toCharArray();
89         for (int i = 0; i < chars.length; ++i) {
90             switch (chars[i]) {
91             case '/':
92             case ':':
93             case '-':
94             case '@':
95             case '$':
96             case '#':
97             case '\'':
98             case '*':
99             case '+':
100             case ',':
101             case ';':
102             case '=':
103                 chars[i] = '.';
104             }
105         }
106
107         packageNameBuilder.append(chars);
108         packageNameBuilder.append(".rev");
109         packageNameBuilder.append(PACKAGE_DATE_FORMAT.get().format(module.getRevision()));
110         return normalizePackageName(packageNameBuilder.toString());
111     }
112
113     public static String normalizePackageName(final String packageName) {
114         if (packageName == null) {
115             return null;
116         }
117
118         final StringBuilder builder = new StringBuilder();
119         boolean first = true;
120
121         for (String p : DOT_SPLITTER.split(packageName.toLowerCase())) {
122             if (first) {
123                 first = false;
124             } else {
125                 builder.append('.');
126             }
127
128             if (Character.isDigit(p.charAt(0)) || BindingMapping.JAVA_RESERVED_WORDS.contains(p)) {
129                 builder.append('_');
130             }
131             builder.append(p);
132         }
133
134         // Prevent duplication of input string
135         return PACKAGE_INTERNER.intern(builder.toString());
136     }
137
138     public static String getMethodName(final QName name) {
139         checkArgument(name != null, "Name should not be null.");
140         return getMethodName(name.getLocalName());
141     }
142
143     public static String getClassName(final String localName) {
144         checkArgument(localName != null, "Name should not be null.");
145         return toFirstUpper(toCamelCase(localName));
146     }
147
148     public static String getMethodName(final String yangIdentifier) {
149         checkArgument(yangIdentifier != null,"Identifier should not be null");
150         return toFirstLower(toCamelCase(yangIdentifier));
151     }
152
153     public static String getClassName(final QName name) {
154         checkArgument(name != null, "Name should not be null.");
155         return toFirstUpper(toCamelCase(name.getLocalName()));
156     }
157
158     public static String getPropertyName(final String yangIdentifier) {
159         final String potential = toFirstLower(toCamelCase(yangIdentifier));
160         if ("class".equals(potential)) {
161             return "xmlClass";
162         }
163         return potential;
164     }
165
166     private static String toCamelCase(final String rawString) {
167         checkArgument(rawString != null, "String should not be null");
168         Iterable<String> components = CAMEL_SPLITTER.split(rawString);
169         StringBuilder builder = new StringBuilder();
170         for (String comp : components) {
171             builder.append(toFirstUpper(comp));
172         }
173         return checkNumericPrefix(builder.toString());
174     }
175
176     private static String checkNumericPrefix(final String rawString) {
177         if (rawString == null || rawString.isEmpty()) {
178             return rawString;
179         }
180         char firstChar = rawString.charAt(0);
181         if (firstChar >= '0' && firstChar <= '9') {
182             return "_" + rawString;
183         } else {
184             return rawString;
185         }
186     }
187
188     /**
189      * Returns the {@link String} {@code s} with an
190      * {@link Character#isUpperCase(char) upper case} first character. This
191      * function is null-safe.
192      *
193      * @param s
194      *            the string that should get an upper case first character. May
195      *            be <code>null</code>.
196      * @return the {@link String} {@code s} with an upper case first character
197      *         or <code>null</code> if the input {@link String} {@code s} was
198      *         <code>null</code>.
199      */
200     public static String toFirstUpper(final String s) {
201         if (s == null || s.length() == 0) {
202             return s;
203         }
204         if (Character.isUpperCase(s.charAt(0))) {
205             return s;
206         }
207         if (s.length() == 1) {
208             return s.toUpperCase();
209         }
210         return s.substring(0, 1).toUpperCase() + s.substring(1);
211     }
212
213     /**
214      * Returns the {@link String} {@code s} with an
215      * {@link Character#isLowerCase(char) lower case} first character. This
216      * function is null-safe.
217      *
218      * @param s
219      *            the string that should get an lower case first character. May
220      *            be <code>null</code>.
221      * @return the {@link String} {@code s} with an lower case first character
222      *         or <code>null</code> if the input {@link String} {@code s} was
223      *         <code>null</code>.
224      */
225     private static String toFirstLower(final String s) {
226         if (s == null || s.length() == 0) {
227             return s;
228         }
229         if (Character.isLowerCase(s.charAt(0))) {
230             return s;
231         }
232         if (s.length() == 1) {
233             return s.toLowerCase();
234         }
235         return s.substring(0, 1).toLowerCase() + s.substring(1);
236     }
237 }