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=2a621b2c9fd838e390142b9ad26ed7c051dc3f88;hb=07a461a735316f15f9a78455e1c1c3caf91b2a3e;hp=fd269a1f163d67f89a47795369571e65b4231bd0;hpb=3d283ec6184505ad5e7eefb173044ff383222e9f;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 fd269a1f16..2a621b2c9f 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,154 +7,120 @@
*/
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
-import java.util.Collections;
-
-import java.util.NoSuchElementException;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-import com.google.common.collect.Collections2;
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
-import com.google.common.collect.Iterables;
-import java.util.LinkedList;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
-import com.google.common.collect.FluentIterable;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import com.google.common.collect.Iterables;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.List;
import java.util.Map;
-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.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.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.RecursiveObjectLeaker;
-abstract public class EffectiveStatementBase>
- implements EffectiveStatement {
-
- private final StmtContext stmtCtx;
- private final ImmutableList extends EffectiveStatement, ?>> substatements;
- private final StatementSource statementSource;
- private final StatementDefinition statementDefinition;
- private D declaredInstance;
-
- private final A argument;
-
- public EffectiveStatementBase(StmtContext ctx) {
-
- this.stmtCtx = ctx;
- this.statementDefinition = ctx.getPublicDefinition();
- this.argument = ctx.getStatementArgument();
- this.statementSource = ctx.getStatementSource();
+public abstract class EffectiveStatementBase> implements EffectiveStatement {
- Collection> declaredSubstatements = ctx
- .declaredSubstatements();
- Collection> effectiveSubstatements = ctx
- .effectiveSubstatements();
+ private static final Predicate> IS_SUPPORTED_TO_BUILD_EFFECTIVE =
+ new Predicate>() {
+ @Override
+ public boolean apply(final StmtContext, ?, ?> input) {
+ return input.isSupportedToBuildEffective();
+ }
+ };
- Collection> substatementsInit = new LinkedList<>();
- substatementsInit.addAll(declaredSubstatements);
+ private static final Predicate> IS_UNKNOWN_STATEMENT_CONTEXT =
+ new Predicate>() {
+ @Override
+ public boolean apply(final StmtContext, ?, ?> input) {
+ return StmtContextUtils.isUnknownStatement(input);
+ }
+ };
+
+ private final List extends EffectiveStatement, ?>> substatements;
+
+ /**
+ * Constructor.
+ *
+ * @param ctx
+ * context of statement.
+ */
+ protected EffectiveStatementBase(final StmtContext ctx) {
+ final Collection> effectiveSubstatements = ctx.effectiveSubstatements();
+ final Collection> substatementsInit = new ArrayList<>();
+
+ for(StatementContextBase, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
+ if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
+ substatementsInit.add(declaredSubstatement);
+ substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
+ ((StatementContextBase, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
+ .getEffectOfStatement());
+ } else {
+ substatementsInit.add(declaredSubstatement);
+ }
+ }
substatementsInit.addAll(effectiveSubstatements);
- this.substatements = FluentIterable.from(substatementsInit)
- .transform(StmtContextUtils.buildEffective()).toList();
- }
-
- @Override
- public StatementDefinition statementDefinition() {
- return statementDefinition;
- }
-
- @Override
- public A argument() {
- return argument;
- }
-
- @Override
- public StatementSource getStatementSource() {
- return statementSource;
- }
-
- @Override
- public D getDeclared() {
- if (declaredInstance == null) {
- declaredInstance = stmtCtx.buildDeclared();
- }
+ // WARNING: this leaks an incompletely-initialized pbject
+ RecursiveObjectLeaker.inConstructor(this);
- return declaredInstance;
+ this.substatements = ImmutableList.copyOf(Collections2.transform(Collections2.filter(substatementsInit,
+ IS_SUPPORTED_TO_BUILD_EFFECTIVE), StmtContextUtils.buildEffective()));
}
- // public > V
- // get(
@Override
- public > V get(
- Class namespace, K identifier) {
- return stmtCtx.getFromNamespace(namespace, identifier);
+ public final > V get(final Class namespace, final K identifier) {
+ throw new UnsupportedOperationException("Not implemented yet.");
}
@Override
- public > Map getAll(
- Class namespace) {
- return (Map) stmtCtx.getAllFromNamespace(namespace);
+ public final > Map getAll(final Class namespace) {
+ throw new UnsupportedOperationException("Not implemented yet.");
}
@Override
- public Collection extends EffectiveStatement, ?>> effectiveSubstatements() {
+ public final Collection extends EffectiveStatement, ?>> effectiveSubstatements() {
return substatements;
}
- public StmtContext getStatementContext() {
- return stmtCtx;
+ protected final > S firstEffective(final Class type) {
+ Optional extends EffectiveStatement, ?>> possible = Iterables.tryFind(substatements,
+ Predicates.instanceOf(type));
+ return possible.isPresent() ? type.cast(possible.get()) : null;
}
- protected final > S firstEffective(
- 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 firstSchemaNode(final Class type) {
+ Optional extends EffectiveStatement, ?>> possible = Iterables.tryFind(substatements,
+ Predicates.instanceOf(type));
+ return possible.isPresent() ? type.cast(possible.get()) : null;
}
@SuppressWarnings("unchecked")
- protected final > Collection extends S> allEffective(
- Class type) {
- Collection extends S> 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 S firstSchemaNode(Class type) {
- S result = null;
- try {
- result = type.cast(Iterables.find(substatements,
- Predicates.instanceOf(type)));
- } catch (NoSuchElementException e) {
- result = null;
- }
- return result;
+ protected final T firstSubstatementOfType(final Class type) {
+ Optional extends EffectiveStatement, ?>> possible = Iterables.tryFind(substatements,
+ Predicates.instanceOf(type));
+ return possible.isPresent() ? type.cast(possible.get()) : null;
}
- @SuppressWarnings("unchecked")
- protected final Collection extends S> allSchemaNodes(
- Class type) {
- Collection extends S> result = null;
-
- try {
- result = Collection.class.cast(Collections2.filter(substatements,
- Predicates.instanceOf(type)));
- } catch (NoSuchElementException e) {
- result = Collections.emptyList();
- }
- return result;
+ protected final R firstSubstatementOfType(final Class> type, final Class returnType) {
+ Optional extends EffectiveStatement, ?>> 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();
+ }
}