2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.binding;
12 import org.opendaylight.yangtools.yang.common.QName;
14 import com.google.common.base.Splitter;
15 import com.google.common.collect.ImmutableSet;
17 import static com.google.common.base.Preconditions.*;
19 public final class BindingMapping {
21 public static final String VERSION = "0.6";
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");
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";
36 private static final Splitter SPACE_SPLITTER = Splitter.on(" ").omitEmptyStrings().trimResults();
38 public static final String MODULE_INFO_CLASS_NAME = "$ModuleInfoImpl";
40 public static final String getMethodName(QName name) {
41 checkArgument(name != null, "Name should not be null.");
42 return getMethodName(name.getLocalName());
45 public static final String getClassName(String localName) {
46 checkArgument(localName != null, "Name should not be null.");
47 return toFirstUpper(toCamelCase(localName));
50 public static final String getMethodName(String yangIdentifier) {
51 checkArgument(yangIdentifier != null,"Identifier should not be null");
52 return toFirstLower(toCamelCase(yangIdentifier));
55 public static final String getClassName(QName name) {
56 checkArgument(name != null, "Name should not be null.");
57 return toFirstUpper(toCamelCase(name.getLocalName()));
60 public static String getPropertyName(String yangIdentifier) {
61 final String potential = toFirstLower(toCamelCase(yangIdentifier));
62 if("class".equals(potential)) {
68 private static final String toCamelCase(String rawString) {
69 checkArgument(rawString != null, "String should not be null");
70 Iterable<String> components = SPACE_SPLITTER.split(rawString.replace('-', ' ').replace('_', ' '));
71 StringBuilder builder = new StringBuilder();
72 for (String comp : components) {
73 builder.append(toFirstUpper(comp));
75 return builder.toString();
79 * Returns the {@link String} {@code s} with an
80 * {@link Character#isUpperCase(char) upper case} first character. This
81 * function is null-safe.
84 * the string that should get an upper case first character. May
85 * be <code>null</code>.
86 * @return the {@link String} {@code s} with an upper case first character
87 * or <code>null</code> if the input {@link String} {@code s} was
90 private static String toFirstUpper(String s) {
91 if (s == null || s.length() == 0)
93 if (Character.isUpperCase(s.charAt(0)))
96 return s.toUpperCase();
97 return s.substring(0, 1).toUpperCase() + s.substring(1);
101 * Returns the {@link String} {@code s} with an
102 * {@link Character#isLowerCase(char) lower case} first character. This
103 * function is null-safe.
106 * the string that should get an lower case first character. May
107 * be <code>null</code>.
108 * @return the {@link String} {@code s} with an lower case first character
109 * or <code>null</code> if the input {@link String} {@code s} was
112 private static String toFirstLower(String s) {
113 if (s == null || s.length() == 0)
115 if (Character.isLowerCase(s.charAt(0)))
118 return s.toLowerCase();
119 return s.substring(0, 1).toLowerCase() + s.substring(1);