Add @SafeVargs
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContextUtils.java
1 /*
2  * Copyright (c) 2015 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.parser.spi.meta;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Predicate;
12 import com.google.common.base.Splitter;
13 import java.util.Collection;
14 import java.util.LinkedHashSet;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
23
24 public final class StmtContextUtils {
25
26     public static final char LIST_KEY_SEPARATOR = ' ';
27     private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults();
28
29     private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
30             new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
31         @Override
32         public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
33             return input.buildDeclared();
34         }
35     };
36
37     private static final Function<StmtContext<?, ?,?>, EffectiveStatement<?,?>> BUILD_EFFECTIVE =
38             new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
39         @Override
40         public EffectiveStatement<?, ?> apply(final StmtContext<?, ?, ?> input) {
41             return input.buildEffective();
42         }
43     };
44
45     public static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE =
46             new Predicate<StmtContext<?,?,?>>() {
47         @Override
48         public boolean apply(final StmtContext<?, ?, ?> input) {
49             return input.isSupportedToBuildEffective();
50         }
51     };
52
53     private StmtContextUtils() {
54         throw new UnsupportedOperationException("Utility class");
55     }
56
57     @SuppressWarnings("unchecked")
58     public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
59         return Function.class.cast(BUILD_DECLARED);
60     }
61
62     @SuppressWarnings("unchecked")
63     public static <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
64         return Function.class.cast(BUILD_EFFECTIVE);
65     }
66
67     @SuppressWarnings("unchecked")
68     public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
69             final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<DT> declaredType) {
70         for (StmtContext<?, ?, ?> ctx : contexts) {
71             if (producesDeclared(ctx, declaredType)) {
72                 return (AT) ctx.getStatementArgument();
73             }
74         }
75         return null;
76     }
77
78     @SuppressWarnings("unchecked")
79     public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(final StmtContext<?, ?, ?> ctx,
80             final Class<DT> declaredType) {
81         return producesDeclared(ctx, declaredType) ? (AT) ctx.getStatementArgument() : null;
82     }
83
84     @SuppressWarnings("unchecked")
85     public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstDeclaredSubstatement(
86             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
87         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
88             if (producesDeclared(subStmtContext,declaredType)) {
89                 return (StmtContext<AT, ?, ?>) subStmtContext;
90             }
91         }
92         return null;
93     }
94
95     @SafeVarargs
96     public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
97             int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
98         if (startIndex >= types.length) {
99             return null;
100         }
101
102         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
103             if (producesDeclared(subStmtContext,types[startIndex])) {
104                 return startIndex + 1 == types.length ? subStmtContext
105                         : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
106             }
107         }
108         return null;
109     }
110
111     public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
112             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType, int sublevel) {
113         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
114             if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
115                 return subStmtContext;
116             }
117             if (sublevel > 1) {
118                 final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
119                     subStmtContext, declaredType, --sublevel);
120                 if (result != null) {
121                     return result;
122                 }
123             }
124         }
125
126         return null;
127     }
128
129     public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
130             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
131         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
132             if (producesDeclared(subStmtContext, declaredType)) {
133                 return subStmtContext;
134             }
135
136             final StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
137             if (result != null) {
138                 return result;
139             }
140         }
141
142         return null;
143     }
144
145     public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
146             final Class<? extends DeclaredStatement<?>> type) {
147         return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
148     }
149
150     public static boolean isInExtensionBody(final StmtContext<?,?,?> stmtCtx) {
151         StmtContext<?,?,?> current = stmtCtx;
152         while (!current.getParentContext().isRootContext()) {
153             current = current.getParentContext();
154             if (producesDeclared(current, UnknownStatementImpl.class)) {
155                 return true;
156             }
157         }
158
159         return false;
160     }
161
162     public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
163         return producesDeclared(stmtCtx, UnknownStatementImpl.class);
164     }
165
166     public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(
167             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
168             final QNameModule newQNameModule) {
169
170         Set<SchemaNodeIdentifier> newKeys = new LinkedHashSet<>();
171         for (String keyToken : KEY_SPLITTER.split(keyStmtCtx.rawStatementArgument())) {
172             final QName keyQName = QName.create(newQNameModule, keyToken);
173             newKeys.add(SchemaNodeIdentifier.create(false, keyQName));
174         }
175
176         return newKeys;
177     }
178 }