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;
18 import org.opendaylight.yangtools.yang.common.QName;
20 public final class BindingMapping {
22 public static final String VERSION = "0.6";
24 public static final Set<String> JAVA_RESERVED_WORDS = ImmutableSet.of("abstract", "assert", "boolean", "break",
25 "byte", "case", "catch", "char", "class", "const", "continue", "default", "double", "do", "else", "enum",
26 "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof",
27 "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return",
28 "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient",
29 "true", "try", "void", "volatile", "while");
31 public static final String DATA_ROOT_SUFFIX = "Data";
32 public static final String RPC_SERVICE_SUFFIX = "Service";
33 public static final String NOTIFICATION_LISTENER_SUFFIX = "Listener";
34 public static final String QNAME_STATIC_FIELD_NAME = "QNAME";
35 public static final String PACKAGE_PREFIX = "org.opendaylight.yang.gen.v1";
37 private static final Splitter CAMEL_SPLITTER = Splitter.on(CharMatcher.anyOf(" _.-").precomputed())
38 .omitEmptyStrings().trimResults();
40 public static final String MODULE_INFO_CLASS_NAME = "$YangModuleInfoImpl";
41 public static final String MODEL_BINDING_PROVIDER_CLASS_NAME = "$YangModelBindingProvider";
43 public static final String RPC_INPUT_SUFFIX = "Input";
44 public static final String RPC_OUTPUT_SUFFIX = "Output";
46 private BindingMapping() {
47 throw new UnsupportedOperationException("Utility class should not be instantiated");
50 public static final String getMethodName(final QName name) {
51 checkArgument(name != null, "Name should not be null.");
52 return getMethodName(name.getLocalName());
55 public static final String getClassName(final String localName) {
56 checkArgument(localName != null, "Name should not be null.");
57 return toFirstUpper(toCamelCase(localName));
60 public static final String getMethodName(final String yangIdentifier) {
61 checkArgument(yangIdentifier != null,"Identifier should not be null");
62 return toFirstLower(toCamelCase(yangIdentifier));
65 public static final String getClassName(final QName name) {
66 checkArgument(name != null, "Name should not be null.");
67 return toFirstUpper(toCamelCase(name.getLocalName()));
70 public static String getPropertyName(final String yangIdentifier) {
71 final String potential = toFirstLower(toCamelCase(yangIdentifier));
72 if("class".equals(potential)) {
78 private static final String toCamelCase(final String rawString) {
79 checkArgument(rawString != null, "String should not be null");
80 Iterable<String> components = CAMEL_SPLITTER.split(rawString);
81 StringBuilder builder = new StringBuilder();
82 for (String comp : components) {
83 builder.append(toFirstUpper(comp));
85 return checkNumericPrefix(builder.toString());
88 private static final String checkNumericPrefix(final String rawString) {
89 if (rawString == null || rawString.isEmpty()) {
92 char firstChar = rawString.charAt(0);
93 if (firstChar >= '0' && firstChar <= '9') {
94 return "_" + rawString;
101 * Returns the {@link String} {@code s} with an
102 * {@link Character#isUpperCase(char) upper case} first character. This
103 * function is null-safe.
106 * the string that should get an upper case first character. May
107 * be <code>null</code>.
108 * @return the {@link String} {@code s} with an upper case first character
109 * or <code>null</code> if the input {@link String} {@code s} was
112 private static String toFirstUpper(final String s) {
113 if (s == null || s.length() == 0) {
116 if (Character.isUpperCase(s.charAt(0))) {
119 if (s.length() == 1) {
120 return s.toUpperCase();
122 return s.substring(0, 1).toUpperCase() + s.substring(1);
126 * Returns the {@link String} {@code s} with an
127 * {@link Character#isLowerCase(char) lower case} first character. This
128 * function is null-safe.
131 * the string that should get an lower case first character. May
132 * be <code>null</code>.
133 * @return the {@link String} {@code s} with an lower case first character
134 * or <code>null</code> if the input {@link String} {@code s} was
137 private static String toFirstLower(final String s) {
138 if (s == null || s.length() == 0) {
141 if (Character.isLowerCase(s.charAt(0))) {
144 if (s.length() == 1) {
145 return s.toLowerCase();
147 return s.substring(0, 1).toLowerCase() + s.substring(1);