Introduce formatting methods for SourceException
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ListEffectiveStatementImpl.java
index 017dfe0615874202be1fbcc0691a814612dbeaf6..7dc2c2361e7a082e9e0c12905807cb4fa9cc8e3a 100644 (file)
@@ -5,12 +5,10 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
 import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -18,69 +16,37 @@ import java.util.List;
 import java.util.Objects;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
-import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
-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.ListStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 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.Utils;
-
-public class ListEffectiveStatementImpl extends AbstractEffectiveDocumentedDataNodeContainer<QName, ListStatement>
-        implements ListSchemaNode, DerivableSchemaNode {
-    private final QName qname;
-    private final SchemaPath path;
-
-    // FIXME: should be private
-    boolean augmenting;
-    private boolean addedByUses;
-    private ListSchemaNode original;
-    private boolean configuration = true;
-    private final ConstraintDefinition constraints;
-    private boolean userOrdered;
-
-    private ImmutableList<QName> keyDefinition;
-    private ImmutableSet<AugmentationSchema> augmentations;
-    private ImmutableList<UnknownSchemaNode> unknownNodes;
-
-    public ListEffectiveStatementImpl(final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
-        super(ctx);
-        this.qname = ctx.getStatementArgument();
-        this.path = Utils.getSchemaPath(ctx);
-        this.constraints = new EffectiveConstraintDefinitionImpl(this);
 
-        initSubstatementCollectionsAndFields();
-        initCopyType(ctx);
+public final class ListEffectiveStatementImpl extends AbstractEffectiveSimpleDataNodeContainer<ListStatement> implements
+        ListSchemaNode, DerivableSchemaNode {
 
-        // should be after initSubstatementCollectionsAndFields()
-        initKeyDefinition(ctx);
-    }
+    private final boolean userOrdered;
+    private final List<QName> keyDefinition;
+    private static final String ORDER_BY_USER_KEYWORD = "user";
+    private final ListSchemaNode original;
 
-    private void initCopyType(final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
+    public ListEffectiveStatementImpl(
+            final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
+        super(ctx);
 
-        List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
+        this.original = ctx.getOriginalCtx() == null ? null : (ListSchemaNode) ctx.getOriginalCtx().buildEffective();
 
-        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;
+        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 = (ListSchemaNode) ctx.getOriginalCtx().buildEffective();
-        }
-    }
 
-    private void initKeyDefinition(final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
+        // initKeyDefinition
         List<QName> keyDefinitionInit = new LinkedList<>();
         KeyEffectiveStatementImpl keyEffectiveSubstatement = firstEffective(KeyEffectiveStatementImpl.class);
 
@@ -98,9 +64,9 @@ public class ListEffectiveStatementImpl extends AbstractEffectiveDocumentedDataN
                 final QName keyQName = key.getLastComponent();
 
                 if (!possibleLeafQNamesForKey.contains(keyQName)) {
-                    throw new IllegalArgumentException(String.format("Key '%s' misses node '%s' in list '%s', file %s",
-                            keyEffectiveSubstatement.getDeclared().rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument(),
-                            ctx.getStatementSourceReference()));
+                    throw new InferenceException(ctx.getStatementSourceReference(),
+                        "Key '%s' misses node '%s' in list '%s'", keyEffectiveSubstatement.getDeclared().rawArgument(),
+                        keyQName.getLocalName(), ctx.getStatementArgument());
                 }
 
                 keyDefinitionInit.add(keyQName);
@@ -110,82 +76,14 @@ public class ListEffectiveStatementImpl extends AbstractEffectiveDocumentedDataN
         this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
     }
 
-    private void initSubstatementCollectionsAndFields() {
-        Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
-
-        List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
-        Set<AugmentationSchema> augmentationsInit = new HashSet<>();
-
-        boolean configurationInit = false;
-        boolean userOrderedInit = false;
-        for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
-            if (effectiveStatement instanceof UnknownSchemaNode) {
-                UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
-                unknownNodesInit.add(unknownNode);
-            }
-            if (effectiveStatement instanceof AugmentationSchema) {
-                AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
-                augmentationsInit.add(augmentationSchema);
-            }
-            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);
-        this.augmentations = ImmutableSet.copyOf(augmentationsInit);
-    }
-
-    @Override
-    public QName getQName() {
-        return qname;
-    }
-
-    @Override
-    public SchemaPath getPath() {
-        return path;
-    }
-
-    @Override
-    public List<QName> getKeyDefinition() {
-        return keyDefinition;
-    }
-
-    @Override
-    public boolean isAugmenting() {
-        return augmenting;
-    }
-
-    @Override
-    public boolean isAddedByUses() {
-        return addedByUses;
-    }
-
     @Override
     public Optional<ListSchemaNode> getOriginal() {
         return Optional.fromNullable(original);
     }
 
     @Override
-    public boolean isConfiguration() {
-        return configuration;
-    }
-
-    @Override
-    public ConstraintDefinition getConstraints() {
-        return constraints;
-    }
-
-    @Override
-    public Set<AugmentationSchema> getAvailableAugmentations() {
-        return augmentations;
+    public List<QName> getKeyDefinition() {
+        return keyDefinition;
     }
 
     @Override
@@ -193,17 +91,12 @@ public class ListEffectiveStatementImpl extends AbstractEffectiveDocumentedDataN
         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;
     }
 
@@ -219,11 +112,11 @@ public class ListEffectiveStatementImpl extends AbstractEffectiveDocumentedDataN
             return false;
         }
         final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) 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() {
-        return "list " + qname.getLocalName();
+        return "list " + getQName().getLocalName();
     }
 }