YANGTOOLS-706: Split up base utility classes into rfc6020.util
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / UniqueStatementImpl.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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
10
11 import java.util.Collection;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
16 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Relative;
17 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueStatement;
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.SubstatementValidator;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UniqueEffectiveStatementImpl;
24
25 public class UniqueStatementImpl extends AbstractDeclaredStatement<Collection<SchemaNodeIdentifier.Relative>>
26         implements UniqueStatement {
27     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
28             .UNIQUE)
29             .build();
30
31     protected UniqueStatementImpl(
32             final StmtContext<Collection<SchemaNodeIdentifier.Relative>, UniqueStatement, ?> context) {
33         super(context);
34     }
35
36     public static class Definition
37             extends AbstractStatementSupport<Collection<SchemaNodeIdentifier.Relative>, UniqueStatement,
38                     EffectiveStatement<Collection<SchemaNodeIdentifier.Relative>, UniqueStatement>> {
39
40         public Definition() {
41             super(YangStmtMapping.UNIQUE);
42         }
43
44         @Override
45         public Collection<SchemaNodeIdentifier.Relative> parseArgumentValue(final StmtContext<?, ?, ?> ctx,
46                 final String value) {
47             final Collection<Relative> uniqueConstraints = Utils.parseUniqueConstraintArgument(ctx, value);
48             SourceException.throwIf(uniqueConstraints.isEmpty(), ctx.getStatementSourceReference(),
49                     "Invalid argument value '%s' of unique statement. The value must contains at least "
50                             + "one descendant schema node identifier.", value);
51             return uniqueConstraints;
52         }
53
54         @Override
55         public UniqueStatement createDeclared(final StmtContext<Collection<Relative>, UniqueStatement, ?> ctx) {
56             return new UniqueStatementImpl(ctx);
57         }
58
59         @Override
60         public EffectiveStatement<Collection<Relative>, UniqueStatement> createEffective(
61                 final StmtContext<Collection<Relative>, UniqueStatement,
62                 EffectiveStatement<Collection<Relative>, UniqueStatement>> ctx) {
63             return new UniqueEffectiveStatementImpl(ctx);
64         }
65
66         @Override
67         protected SubstatementValidator getSubstatementValidator() {
68             return SUBSTATEMENT_VALIDATOR;
69         }
70     }
71
72     @Nonnull
73     @Override
74     public Collection<Relative> getTag() {
75         return argument();
76     }
77 }