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;
12 import com.google.common.base.CharMatcher;
13 import com.google.common.base.Splitter;
14 import com.google.common.collect.ImmutableSet;
16 import org.opendaylight.yangtools.yang.common.QName;
18 public final class BindingMapping {
20 public static final String VERSION = "0.6";
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");
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 public static final String PACKAGE_PREFIX = "org.opendaylight.yang.gen.v1";
35 private static final Splitter CAMEL_SPLITTER = Splitter.on(CharMatcher.anyOf(" _.-").precomputed())
36 .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 = CAMEL_SPLITTER.split(rawString);
79 StringBuilder builder = new StringBuilder();
80 for (String comp : components) {
81 builder.append(toFirstUpper(comp));
83 return checkNumericPrefix(builder.toString());
86 private static final String checkNumericPrefix(final String rawString) {
87 if (rawString == null || rawString.isEmpty()) {
90 char firstChar = rawString.charAt(0);
91 if (firstChar >= '0' && firstChar <= '9') {
92 return "_" + rawString;
99 * Returns the {@link String} {@code s} with an
100 * {@link Character#isUpperCase(char) upper case} first character. This
101 * function is null-safe.
104 * the string that should get an upper case first character. May
105 * be <code>null</code>.
106 * @return the {@link String} {@code s} with an upper case first character
107 * or <code>null</code> if the input {@link String} {@code s} was
110 public static String toFirstUpper(final String s) {
111 if (s == null || s.length() == 0) {
114 if (Character.isUpperCase(s.charAt(0))) {
117 if (s.length() == 1) {
118 return s.toUpperCase();
120 return s.substring(0, 1).toUpperCase() + s.substring(1);
124 * Returns the {@link String} {@code s} with an
125 * {@link Character#isLowerCase(char) lower case} first character. This
126 * function is null-safe.
129 * the string that should get an lower case first character. May
130 * be <code>null</code>.
131 * @return the {@link String} {@code s} with an lower case first character
132 * or <code>null</code> if the input {@link String} {@code s} was
135 private static String toFirstLower(final String s) {
136 if (s == null || s.length() == 0) {
139 if (Character.isLowerCase(s.charAt(0))) {
142 if (s.length() == 1) {
143 return s.toLowerCase();
145 return s.substring(0, 1).toLowerCase() + s.substring(1);