Eliminate use of String.split()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / ValidationUtil.java
1 /*
2  * Copyright (c) 2013 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/eplv10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.impl;
9
10 import com.google.common.base.Splitter;
11
12 import java.util.HashSet;
13 import java.util.Set;
14
15 import org.antlr.v4.runtime.tree.ParseTree;
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_stmtContext;
17 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Submodule_stmtContext;
18 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
19
20 /**
21  * Validation utilities
22  */
23 final class ValidationUtil {
24     private static final Splitter SPACE_SPLITTER = Splitter.on(' ');
25
26     /**
27      * It isn't desirable to create instance of this class
28      */
29     private ValidationUtil() {
30     }
31
32     static void ex(final String message) {
33         throw new YangValidationException(message);
34     }
35
36     static Set<String> getDuplicates(final Iterable<String> keyList) {
37         Set<String> all = new HashSet<>();
38         Set<String> duplicates = new HashSet<>();
39
40         for (String key : keyList) {
41             if (!all.add(key)) {
42                 duplicates.add(key);
43             }
44         }
45         return duplicates;
46     }
47
48     static Iterable<String> listKeysFromId(final String keys) {
49         return SPACE_SPLITTER.split(keys);
50     }
51
52     static String getRootParentName(final ParseTree ctx) {
53         ParseTree root = getRootParent(ctx);
54         return ValidationUtil.getName(root);
55     }
56
57     private static ParseTree getRootParent(final ParseTree ctx) {
58         ParseTree root = ctx;
59         while (root.getParent() != null) {
60             if (root.getClass().equals(Module_stmtContext.class) || root.getClass().equals(Submodule_stmtContext.class)) {
61                 break;
62             }
63             root = root.getParent();
64         }
65         return root;
66     }
67
68     static String getName(final ParseTree child) {
69         return ParserListenerUtils.stringFromNode(child);
70     }
71
72     static String f(final String base, final Object... args) {
73         return String.format(base, args);
74     }
75
76     /**
77      * Get simple name from statement class e.g. Module from Module_stmt_context
78      */
79     static String getSimpleStatementName(final Class<? extends ParseTree> typeOfStatement) {
80
81         String className = typeOfStatement.getSimpleName();
82         int lastIndexOf = className.indexOf('$');
83         className = lastIndexOf == -1 ? className : className.substring(lastIndexOf + 1);
84         int indexOfStmt = className.indexOf("_stmt");
85         int index = indexOfStmt == -1 ? className.indexOf("_arg") : indexOfStmt;
86         return className.substring(0, index).replace('_', '-');
87     }
88
89     static int countPresentChildrenOfType(final ParseTree parent, final Set<Class<? extends ParseTree>> expectedChildTypes) {
90         int foundChildrenOfType = 0;
91
92         for (Class<? extends ParseTree> type : expectedChildTypes) {
93             foundChildrenOfType += countPresentChildrenOfType(parent, type);
94         }
95         return foundChildrenOfType;
96     }
97
98     static int countPresentChildrenOfType(final ParseTree parent, final Class<? extends ParseTree> expectedChildType) {
99         int foundChildrenOfType = 0;
100
101         for (int i = 0; i < parent.getChildCount(); i++) {
102             ParseTree child = parent.getChild(i);
103             if (expectedChildType.isInstance(child)) {
104                 foundChildrenOfType++;
105             }
106         }
107         return foundChildrenOfType;
108     }
109
110 }