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;
10 import static com.google.common.base.Preconditions.checkArgument;
14 import org.opendaylight.yangtools.yang.common.QName;
16 import com.google.common.base.Splitter;
17 import com.google.common.collect.ImmutableSet;
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 = "$YangModuleInfoImpl";
39 public static final String MODEL_BINDING_PROVIDER_CLASS_NAME = "$YangModelBindingProvider";
41 public static final String RPC_INPUT_SUFFIX = "Input";
42 public static final String RPC_OUTPUT_SUFFIX = "Output";
44 private BindingMapping() {
45 throw new UnsupportedOperationException("Utility class should not be instantiated");
48 public static final String getMethodName(final QName name) {
49 checkArgument(name != null, "Name should not be null.");
50 return getMethodName(name.getLocalName());
53 public static final String getClassName(final String localName) {
54 checkArgument(localName != null, "Name should not be null.");
55 return toFirstUpper(toCamelCase(localName));
58 public static final String getMethodName(final String yangIdentifier) {
59 checkArgument(yangIdentifier != null,"Identifier should not be null");
60 return toFirstLower(toCamelCase(yangIdentifier));
63 public static final String getClassName(final QName name) {
64 checkArgument(name != null, "Name should not be null.");
65 return toFirstUpper(toCamelCase(name.getLocalName()));
68 public static String getPropertyName(final String yangIdentifier) {
69 final String potential = toFirstLower(toCamelCase(yangIdentifier));
70 if("class".equals(potential)) {
76 private static final String toCamelCase(final String rawString) {
77 checkArgument(rawString != null, "String should not be null");
78 Iterable<String> components = SPACE_SPLITTER.split(rawString.replace('-', ' ').replace('_', ' ')
80 StringBuilder builder = new StringBuilder();
81 for (String comp : components) {
82 builder.append(toFirstUpper(comp));
84 return checkNumericPrefix(builder.toString());
87 private static final String checkNumericPrefix(final String rawString) {
88 if (rawString == null || rawString.isEmpty()) {
91 char firstChar = rawString.charAt(0);
92 if (firstChar >= '0' && firstChar <= '9') {
93 return "_" + rawString;
100 * Returns the {@link String} {@code s} with an
101 * {@link Character#isUpperCase(char) upper case} first character. This
102 * function is null-safe.
105 * the string that should get an upper case first character. May
106 * be <code>null</code>.
107 * @return the {@link String} {@code s} with an upper case first character
108 * or <code>null</code> if the input {@link String} {@code s} was
111 private static String toFirstUpper(final String s) {
112 if (s == null || s.length() == 0) {
115 if (Character.isUpperCase(s.charAt(0))) {
118 if (s.length() == 1) {
119 return s.toUpperCase();
121 return s.substring(0, 1).toUpperCase() + s.substring(1);
125 * Returns the {@link String} {@code s} with an
126 * {@link Character#isLowerCase(char) lower case} first character. This
127 * function is null-safe.
130 * the string that should get an lower case first character. May
131 * be <code>null</code>.
132 * @return the {@link String} {@code s} with an lower case first character
133 * or <code>null</code> if the input {@link String} {@code s} was
136 private static String toFirstLower(final String s) {
137 if (s == null || s.length() == 0) {
140 if (Character.isLowerCase(s.charAt(0))) {
143 if (s.length() == 1) {
144 return s.toLowerCase();
146 return s.substring(0, 1).toLowerCase() + s.substring(1);