package org.opendaylight.yangtools.yang.parser.spi.meta;
import com.google.common.base.Function;
-import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSet.Builder;
import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
public final class StmtContextUtils {
-
- public static final char LIST_KEY_SEPARATOR = ' ';
- private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults();
+ public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
}
};
- public static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE =
- new Predicate<StmtContext<?,?,?>>() {
- @Override
- public boolean apply(final StmtContext<?, ?, ?> input) {
- return input.isSupportedToBuildEffective();
- }
- };
-
private StmtContextUtils() {
throw new UnsupportedOperationException("Utility class");
}
return null;
}
+ @SuppressWarnings("unchecked")
+ public static <AT,DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllDeclaredSubstatement(
+ final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
+ ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
+ for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
+ if (producesDeclared(subStmtContext,declaredType)) {
+ listBuilder.add((StmtContext<AT, DT, ?>) subStmtContext);
+ }
+ }
+ return listBuilder.build();
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstEffectiveSubstatement(
+ final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
+ for (StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
+ if (producesDeclared(subStmtContext,declaredType)) {
+ return (StmtContext<AT, ?, ?>) subStmtContext;
+ }
+ }
+ return null;
+ }
+
+ public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstSubstatement(
+ final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
+ StmtContext<AT, ?, ?> declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType);
+ return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext, declaredType);
+ }
+
+ @SafeVarargs
public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
if (startIndex >= types.length) {
return null;
}
- Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext.declaredSubstatements();
- for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
+ for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
if (producesDeclared(subStmtContext,types[startIndex])) {
- if (startIndex + 1 == types.length) {
- return subStmtContext;
- } else {
- return findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
- }
+ return startIndex + 1 == types.length ? subStmtContext
+ : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
}
}
return null;
return subStmtContext;
}
if (sublevel > 1) {
- StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
+ final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
subStmtContext, declaredType, --sublevel);
if (result != null) {
return result;
return subStmtContext;
}
- StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
+ final StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
if (result != null) {
return result;
}
public static boolean isInExtensionBody(final StmtContext<?,?,?> stmtCtx) {
StmtContext<?,?,?> current = stmtCtx;
- while(!current.getParentContext().isRootContext()) {
+ while (!current.getParentContext().isRootContext()) {
current = current.getParentContext();
if (producesDeclared(current, UnknownStatementImpl.class)) {
return true;
final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
final QNameModule newQNameModule) {
- List<String> keyTokens = KEY_SPLITTER.splitToList(keyStmtCtx.rawStatementArgument());
- Set<SchemaNodeIdentifier> newKeys = new HashSet<>();
- for (String keyToken : keyTokens) {
- QName keyQName = QName.create(newQNameModule, keyToken);
- SchemaNodeIdentifier keyIdentifier = SchemaNodeIdentifier.create(false, keyQName);
- newKeys.add(keyIdentifier);
+ final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
+ boolean replaced = false;
+ for (SchemaNodeIdentifier arg : keyStmtCtx.getStatementArgument()) {
+ final QName qname = arg.getLastComponent();
+ if (!newQNameModule.equals(qname)) {
+ final QName newQname = keyStmtCtx.getFromNamespace(QNameCacheNamespace.class,
+ QName.create(newQNameModule, qname.getLocalName()));
+ builder.add(SchemaNodeIdentifier.create(false, newQname));
+ replaced = true;
+ } else {
+ builder.add(arg);
+ }
}
- return newKeys;
+ // This makes sure we reuse the collection when a grouping is instantiated in the same module
+ return replaced ? builder.build() : keyStmtCtx.getStatementArgument();
}
}