Bug 4079: Unable to compile pattern defined in module
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / KeyStatementImpl.java
index 48f29b08f0f02e6c92d83af4ab822cd7df69dbc0..1ff02358654a87be8e41fb688b7d64f8e8d8be3f 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
@@ -7,72 +7,75 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
-
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.KeyEffectiveStatementImpl;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSet.Builder;
 import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import com.google.common.base.Splitter;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.KeyEffectiveStatementImpl;
 
 public class KeyStatementImpl extends AbstractDeclaredStatement<Collection<SchemaNodeIdentifier>> implements
         KeyStatement {
+    private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
+            .KEY)
+            .build();
 
-    protected KeyStatementImpl(StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> context) {
+    protected KeyStatementImpl(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> context) {
         super(context);
     }
 
     public static class Definition
             extends
-            AbstractStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement, EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> {
+            AbstractStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement,
+                    EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> {
 
         public Definition() {
             super(Rfc6020Mapping.KEY);
         }
 
         @Override
-        public Collection<SchemaNodeIdentifier> parseArgumentValue(StmtContext<?, ?, ?> ctx, String value)
-                throws SourceException {
-
-            Splitter keySplitter = Splitter.on(StmtContextUtils.LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults();
-            List<String> keyTokens = keySplitter.splitToList(value);
-
-            // to detect if key contains duplicates
-            if ((new HashSet<>(keyTokens)).size() < keyTokens.size()) {
-                throw new IllegalArgumentException();
+        public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
+            final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
+            int tokens = 0;
+            for (String keyToken : StmtContextUtils.LIST_KEY_SPLITTER.split(value)) {
+                builder.add(SchemaNodeIdentifier.create(false, Utils.qNameFromArgument(ctx, keyToken)));
+                tokens++;
             }
 
-            Set<SchemaNodeIdentifier> keyNodes = new HashSet<>();
+            // Throws NPE on nulls, retains first inserted value, cannot be modified
+            final Collection<SchemaNodeIdentifier> ret = builder.build();
 
-            for (String keyToken : keyTokens) {
-
-                SchemaNodeIdentifier keyNode = SchemaNodeIdentifier
-                        .create(false, Utils.qNameFromArgument(ctx, keyToken));
-                keyNodes.add(keyNode);
-            }
-
-            return keyNodes;
+            Preconditions.checkArgument(ret.size() == tokens, "Key argument '%s' contains duplicates. At %s", value,
+                    ctx.getStatementSourceReference());
+            return ret;
         }
 
         @Override
-        public KeyStatement createDeclared(StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
+        public KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
             return new KeyStatementImpl(ctx);
         }
 
         @Override
         public EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement> createEffective(
-                StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx) {
+                final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
+                        EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx) {
             return new KeyEffectiveStatementImpl(ctx);
         }
-    }
 
+        @Override
+        public void onFullDefinitionDeclared(StmtContext.Mutable<Collection<SchemaNodeIdentifier>, KeyStatement,
+                EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> stmt) throws SourceException {
+            super.onFullDefinitionDeclared(stmt);
+            SUBSTATEMENT_VALIDATOR.validate(stmt);
+        }
+    }
 }