2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ordered_by;
10 import com.google.common.collect.ImmutableList;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
13 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByStatement;
17 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStringStatementSupport;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
21 public final class OrderedByStatementSupport
22 extends BaseStringStatementSupport<OrderedByStatement, OrderedByEffectiveStatement> {
23 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
24 SubstatementValidator.builder(YangStmtMapping.ORDERED_BY).build();
25 private static final OrderedByStatementSupport INSTANCE = new OrderedByStatementSupport();
28 * Ordered-by has low argument cardinality, hence we can reuse them in case declaration does not have any
29 * substatements (which is the usual case).
31 private static final @NonNull EmptyOrderedByStatement EMPTY_SYSTEM_DECL = new EmptyOrderedByStatement("system");
32 private static final @NonNull EmptyOrderedByStatement EMPTY_USER_DECL = new EmptyOrderedByStatement("user");
33 private static final @NonNull EmptyOrderedByEffectiveStatement EMPTY_SYSTEM_EFF =
34 new EmptyOrderedByEffectiveStatement(EMPTY_SYSTEM_DECL);
35 private static final @NonNull EmptyOrderedByEffectiveStatement EMPTY_USER_EFF =
36 new EmptyOrderedByEffectiveStatement(EMPTY_USER_DECL);
38 private OrderedByStatementSupport() {
39 super(YangStmtMapping.ORDERED_BY);
42 public static OrderedByStatementSupport getInstance() {
47 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
52 public String internArgument(final String rawArgument) {
53 if ("user".equals(rawArgument)) {
55 } else if ("system".equals(rawArgument)) {
63 protected SubstatementValidator getSubstatementValidator() {
64 return SUBSTATEMENT_VALIDATOR;
68 protected OrderedByStatement createDeclared(final StmtContext<String, OrderedByStatement, ?> ctx,
69 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
70 return new RegularOrderedByStatement(ctx.coerceStatementArgument(), substatements);
74 protected OrderedByStatement createEmptyDeclared(final StmtContext<String, OrderedByStatement, ?> ctx) {
75 final String argument = ctx.coerceStatementArgument();
78 return EMPTY_SYSTEM_DECL;
80 return EMPTY_USER_DECL;
82 throw new IllegalStateException("Unhandled argument " + argument);
87 protected OrderedByEffectiveStatement createEffective(
88 final StmtContext<String, OrderedByStatement, OrderedByEffectiveStatement> ctx,
89 final OrderedByStatement declared,
90 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
91 return new RegularOrderedByEffectiveStatement(declared, substatements);
95 protected OrderedByEffectiveStatement createEmptyEffective(
96 final StmtContext<String, OrderedByStatement, OrderedByEffectiveStatement> ctx,
97 final OrderedByStatement declared) {
98 // Aggressively reuse effective instances which are backed by the corresponding empty declared instance, as this
99 // is the case unless there is a weird extension in use.
100 final String argument = declared.getValue();
103 if (EMPTY_SYSTEM_DECL.equals(declared)) {
104 return EMPTY_SYSTEM_EFF;
108 if (EMPTY_USER_DECL.equals(declared)) {
109 return EMPTY_USER_EFF;
113 throw new IllegalStateException("Unhandled argument " + argument);
116 // Declared instance was non-empty, which can happen with extensions
117 return new EmptyOrderedByEffectiveStatement(declared);