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=77b285f16a9a3137dd17a1ac8b6fb3acee136515;hb=ea32c2c6fac2ebe9d0e30c9c4e5279c5ef0d2314;hp=eccbaca07d4bc14093b6a60b8b17f76e9c4280a5;hpb=30c67c4d780e897d972d60bb79d546b512516f90;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 eccbaca07d..77b285f16a 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,47 +8,38 @@ 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.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl; public final class StmtContextUtils { + public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults(); - public static final char LIST_KEY_SEPARATOR = ' '; - private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults(); - - private static final Function, DeclaredStatement> BUILD_DECLARED = new Function, DeclaredStatement>() { + private static final Function, DeclaredStatement> BUILD_DECLARED = + new Function, DeclaredStatement>() { @Override public DeclaredStatement apply(final StmtContext input) { return input.buildDeclared(); } }; - private static final Function, EffectiveStatement> BUILD_EFFECTIVE = new Function, EffectiveStatement>() { + private static final Function, EffectiveStatement> BUILD_EFFECTIVE = + new Function, EffectiveStatement>() { @Override public EffectiveStatement apply(final StmtContext input) { return input.buildEffective(); } }; - public static final Predicate> IS_SUPPORTED_TO_BUILD_EFFECTIVE = new Predicate>() { - @Override - public boolean apply(StmtContext input) { - return input.isSupportedToBuildEffective(); - } - }; - private StmtContextUtils() { throw new UnsupportedOperationException("Utility class"); } @@ -59,12 +50,12 @@ public final class StmtContextUtils { } @SuppressWarnings("unchecked") - public static final > Function, E> buildEffective() { + public static > Function, E> buildEffective() { return Function.class.cast(BUILD_EFFECTIVE); } @SuppressWarnings("unchecked") - public static final > AT firstAttributeOf( + public static > AT firstAttributeOf( final Iterable> contexts, final Class
declaredType) { for (StmtContext ctx : contexts) { if (producesDeclared(ctx, declaredType)) { @@ -75,94 +66,108 @@ public final class StmtContextUtils { } @SuppressWarnings("unchecked") - public static final > AT firstAttributeOf(final StmtContext ctx, + public static > AT firstAttributeOf(final StmtContext ctx, final Class
declaredType) { + return producesDeclared(ctx, declaredType) ? (AT) ctx.getStatementArgument() : null; + } - if (producesDeclared(ctx, declaredType)) { - return (AT) ctx.getStatementArgument(); + @SuppressWarnings("unchecked") + public static > StmtContext findFirstDeclaredSubstatement( + final StmtContext stmtContext, final Class
declaredType) { + for (StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (producesDeclared(subStmtContext,declaredType)) { + return (StmtContext) subStmtContext; + } } - return null; } @SuppressWarnings("unchecked") - public static final > StmtContext findFirstDeclaredSubstatement( - StmtContext stmtContext, Class
declaredType) { - Collection> declaredSubstatements = stmtContext.declaredSubstatements(); - for (StmtContext subStmtContext : declaredSubstatements) { - if (producesDeclared(subStmtContext, declaredType)) { + public static > Collection> findAllDeclaredSubstatement( + 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 > StmtContext findFirstEffectiveSubstatement( + final StmtContext stmtContext, final Class
declaredType) { + for (StmtContext subStmtContext : stmtContext.effectiveSubstatements()) { + if (producesDeclared(subStmtContext,declaredType)) { return (StmtContext) subStmtContext; } } return null; } - public static final StmtContext findFirstDeclaredSubstatement(final StmtContext stmtContext, - int startIndex, final Class>... types) { + 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) { - if (producesDeclared(subStmtContext, types[startIndex])) { - if (startIndex + 1 == types.length) { - return subStmtContext; - } else { - return findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types); - } + for (StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (producesDeclared(subStmtContext,types[startIndex])) { + return startIndex + 1 == types.length ? subStmtContext + : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types); } } return null; } - public static final
> StmtContext findFirstDeclaredSubstatementOnSublevel( + public static
> StmtContext findFirstDeclaredSubstatementOnSublevel( final StmtContext stmtContext, final Class
declaredType, int sublevel) { - Collection> declaredSubstatements = stmtContext.declaredSubstatements(); - for (StmtContext subStmtContext : declaredSubstatements) { + for (StmtContext subStmtContext : stmtContext.declaredSubstatements()) { if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) { return subStmtContext; - } else { - if (sublevel > 1) { - StmtContext result = findFirstDeclaredSubstatementOnSublevel(subStmtContext, declaredType, - --sublevel); - if (result != null) { - return result; - } + } + if (sublevel > 1) { + final StmtContext result = findFirstDeclaredSubstatementOnSublevel( + subStmtContext, declaredType, --sublevel); + if (result != null) { + return result; } } } + return null; } - public static final
> StmtContext findDeepFirstDeclaredSubstatement( + public static
> StmtContext findDeepFirstDeclaredSubstatement( final StmtContext stmtContext, final Class
declaredType) { - - Collection> declaredSubstatements = stmtContext.declaredSubstatements(); - - for (StmtContext subStmtContext : declaredSubstatements) { + for (StmtContext subStmtContext : stmtContext.declaredSubstatements()) { if (producesDeclared(subStmtContext, declaredType)) { return subStmtContext; - } else { - StmtContext result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType); - if (result != null) { - return result; - } + } + final StmtContext result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType); + if (result != null) { + return result; } } + return null; } - public static final boolean producesDeclared(final StmtContext ctx, + public static boolean producesDeclared(final StmtContext ctx, final Class> type) { return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass()); } - public static boolean isInExtensionBody(StmtContext stmtCtx) { - - StmtContext current = stmtCtx; + public static boolean isInExtensionBody(final StmtContext stmtCtx) { + StmtContext current = stmtCtx; while (!current.getParentContext().isRootContext()) { current = current.getParentContext(); if (producesDeclared(current, UnknownStatementImpl.class)) { @@ -173,36 +178,29 @@ public final class StmtContextUtils { return false; } - public static boolean isUnknownStatement(StmtContext stmtCtx) { + public static boolean isUnknownStatement(final StmtContext stmtCtx) { return producesDeclared(stmtCtx, UnknownStatementImpl.class); } - public static Set getCopyTypesFromOriginal(StmtContext ctx) { - - Set copyTypesFromOriginal = new HashSet<>(); - StmtContext current = ctx; - - while (current.getOriginalCtx() != null) { - copyTypesFromOriginal.add(current.getTypeOfCopy()); - current = current.getOriginalCtx(); - } - - return copyTypesFromOriginal; - } - public static Collection replaceModuleQNameForKey( - StmtContext, KeyStatement, ?> keyStmtCtx, QNameModule newQNameModule) { - - List keyTokens = KEY_SPLITTER.splitToList(keyStmtCtx.rawStatementArgument()); - - Set newKeys = new HashSet<>(); - - for (String keyToken : keyTokens) { - QName keyQName = QName.create(newQNameModule, keyToken); - SchemaNodeIdentifier keyIdentifier = SchemaNodeIdentifier.create(false, keyQName); - newKeys.add(keyIdentifier); + final StmtContext, KeyStatement, ?> keyStmtCtx, + final QNameModule newQNameModule) { + + 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(); } }