Bug 4412: New yang parser effective statements cleanup
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / LeafListEffectiveStatementImpl.java
index 92cb8de401392849c5c4c9f4b6139008efbab837..1963d6b79acbfd69783065b19933866312099d08 100644 (file)
@@ -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
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
 import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableList;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
 import java.util.Objects;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-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.LeafListStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
 
-public class LeafListEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, LeafListStatement> implements
+public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement> implements
         LeafListSchemaNode, DerivableSchemaNode {
-    private final QName qname;
-    private final SchemaPath path;
 
-    // FIXME: should be private
-    boolean augmenting;
-    private boolean addedByUses;
-    private LeafListSchemaNode original;
-    private boolean configuration = true;
-    private final ConstraintDefinition constraintsDef;
-    private TypeDefinition<?> type;
-    private boolean userOrdered;
-
-    private ImmutableList<UnknownSchemaNode> unknownNodes;
+    private final LeafListSchemaNode original;
+    private final TypeDefinition<?> type;
+    private final boolean userOrdered;
+    private static final String ORDER_BY_USER_KEYWORD = "user";
 
     public LeafListEffectiveStatementImpl(
             final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
         super(ctx);
-        this.qname = ctx.getStatementArgument();
-        this.path = Utils.getSchemaPath(ctx);
-        this.constraintsDef = new EffectiveConstraintDefinitionImpl(this);
-
-        // :TODO init TypeDefinition
-
-        initSubstatementCollections();
-        initCopyType(ctx);
-    }
-
-    private void initCopyType(
-            final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
-
-        List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
-
-        if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
-            augmenting = true;
-        }
-        if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
-            addedByUses = true;
-        }
-        if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
-            addedByUses = augmenting = true;
+        this.original = ctx.getOriginalCtx() == null ? null : (LeafListSchemaNode) ctx.getOriginalCtx()
+                .buildEffective();
+
+        OrderedByEffectiveStatementImpl orderedByStmt = firstEffective(OrderedByEffectiveStatementImpl.class);
+        if (orderedByStmt != null && orderedByStmt.argument().equals(ORDER_BY_USER_KEYWORD)) {
+            this.userOrdered = true;
+        } else {
+            this.userOrdered = false;
         }
 
-        if (ctx.getOriginalCtx() != null) {
-            original = (LeafListSchemaNode) ctx.getOriginalCtx().buildEffective();
-        }
-    }
-    private void initSubstatementCollections() {
-        Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
-
-        List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
-
-        boolean configurationInit = false;
-        boolean userOrderedInit = false;
-        for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
-            if (effectiveStatement instanceof UnknownSchemaNode) {
-                unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
-            }
-            if (effectiveStatement instanceof TypeDefinition) {
-                type = TypeUtils.getTypeFromEffectiveStatement(effectiveStatement);
-            }
-            if (!configurationInit && effectiveStatement instanceof ConfigEffectiveStatementImpl) {
-                ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveStatement;
-                this.configuration = configStmt.argument();
-                configurationInit = true;
-            }
-            if (!userOrderedInit && effectiveStatement instanceof OrderedByEffectiveStatementImpl) {
-                OrderedByEffectiveStatementImpl orderedByStmt = (OrderedByEffectiveStatementImpl) effectiveStatement;
-                this.userOrdered = orderedByStmt.argument().equals("user") ? true : false;
-                userOrderedInit = true;
-            }
-        }
-
-        this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
-    }
-
-    @Override
-    public QName getQName() {
-        return qname;
-    }
-
-    @Override
-    public SchemaPath getPath() {
-        return path;
-    }
-
-    @Override
-    public boolean isAugmenting() {
-        return augmenting;
-    }
-
-    @Override
-    public boolean isAddedByUses() {
-        return addedByUses;
+        EffectiveStatement<?, ?> typeEffectiveSubstatement = firstEffectiveSubstatementOfType(TypeDefinition.class);
+        this.type = TypeUtils.getTypeFromEffectiveStatement(typeEffectiveSubstatement);
     }
 
     @Override
@@ -129,16 +48,6 @@ public class LeafListEffectiveStatementImpl extends AbstractEffectiveDocumentedN
         return Optional.fromNullable(original);
     }
 
-    @Override
-    public boolean isConfiguration() {
-        return configuration;
-    }
-
-    @Override
-    public ConstraintDefinition getConstraints() {
-        return constraintsDef;
-    }
-
     @Override
     public TypeDefinition<?> getType() {
         return type;
@@ -149,17 +58,12 @@ public class LeafListEffectiveStatementImpl extends AbstractEffectiveDocumentedN
         return userOrdered;
     }
 
-    @Override
-    public List<UnknownSchemaNode> getUnknownSchemaNodes() {
-        return unknownNodes;
-    }
-
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + Objects.hashCode(qname);
-        result = prime * result + Objects.hashCode(path);
+        result = prime * result + Objects.hashCode(getQName());
+        result = prime * result + Objects.hashCode(getPath());
         return result;
     }
 
@@ -175,14 +79,14 @@ public class LeafListEffectiveStatementImpl extends AbstractEffectiveDocumentedN
             return false;
         }
         LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
-        return Objects.equals(qname, other.qname) && Objects.equals(path, other.path);
+        return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
     }
 
     @Override
     public String toString() {
         StringBuilder sb = new StringBuilder(LeafListEffectiveStatementImpl.class.getSimpleName());
         sb.append("[");
-        sb.append(qname);
+        sb.append(getQName());
         sb.append("]");
         return sb.toString();
     }