*/
package org.opendaylight.yangtools.yang.model.api.meta;
+import com.google.common.annotations.Beta;
import java.util.Collection;
import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
* @return collection of all effective substatements.
*/
@Nonnull Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements();
+
+ /**
+ * Find the first effective substatement of specified type.
+ *
+ * @return First effective substatement, or empty if no match is found.
+ */
+ @Beta
+ default <T extends EffectiveStatement<?, ?>> Optional<T> findFirstEffectiveSubstatement(
+ @Nonnull final Class<T> type) {
+ return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast);
+ }
+
+ /**
+ * Find the first effective substatement of specified type and return its value.
+ *
+ * @return First effective substatement's argument, or empty if no match is found.
+ */
+ @Beta
+ default <V, T extends EffectiveStatement<V, ?>> Optional<V> findFirstEffectiveSubstatementArgument(
+ @Nonnull final Class<T> type) {
+ return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast)
+ .map(EffectiveStatement::argument);
+ }
+
+ /**
+ * Find all effective substatements of specified type and return them as a stream.
+ *
+ * @return A stream of all effective substatements of specified type.
+ */
+ @Beta
+ default <T extends EffectiveStatement<?, ?>> Stream<T> streamEffectiveSubstatements(@Nonnull final Class<T> type) {
+ return effectiveSubstatements().stream().filter(type::isInstance).map(type::cast);
+ }
}
protected AbstractEffectiveDataSchemaNode(final StmtContext<QName, D, ?> ctx) {
super(ctx);
this.configuration = ctx.isConfiguration();
-
- final WhenEffectiveStatement whenStmt = firstEffective(WhenEffectiveStatement.class);
- whenCondition = whenStmt != null ? whenStmt.argument() : null;
+ whenCondition = findFirstEffectiveSubstatementArgument(WhenEffectiveStatement.class).orElse(null);
// initCopyType
final CopyHistory originalHistory = ctx.getCopyHistory();
*/
protected AbstractEffectiveDocumentedNode(final StmtContext<A, D, ?> ctx) {
super(ctx);
-
- final DescriptionEffectiveStatement descStmt = firstEffective(DescriptionEffectiveStatement.class);
- if (descStmt != null) {
- description = descStmt.argument();
- } else {
- description = null;
- }
-
- final ReferenceEffectiveStatement refStmt = firstEffective(ReferenceEffectiveStatement.class);
- if (refStmt != null) {
- reference = refStmt.argument();
- } else {
- reference = null;
- }
-
- final StatusEffectiveStatement statusStmt = firstEffective(StatusEffectiveStatement.class);
- if (statusStmt != null) {
- status = statusStmt.argument();
- } else {
- status = Status.CURRENT;
- }
+ description = findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class).orElse(null);
+ reference = findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class).orElse(null);
+ status = findFirstEffectiveSubstatementArgument(StatusEffectiveStatement.class).orElse(Status.CURRENT);
}
@Override
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.concepts.SemVer;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
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.BelongsToEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.ContactEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.OrganizationEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.YangVersionEffectiveStatement;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.belongs_to.BelongsToEffectiveStatementImpl;
import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.extension.ExtensionEffectiveStatementImpl;
import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.submodule.SubmoduleEffectiveStatementImpl;
import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
this.name = argument();
- EffectiveStatementBase<?, ?> parentOfPrefix = this;
+ final EffectiveStatement<?, ?> parentOfPrefix;
if (ctx.getPublicDefinition() == YangStmtMapping.SUBMODULE) {
- parentOfPrefix = firstEffective(BelongsToEffectiveStatementImpl.class);
- SourceException.throwIfNull(parentOfPrefix, ctx.getStatementSourceReference(),
+ final Optional<BelongsToEffectiveStatement> optParent =
+ findFirstEffectiveSubstatement(BelongsToEffectiveStatement.class);
+ SourceException.throwIf(!optParent.isPresent(), ctx.getStatementSourceReference(),
"Unable to find belongs-to statement in submodule %s.", ctx.getStatementArgument());
+ parentOfPrefix = optParent.get();
+ } else {
+ parentOfPrefix = this;
}
- final PrefixEffectiveStatement prefixStmt = parentOfPrefix.firstEffective(PrefixEffectiveStatement.class);
- SourceException.throwIfNull(prefixStmt, ctx.getStatementSourceReference(),
+ final Optional<@NonNull PrefixEffectiveStatement> prefixStmt = parentOfPrefix.findFirstEffectiveSubstatement(
+ PrefixEffectiveStatement.class);
+ SourceException.throwIf(!prefixStmt.isPresent(), ctx.getStatementSourceReference(),
"Unable to resolve prefix for module or submodule %s.", ctx.getStatementArgument());
- this.prefix = prefixStmt.argument();
-
- final YangVersionEffectiveStatement yangVersionStmt = firstEffective(YangVersionEffectiveStatement.class);
- this.yangVersion = yangVersionStmt == null ? YangVersion.VERSION_1 : yangVersionStmt.argument();
-
- final OpenconfigVersionEffectiveStatement semanticVersionStmt =
- firstEffective(OpenconfigVersionEffectiveStatement.class);
- this.semanticVersion = semanticVersionStmt == null ? null : semanticVersionStmt.argument();
-
- final OrganizationEffectiveStatement organizationStmt = firstEffective(OrganizationEffectiveStatement.class);
- this.organization = organizationStmt == null ? null : organizationStmt.argument();
-
- final ContactEffectiveStatement contactStmt = firstEffective(ContactEffectiveStatement.class);
- this.contact = contactStmt == null ? null : contactStmt.argument();
+ this.prefix = prefixStmt.get().argument();
+ this.yangVersion = findFirstEffectiveSubstatementArgument(YangVersionEffectiveStatement.class)
+ .orElse(YangVersion.VERSION_1);
+ this.semanticVersion = findFirstEffectiveSubstatementArgument(OpenconfigVersionEffectiveStatement.class)
+ .orElse(null);
+ this.organization = findFirstEffectiveSubstatementArgument(OrganizationEffectiveStatement.class)
+ .orElse(null);
+ this.contact = findFirstEffectiveSubstatementArgument(ContactEffectiveStatement.class)
+ .orElse(null);
// init submodules and substatements of submodules
final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
this.path = ctx.getSchemaPath().get();
this.configuration = ctx.isConfiguration();
- final WhenEffectiveStatement whenStmt = firstEffective(WhenEffectiveStatement.class);
- whenCondition = whenStmt != null ? whenStmt.argument() : null;
+ whenCondition = findFirstEffectiveSubstatementArgument(WhenEffectiveStatement.class).orElse(null);
// initSubstatementCollectionsAndFields
import java.util.Map;
import java.util.function.Predicate;
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
return substatements;
}
+ /**
+ * Find first substatement of specified type.
+ *
+ * @param type Requested type
+ * @return First matching substatement, or null if no match is found.
+ *
+ * @deprecated Use {@link #findFirstEffectiveSubstatement(Class)} instead.
+ */
+ @Deprecated
public final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
- return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
+ return findFirstEffectiveSubstatement(type).orElse(null);
}
protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
return Collection.class.cast(Collections2.filter(substatements, type::isInstance));
}
- protected final <T> T firstSubstatementOfType(final Class<T> type) {
+ @Nullable protected final <T> T firstSubstatementOfType(final Class<T> type) {
return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
}
effectiveStatement.argument());
}
- public static Optional<ElementCountConstraint> createElementCountConstraint(
- final EffectiveStatementBase<?, ?> stmt) {
- final MinElementsEffectiveStatement firstMinElementsStmt = stmt.firstEffective(
- MinElementsEffectiveStatement.class);
+ public static Optional<ElementCountConstraint> createElementCountConstraint(final EffectiveStatement<?, ?> stmt) {
final Integer minElements;
- if (firstMinElementsStmt != null) {
- final Integer m = firstMinElementsStmt.argument();
+ final Optional<Integer> min = stmt.findFirstEffectiveSubstatementArgument(MinElementsEffectiveStatement.class);
+ if (min.isPresent()) {
+ final Integer m = min.get();
minElements = m > 0 ? m : null;
} else {
minElements = null;
}
- final MaxElementsEffectiveStatement firstMaxElementsStmt = stmt.firstEffective(
- MaxElementsEffectiveStatement.class);
- final String maxElementsArg = firstMaxElementsStmt == null ? UNBOUNDED_STR : firstMaxElementsStmt.argument();
final Integer maxElements;
- if (!UNBOUNDED_STR.equals(maxElementsArg)) {
- final Integer m = Integer.valueOf(maxElementsArg);
+ final String max = stmt.findFirstEffectiveSubstatementArgument(MaxElementsEffectiveStatement.class)
+ .orElse(UNBOUNDED_STR);
+ if (!UNBOUNDED_STR.equals(max)) {
+ final Integer m = Integer.valueOf(max);
maxElements = m < Integer.MAX_VALUE ? m : null;
} else {
maxElements = null;
final StmtContext<QName, AnydataStatement, EffectiveStatement<QName, AnydataStatement>> ctx) {
super(ctx);
this.original = (AnyDataSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
- final MandatoryEffectiveStatement mandatoryStmt = firstEffective(MandatoryEffectiveStatement.class);
- mandatory = mandatoryStmt == null ? false : mandatoryStmt.argument().booleanValue();
+ mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
+ .booleanValue();
mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
/*
final StmtContext<QName, AnyxmlStatement, EffectiveStatement<QName, AnyxmlStatement>> ctx) {
super(ctx);
this.original = (AnyXmlSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
- final MandatoryEffectiveStatement mandatoryStmt = firstEffective(MandatoryEffectiveStatement.class);
- mandatory = mandatoryStmt == null ? false : mandatoryStmt.argument().booleanValue();
+ mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
+ .booleanValue();
mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
}
-/**
+/*
* Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-// FIXME: hide this class
-public final class ArgumentEffectiveStatementImpl extends DeclaredEffectiveStatementBase<QName, ArgumentStatement>
+final class ArgumentEffectiveStatementImpl extends DeclaredEffectiveStatementBase<QName, ArgumentStatement>
implements ArgumentEffectiveStatement {
ArgumentEffectiveStatementImpl(final StmtContext<QName, ArgumentStatement, ?> ctx) {
super(ctx);
this.revision = rootModuleQName.getRevision().orElse(null);
this.copyOf = (AugmentationSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
-
- final WhenEffectiveStatement whenStmt = firstEffective(WhenEffectiveStatement.class);
- this.whenCondition = whenStmt == null ? null : whenStmt.argument();
+ whenCondition = findFirstEffectiveSubstatementArgument(WhenEffectiveStatement.class).orElse(null);
// initSubstatementCollections
final ImmutableSet.Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-// FIXME: hide this class
-public final class BelongsToEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, BelongsToStatement>
+final class BelongsToEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, BelongsToStatement>
implements BelongsToEffectiveStatement {
BelongsToEffectiveStatementImpl(final StmtContext<String, BelongsToStatement, ?> ctx) {
super(ctx);
this.augmentations = ImmutableSet.copyOf(augmentationsInit);
this.cases = ImmutableSortedMap.copyOfSorted(casesInit);
- final DefaultEffectiveStatement defaultStmt = firstEffective(DefaultEffectiveStatement.class);
- if (defaultStmt != null) {
+ final Optional<String> defaultArg = findFirstEffectiveSubstatementArgument(DefaultEffectiveStatement.class);
+ if (defaultArg.isPresent()) {
+ final String arg = defaultArg.get();
final QName qname;
try {
- qname = QName.create(getQName(), defaultStmt.argument());
+ qname = QName.create(getQName(), arg);
} catch (IllegalArgumentException e) {
throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
- defaultStmt.argument(), e);
+ arg, e);
}
// FIXME: this does not work with submodules, as they are
defaultCase = null;
}
- final MandatoryEffectiveStatement mandatoryStmt = firstEffective(MandatoryEffectiveStatement.class);
- mandatory = mandatoryStmt == null ? false : mandatoryStmt.argument().booleanValue();
+ mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
+ .booleanValue();
}
private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
this.actions = actionsBuilder.build();
this.notifications = notificationsBuilder.build();
- presence = firstEffective(PresenceEffectiveStatement.class) != null;
+ presence = findFirstEffectiveSubstatement(PresenceEffectiveStatement.class).isPresent();
}
@Override
import org.opendaylight.yangtools.yang.model.api.stmt.MaxElementsEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.MinElementsEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
DeviateEffectiveStatementImpl(final StmtContext<DeviateKind, DeviateStatement, ?> ctx) {
super(ctx);
- this.deviateType = argument();
-
- final ConfigEffectiveStatement configStmt = firstEffective(ConfigEffectiveStatement.class);
- this.deviatedConfig = OptionalBoolean.ofNullable(configStmt == null ? null : configStmt.argument());
- final DefaultEffectiveStatement defaultStmt = firstEffective(DefaultEffectiveStatement.class);
- this.deviatedDefault = defaultStmt == null ? null : defaultStmt.argument();
- final MandatoryEffectiveStatement mandatoryStmt = firstEffective(MandatoryEffectiveStatement.class);
- this.deviatedMandatory = OptionalBoolean.ofNullable(mandatoryStmt == null ? null : mandatoryStmt.argument());
- final MaxElementsEffectiveStatement maxElementsStmt = firstEffective(MaxElementsEffectiveStatement.class);
- this.deviatedMaxElements = maxElementsStmt == null ? null : Integer.valueOf(maxElementsStmt.argument());
- final MinElementsEffectiveStatement minElementsStmt = firstEffective(MinElementsEffectiveStatement.class);
- this.deviatedMinElements = minElementsStmt == null ? null : minElementsStmt.argument();
- final TypeEffectiveStatement<TypeStatement> typeStmt = firstEffective(TypeEffectiveStatement.class);
- this.deviatedType = typeStmt == null ? null : typeStmt.getTypeDefinition();
- final UnitsEffectiveStatement unitsStmt = firstEffective(UnitsEffectiveStatement.class);
- this.deviatedUnits = unitsStmt == null ? null : unitsStmt.argument();
-
- this.deviatedMustDefinitions = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
- this.deviatedUniqueConstraints = ImmutableList.copyOf(allSubstatementsOfType(UniqueConstraint.class));
+ deviateType = argument();
+ deviatedConfig = OptionalBoolean.ofNullable(findFirstEffectiveSubstatementArgument(
+ ConfigEffectiveStatement.class).orElse(null));
+ deviatedMandatory = OptionalBoolean.ofNullable(findFirstEffectiveSubstatementArgument(
+ MandatoryEffectiveStatement.class).orElse(null));
+ deviatedDefault = findFirstEffectiveSubstatementArgument(DefaultEffectiveStatement.class).orElse(null);
+ deviatedMaxElements = findFirstEffectiveSubstatementArgument(MaxElementsEffectiveStatement.class)
+ // FIXME: this does not handle 'unbounded'
+ .map(Integer::valueOf).orElse(null);
+ deviatedMinElements = findFirstEffectiveSubstatementArgument(MinElementsEffectiveStatement.class).orElse(null);
+ deviatedType = findFirstEffectiveSubstatement(TypeEffectiveStatement.class)
+ .map(TypeEffectiveStatement::getTypeDefinition).orElse(null);
+ deviatedUnits = findFirstEffectiveSubstatementArgument(UnitsEffectiveStatement.class).orElse(null);
+
+ deviatedMustDefinitions = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
+ deviatedUniqueConstraints = ImmutableList.copyOf(allSubstatementsOfType(UniqueConstraint.class));
}
@Override
this.deviateDefinitions = ImmutableList.copyOf(allSubstatementsOfType(DeviateDefinition.class));
- DescriptionEffectiveStatement descriptionStmt = firstEffective(DescriptionEffectiveStatement.class);
- this.description = descriptionStmt == null ? null : descriptionStmt.argument();
-
- ReferenceEffectiveStatement referenceStmt = firstEffective(ReferenceEffectiveStatement.class);
- this.reference = referenceStmt == null ? null : referenceStmt.argument();
+ description = findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class).orElse(null);
+ reference = findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class).orElse(null);
List<UnknownSchemaNode> unknownSchemaNodesInit = new ArrayList<>();
for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
import java.util.Deque;
import java.util.List;
import java.util.Objects;
+import java.util.Optional;
import javax.annotation.Nonnull;
import org.opendaylight.yangtools.util.RecursiveObjectLeaker;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ArgumentEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.YinElementEffectiveStatement;
import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDocumentedNode;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.argument.ArgumentEffectiveStatementImpl;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
// FIXME: hide this class
this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
// initFields
- ArgumentEffectiveStatementImpl argumentSubstatement = firstEffective(ArgumentEffectiveStatementImpl.class);
- if (argumentSubstatement != null) {
- this.argument = argumentSubstatement.argument().getLocalName();
-
- YinElementEffectiveStatement yinElement = argumentSubstatement
- .firstEffective(YinElementEffectiveStatement.class);
- if (yinElement != null) {
- this.yin = yinElement.argument();
- } else {
- this.yin = false;
- }
+ final Optional<ArgumentEffectiveStatement> optArgumentSubstatement = findFirstEffectiveSubstatement(
+ ArgumentEffectiveStatement.class);
+ if (optArgumentSubstatement.isPresent()) {
+ final ArgumentEffectiveStatement argumentStatement = optArgumentSubstatement.get();
+ this.argument = argumentStatement.argument().getLocalName();
+ this.yin = argumentStatement.findFirstEffectiveSubstatement(YinElementEffectiveStatement.class)
+ .map(YinElementEffectiveStatement::argument).orElse(Boolean.FALSE).booleanValue();
} else {
this.argument = null;
this.yin = false;
super(ctx);
moduleName = ctx.getStatementArgument();
- final PrefixEffectiveStatement prefixStmt = firstEffective(PrefixEffectiveStatement.class);
- if (prefixStmt != null) {
- this.prefix = prefixStmt.argument();
- } else {
- throw new MissingSubstatementException("Prefix is mandatory substatement of import statement",
- ctx.getStatementSourceReference());
- }
+ final Optional<String> prefixStmt = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class);
+ MissingSubstatementException.throwIf(!prefixStmt.isPresent(), ctx.getStatementSourceReference(),
+ "Prefix is mandatory substatement of import statement");
+ this.prefix = prefixStmt.get();
if (!ctx.isEnabledSemanticVersioning()) {
- final RevisionDateEffectiveStatement revisionDateStmt = firstEffective(
+ final Optional<Revision> optRev = findFirstEffectiveSubstatementArgument(
RevisionDateEffectiveStatement.class);
- this.revision = revisionDateStmt == null ? getImportedRevision(ctx) : revisionDateStmt.argument();
+ this.revision = optRev.isPresent() ? optRev.get() : getImportedRevision(ctx);
this.semVer = null;
} else {
final SemVerSourceIdentifier importedModuleIdentifier = ctx.getFromNamespace(
semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
}
- final DescriptionEffectiveStatement descriptionStmt = firstEffective(DescriptionEffectiveStatement.class);
- this.description = descriptionStmt != null ? descriptionStmt.argument() : null;
-
- final ReferenceEffectiveStatement referenceStmt = firstEffective(ReferenceEffectiveStatement.class);
- this.reference = referenceStmt != null ? referenceStmt.argument() : null;
+ description = findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class).orElse(null);
+ reference = findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class).orElse(null);
}
private Revision getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
defaultStr = dflt;
unitsStr = units;
type = builder.build();
- final MandatoryEffectiveStatement mandatoryStmt = firstEffective(MandatoryEffectiveStatement.class);
- mandatory = mandatoryStmt == null ? false : mandatoryStmt.argument().booleanValue();
+ mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
+ .booleanValue();
mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
}
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
-import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
super(ctx);
this.original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
-
- final OrderedByEffectiveStatement orderedByStmt = firstEffective(OrderedByEffectiveStatement.class);
- if (orderedByStmt != null && ORDER_BY_USER_KEYWORD.equals(orderedByStmt.argument())) {
- this.userOrdered = true;
- } else {
- this.userOrdered = false;
- }
+ this.userOrdered = findFirstEffectiveSubstatementArgument(OrderedByEffectiveStatement.class)
+ .map(ORDER_BY_USER_KEYWORD::equals).orElse(Boolean.FALSE).booleanValue();
// initKeyDefinition
- final List<QName> keyDefinitionInit = new LinkedList<>();
- final KeyEffectiveStatement keyEffectiveSubstatement = firstEffective(KeyEffectiveStatement.class);
- if (keyEffectiveSubstatement != null) {
+ final Optional<KeyEffectiveStatement> optKeyStmt = findFirstEffectiveSubstatement(KeyEffectiveStatement.class);
+ if (optKeyStmt.isPresent()) {
+ final KeyEffectiveStatement keyStmt = optKeyStmt.get();
+ final List<QName> keyDefinitionInit = new ArrayList<>(keyStmt.argument().size());
final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
if (effectiveStatement instanceof LeafSchemaNode) {
possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
}
}
- for (final SchemaNodeIdentifier key : keyEffectiveSubstatement.argument()) {
+ for (final SchemaNodeIdentifier key : keyStmt.argument()) {
final QName keyQName = key.getLastComponent();
if (!possibleLeafQNamesForKey.contains(keyQName)) {
throw new InferenceException(ctx.getStatementSourceReference(),
- "Key '%s' misses node '%s' in list '%s'", keyEffectiveSubstatement.getDeclared()
- .rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument());
+ "Key '%s' misses node '%s' in list '%s'", keyStmt.getDeclared().rawArgument(),
+ keyQName.getLocalName(), ctx.getStatementArgument());
}
keyDefinitionInit.add(keyQName);
}
+
+ this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
+ } else {
+ this.keyDefinition = ImmutableList.of();
}
- this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
+
this.uniqueConstraints = ImmutableList.copyOf(allSubstatementsOfType(UniqueConstraint.class));
final ImmutableSet.Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
MustEffectiveStatementImpl(final StmtContext<RevisionAwareXPath, MustStatement, ?> ctx) {
super(ctx);
- this.xpath = ctx.getStatementArgument();
-
- DescriptionEffectiveStatement descriptionStmt = firstEffective(DescriptionEffectiveStatement.class);
- this.description = descriptionStmt == null ? null : descriptionStmt.argument();
-
- ErrorAppTagEffectiveStatement errorAppTagStmt = firstEffective(ErrorAppTagEffectiveStatement.class);
- this.errorAppTag = errorAppTagStmt == null ? null : errorAppTagStmt.argument();
-
- ErrorMessageEffectiveStatement errorMessageStmt = firstEffective(ErrorMessageEffectiveStatement.class);
- this.errorMessage = errorMessageStmt == null ? null : errorMessageStmt.argument();
-
- ReferenceEffectiveStatement referenceStmt = firstEffective(ReferenceEffectiveStatement.class);
- this.reference = referenceStmt == null ? null : referenceStmt.argument();
+ xpath = ctx.getStatementArgument();
+ description = findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class).orElse(null);
+ errorAppTag = findFirstEffectiveSubstatementArgument(ErrorAppTagEffectiveStatement.class).orElse(null);
+ errorMessage = findFirstEffectiveSubstatementArgument(ErrorMessageEffectiveStatement.class).orElse(null);
+ reference = findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class).orElse(null);
}
@Override
RevisionEffectiveStatementImpl(final StmtContext<Revision, RevisionStatement, ?> ctx) {
super(ctx);
-
- final DescriptionEffectiveStatement descStmt = firstEffective(DescriptionEffectiveStatement.class);
- if (descStmt != null) {
- this.description = descStmt.argument();
- } else {
- this.description = null;
- }
-
- final ReferenceEffectiveStatement refStmt = firstEffective(ReferenceEffectiveStatement.class);
- if (refStmt != null) {
- this.reference = refStmt.argument();
- } else {
- this.reference = null;
- }
+ description = findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class).orElse(null);
+ reference = findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class).orElse(null);
}
@Override
import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
import java.util.Objects;
+import java.util.Optional;
import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.RevisionEffectiveStatement;
final String belongsToModuleName = firstAttributeOf(ctx.declaredSubstatements(), BelongsToStatement.class);
final QNameModule belongsToModuleQName = ctx.getFromNamespace(ModuleNameToModuleQName.class,
belongsToModuleName);
- final RevisionEffectiveStatement submoduleRevision = firstEffective(RevisionEffectiveStatement.class);
- this.qnameModule = QNameModule.create(belongsToModuleQName.getNamespace(),
- submoduleRevision == null ? null : submoduleRevision.argument()).intern();
+
+ final Optional<Revision> submoduleRevision = findFirstEffectiveSubstatementArgument(
+ RevisionEffectiveStatement.class);
+ this.qnameModule = QNameModule.create(belongsToModuleQName.getNamespace(), submoduleRevision).intern();
}
@Override
this.augmentations = ImmutableSet.copyOf(augmentationsInit);
this.refines = ImmutableMap.copyOf(refinesInit);
- final WhenEffectiveStatement whenStmt = firstEffective(WhenEffectiveStatement.class);
- this.whenCondition = whenStmt == null ? null : whenStmt.argument();
+ whenCondition = findFirstEffectiveSubstatementArgument(WhenEffectiveStatement.class).orElse(null);
}
@Nonnull