Clean up TreeNode API
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / YangNames.java
1 /*
2  * Copyright (c) 2016 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.common;
9
10 import com.google.common.base.CharMatcher;
11 import java.util.AbstractMap.SimpleEntry;
12 import java.util.Map.Entry;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15
16 /**
17  * Utility class for handling various naming conventions mentioned in YANG and related specifications.
18  */
19 public final class YangNames {
20     /**
21      * A {@link CharMatcher} matching the first character of a YANG {@code identifier} ABNF production,
22      * {@code (ALPHA / "_")}.
23      */
24     public static final @NonNull CharMatcher IDENTIFIER_START =
25         CharMatcher.inRange('A', 'Z').or(CharMatcher.inRange('a', 'z').or(CharMatcher.is('_'))).precomputed();
26     /**
27      * A {@link CharMatcher} NOT matching second and later characters of a YANG {@code identifier} ABNF production,
28      * {@code (ALPHA / DIGIT / "_" / "-" / ".")}.
29      */
30     public static final @NonNull CharMatcher NOT_IDENTIFIER_PART =
31         IDENTIFIER_START.or(CharMatcher.inRange('0', '9')).or(CharMatcher.anyOf("-.")).negate().precomputed();
32
33     private YangNames() {
34         // Hidden on purpose
35     }
36
37     /**
38      * Parse a file name according to rules outlined in https://www.rfc-editor.org/rfc/rfc6020#section-5.2. Input string
39      * should be the base path with file extension stripped.
40      *
41      * @param baseName file base name
42      * @return A tuple containing the module name and parsed revision, if present.
43      * @throws NullPointerException if {@code baseName} is null
44      */
45     public static @NonNull Entry<@NonNull String, @Nullable String> parseFilename(final String baseName) {
46         final int zavinac = baseName.lastIndexOf('@');
47         if (zavinac < 0) {
48             return new SimpleEntry<>(baseName, null);
49         }
50
51         return new SimpleEntry<>(baseName.substring(0, zavinac), baseName.substring(zavinac + 1));
52     }
53 }