X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fspi%2Fmeta%2FStmtContextUtils.java;h=05b7f67c333ceab95ede69325755457f7db658c5;hb=17f092cc375493891b15139cc0f57584298ba18e;hp=9bc3fcbf8a3d2231026a9a4989d631e74a682f43;hpb=d0841bf7a4d70d1b9968fb08d3bf6c7829489cfe;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java index 9bc3fcbf8a..05b7f67c33 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java @@ -8,11 +8,11 @@ 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.Set; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; @@ -22,9 +22,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier; 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, DeclaredStatement> BUILD_DECLARED = new Function, DeclaredStatement>() { @@ -42,14 +40,6 @@ public final class StmtContextUtils { } }; - public static final Predicate> IS_SUPPORTED_TO_BUILD_EFFECTIVE = - new Predicate>() { - @Override - public boolean apply(final StmtContext input) { - return input.isSupportedToBuildEffective(); - } - }; - private StmtContextUtils() { throw new UnsupportedOperationException("Utility class"); } @@ -92,20 +82,66 @@ public final class StmtContextUtils { return null; } + @SuppressWarnings("unchecked") + public static > Collection> findAllDeclaredSubstatements( + final StmtContext stmtContext, final Class
declaredType) { + ImmutableList.Builder> listBuilder = ImmutableList.builder(); + for (StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (producesDeclared(subStmtContext, declaredType)) { + listBuilder.add((StmtContext) subStmtContext); + } + } + return listBuilder.build(); + } + + @SuppressWarnings("unchecked") + public static > Collection> findAllEffectiveSubstatements( + final StmtContext stmtContext, final Class
type) { + ImmutableList.Builder> listBuilder = ImmutableList.builder(); + for (StmtContext subStmtContext : stmtContext.effectiveSubstatements()) { + if (producesDeclared(subStmtContext, type)) { + listBuilder.add((StmtContext) subStmtContext); + } + } + return listBuilder.build(); + } + + public static > Collection> findAllSubstatements( + final StmtContext stmtContext, final Class
type) { + ImmutableList.Builder> listBuilder = ImmutableList.builder(); + listBuilder.addAll(findAllDeclaredSubstatements(stmtContext, type)); + listBuilder.addAll(findAllEffectiveSubstatements(stmtContext, type)); + return listBuilder.build(); + } + + @SuppressWarnings("unchecked") + public static > StmtContext findFirstEffectiveSubstatement( + final StmtContext stmtContext, final Class
declaredType) { + for (StmtContext subStmtContext : stmtContext.effectiveSubstatements()) { + if (producesDeclared(subStmtContext,declaredType)) { + return (StmtContext) subStmtContext; + } + } + return null; + } + + public static > StmtContext findFirstSubstatement( + final StmtContext stmtContext, final Class
declaredType) { + StmtContext declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType); + return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext, declaredType); + } + + @SafeVarargs public static StmtContext findFirstDeclaredSubstatement(final StmtContext stmtContext, int startIndex, final Class>... types) { if (startIndex >= types.length) { return null; } - Collection> 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; @@ -118,7 +154,7 @@ public final class StmtContextUtils { return subStmtContext; } if (sublevel > 1) { - StmtContext result = findFirstDeclaredSubstatementOnSublevel( + final StmtContext result = findFirstDeclaredSubstatementOnSublevel( subStmtContext, declaredType, --sublevel); if (result != null) { return result; @@ -136,7 +172,7 @@ public final class StmtContextUtils { return subStmtContext; } - StmtContext result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType); + final StmtContext result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType); if (result != null) { return result; } @@ -152,7 +188,7 @@ public final class StmtContextUtils { 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; @@ -170,13 +206,21 @@ public final class StmtContextUtils { final StmtContext, KeyStatement, ?> keyStmtCtx, final QNameModule newQNameModule) { - Set newKeys = new HashSet<>(); - for (String keyToken : KEY_SPLITTER.split(keyStmtCtx.rawStatementArgument())) { - QName keyQName = QName.create(newQNameModule, keyToken); - SchemaNodeIdentifier keyIdentifier = SchemaNodeIdentifier.create(false, keyQName); - newKeys.add(keyIdentifier); + final Builder 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(); } }