Store key arguments in an ImmutableSet
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / KeyStatementImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.collect.ImmutableSet.Builder;
13 import java.util.Collection;
14 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.KeyEffectiveStatementImpl;
23
24 public class KeyStatementImpl extends AbstractDeclaredStatement<Collection<SchemaNodeIdentifier>> implements
25         KeyStatement {
26
27     protected KeyStatementImpl(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> context) {
28         super(context);
29     }
30
31     public static class Definition
32             extends
33             AbstractStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement, EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> {
34
35         public Definition() {
36             super(Rfc6020Mapping.KEY);
37         }
38
39         @Override
40         public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
41             final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
42             int tokens = 0;
43             for (String keyToken : StmtContextUtils.LIST_KEY_SPLITTER.split(value)) {
44                 builder.add(SchemaNodeIdentifier.create(false, Utils.qNameFromArgument(ctx, keyToken)));
45                 tokens++;
46             }
47
48             // Throws NPE on nulls, retains first inserted value, cannot be modified
49             final Collection<SchemaNodeIdentifier> ret = builder.build();
50
51             Preconditions.checkArgument(ret.size() == tokens, "Key argument '%s' contains duplicates", value);
52             return ret;
53         }
54
55         @Override
56         public KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
57             return new KeyStatementImpl(ctx);
58         }
59
60         @Override
61         public EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement> createEffective(
62                 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx) {
63             return new KeyEffectiveStatementImpl(ctx);
64         }
65     }
66
67 }