Added headers to the concepts bundles
[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 java.util.Set;
11
12 import org.opendaylight.yangtools.yang.common.QName;
13
14 import com.google.common.base.Splitter;
15 import com.google.common.collect.ImmutableSet;
16 import static com.google.common.base.Preconditions.*;
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
34     private static final Splitter SPACE_SPLITTER = Splitter.on(" ").omitEmptyStrings().trimResults();
35
36     public static final String getMethodName(QName name) {
37         checkArgument(name != null, "Name should not be null.");
38         return toFirstLower(toCamelCase(name.getLocalName()));
39     }
40
41     public static final String getClassName(String localName) {
42         return toFirstUpper(toCamelCase(localName));
43     }
44
45     public static final String getClassName(QName name) {
46         checkArgument(name != null, "Name should not be null.");
47         return toFirstUpper(toCamelCase(name.getLocalName()));
48     }
49
50     private static final String toCamelCase(String rawString) {
51         checkArgument(rawString != null, "String should not be null");
52         Iterable<String> components = SPACE_SPLITTER.split(rawString.replace('-', ' ').replace('_', ' '));
53         StringBuilder builder = new StringBuilder();
54         for (String comp : components) {
55             builder.append(toFirstUpper(comp));
56         }
57         return builder.toString();
58     }
59
60     /**
61      * Returns the {@link String} {@code s} with an
62      * {@link Character#isUpperCase(char) upper case} first character. This
63      * function is null-safe.
64      * 
65      * @param s
66      *            the string that should get an upper case first character. May
67      *            be <code>null</code>.
68      * @return the {@link String} {@code s} with an upper case first character
69      *         or <code>null</code> if the input {@link String} {@code s} was
70      *         <code>null</code>.
71      */
72     private static String toFirstUpper(String s) {
73         if (s == null || s.length() == 0)
74             return s;
75         if (Character.isUpperCase(s.charAt(0)))
76             return s;
77         if (s.length() == 1)
78             return s.toUpperCase();
79         return s.substring(0, 1).toUpperCase() + s.substring(1);
80     }
81
82     /**
83      * Returns the {@link String} {@code s} with an
84      * {@link Character#isLowerCase(char) lower case} first character. This
85      * function is null-safe.
86      * 
87      * @param s
88      *            the string that should get an lower case first character. May
89      *            be <code>null</code>.
90      * @return the {@link String} {@code s} with an lower case first character
91      *         or <code>null</code> if the input {@link String} {@code s} was
92      *         <code>null</code>.
93      */
94     private static String toFirstLower(String s) {
95         if (s == null || s.length() == 0)
96             return s;
97         if (Character.isLowerCase(s.charAt(0)))
98             return s;
99         if (s.length() == 1)
100             return s.toLowerCase();
101         return s.substring(0, 1).toLowerCase() + s.substring(1);
102     }
103 }