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%2Fstmt%2Frfc6020%2Feffective%2FEffectiveStatementBase.java;h=7f55295465c586afec4228f49cc0ee55cf66480e;hb=f6eae2a11c570c1097eb9202debc7e36ce72ef6d;hp=cebfc0d924f04fd6f74d8ad5e8bf79969cb9e5d8;hpb=47b4850cc6a2cbe462f394f7afdb5b684ced1c22;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java index cebfc0d924..7f55295465 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the @@ -7,173 +7,136 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; +import com.google.common.base.Optional; +import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.Collections2; -import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; +import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.LinkedList; +import java.util.List; import java.util.Map; -import java.util.NoSuchElementException; import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; import org.opendaylight.yangtools.yang.model.api.SchemaNode; 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.meta.IdentifierNamespace; -import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition; -import org.opendaylight.yangtools.yang.model.api.meta.StatementSource; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase; -abstract public class EffectiveStatementBase> - implements EffectiveStatement { +public abstract class EffectiveStatementBase> implements EffectiveStatement { - private final StmtContext stmtCtx; - private final ImmutableList> substatements; - private final StatementSource statementSource; - private final StatementDefinition statementDefinition; - private D declaredInstance; + private static final Predicate> IS_UNKNOWN_STATEMENT_CONTEXT = + StmtContextUtils::isUnknownStatement; + private static final Predicate> IS_NOT_UNKNOWN_STATEMENT_CONTEXT = + Predicates.not(IS_UNKNOWN_STATEMENT_CONTEXT); - private final A argument; + private final List> substatements; + private final List> unknownSubstatementsToBuild; - public EffectiveStatementBase(final StmtContext ctx) { - - this.stmtCtx = ctx; - this.statementDefinition = ctx.getPublicDefinition(); - this.argument = ctx.getStatementArgument(); - this.statementSource = ctx.getStatementSource(); - - Collection> declaredSubstatements = ctx - .declaredSubstatements(); - Collection> effectiveSubstatements = ctx - .effectiveSubstatements(); - - Collection> substatementsInit = new LinkedList<>(); + protected EffectiveStatementBase(final StmtContext ctx) { + this(ctx, true); + } - for(StatementContextBase declaredSubstatement : declaredSubstatements) { - if(declaredSubstatement.getPublicDefinition() == Rfc6020Mapping.USES) { + /** + * Constructor. + * + * @param ctx + * context of statement. + * @param buildUnknownSubstatements + * if it is false, the unknown substatements are omitted from + * build of effective substatements till the call of either + * effectiveSubstatements or getOmittedUnknownSubstatements + * method. The main purpose of this is to allow the build of + * recursive extension definitions. + */ + protected EffectiveStatementBase(final StmtContext ctx, final boolean buildUnknownSubstatements) { + + final Collection> effectiveSubstatements = ctx.effectiveSubstatements(); + final Collection> substatementsInit = new ArrayList<>(); + + final Collection> supportedDeclaredSubStmts = Collections2.filter( + ctx.declaredSubstatements(), StmtContextUtils::areFeaturesSupported); + for (final StatementContextBase declaredSubstatement : supportedDeclaredSubStmts) { + if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) { substatementsInit.add(declaredSubstatement); substatementsInit.addAll(declaredSubstatement.getEffectOfStatement()); - ((StatementContextBase)ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement + ((StatementContextBase) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement .getEffectOfStatement()); } else { substatementsInit.add(declaredSubstatement); } } - substatementsInit.addAll(effectiveSubstatements); - this.substatements = FluentIterable.from(substatementsInit).filter(StmtContextUtils.IS_SUPPORTED_TO_BUILD_EFFECTIVE) - .transform(StmtContextUtils.buildEffective()).toList(); - } - - @Override - public StatementDefinition statementDefinition() { - return statementDefinition; - } + Collection> substatementsToBuild = Collections2.filter(substatementsInit, + StmtContext::isSupportedToBuildEffective); + if (!buildUnknownSubstatements) { + this.unknownSubstatementsToBuild = ImmutableList.copyOf(Collections2.filter(substatementsToBuild, + IS_UNKNOWN_STATEMENT_CONTEXT)); + substatementsToBuild = Collections2.filter(substatementsToBuild, IS_NOT_UNKNOWN_STATEMENT_CONTEXT); + } else { + this.unknownSubstatementsToBuild = ImmutableList.of(); + } - @Override - public A argument() { - return argument; + this.substatements = ImmutableList.copyOf(Collections2.transform(substatementsToBuild, StatementContextBase::buildEffective)); } - @Override - public StatementSource getStatementSource() { - return statementSource; + Collection> getOmittedUnknownSubstatements() { + return Collections2.transform(unknownSubstatementsToBuild, StatementContextBase::buildEffective); } @Override - public D getDeclared() { - if (declaredInstance == null) { - declaredInstance = stmtCtx.buildDeclared(); - } - return declaredInstance; + public final > V get(final Class namespace, final K identifier) { + throw new UnsupportedOperationException("Not implemented yet."); } @Override - public > V get( - final Class namespace, final K identifier) { - return stmtCtx.getFromNamespace(namespace, identifier); + public final > Map getAll(final Class namespace) { + throw new UnsupportedOperationException("Not implemented yet."); } @Override - public > Map getAll( - final Class namespace) { - return stmtCtx.getAllFromNamespace(namespace); - } - - @Override - public Collection> effectiveSubstatements() { - return substatements; - } - - public StmtContext getStatementContext() { - return stmtCtx; + public final Collection> effectiveSubstatements() { + if (unknownSubstatementsToBuild.isEmpty()) { + return substatements; + } else { + return ImmutableList.copyOf(Iterables.concat(substatements, getOmittedUnknownSubstatements())); + } } - protected final > S firstEffective( - final Class type) { - S result = null; - try { - result = type.cast(Iterables.find(substatements, - Predicates.instanceOf(type))); - } catch (NoSuchElementException e) { - result = null; - } - return result; + protected final > S firstEffective(final Class type) { + final Optional> possible = Iterables.tryFind(substatements, + Predicates.instanceOf(type)); + return possible.isPresent() ? type.cast(possible.get()) : null; } protected final S firstSchemaNode(final Class type) { - S result = null; - try { - result = type.cast(Iterables.find(substatements, - Predicates.instanceOf(type))); - } catch (NoSuchElementException e) { - result = null; - } - return result; + final Optional> possible = Iterables.tryFind(substatements, + Predicates.instanceOf(type)); + return possible.isPresent() ? type.cast(possible.get()) : null; } @SuppressWarnings("unchecked") - protected final Collection allSubstatementsOfType( - final Class type) { - Collection result = null; - - try { - result = Collection.class.cast(Collections2.filter(substatements, - Predicates.instanceOf(type))); - } catch (NoSuchElementException e) { - result = Collections.emptyList(); - } - return result; + protected final Collection allSubstatementsOfType(final Class type) { + return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type))); } protected final T firstSubstatementOfType(final Class type) { - T result = null; - try { - result = type.cast(Iterables.find(substatements, - Predicates.instanceOf(type))); - } catch (NoSuchElementException e) { - result = null; - } - return result; + final Optional> possible = Iterables.tryFind(substatements, + Predicates.instanceOf(type)); + return possible.isPresent() ? type.cast(possible.get()) : null; } - protected final R firstSubstatementOfType(final Class type, - final Class returnType) { - R result = null; - try { - result = returnType.cast(Iterables.find( - substatements, - Predicates.and(Predicates.instanceOf(type), - Predicates.instanceOf(returnType)))); - } catch (NoSuchElementException e) { - result = null; - } - return result; + protected final R firstSubstatementOfType(final Class type, final Class returnType) { + final Optional> possible = Iterables.tryFind(substatements, + Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType))); + return possible.isPresent() ? returnType.cast(possible.get()) : null; } + protected final EffectiveStatement firstEffectiveSubstatementOfType(final Class type) { + return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull(); + } }