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.common.Ordering;
13 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByStatement;
18 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 public final class OrderedByStatementSupport
24 extends BaseStatementSupport<Ordering, OrderedByStatement, OrderedByEffectiveStatement> {
25 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
26 SubstatementValidator.builder(YangStmtMapping.ORDERED_BY).build();
27 private static final OrderedByStatementSupport INSTANCE = new OrderedByStatementSupport();
30 * Ordered-by has low argument cardinality, hence we can reuse them in case declaration does not have any
31 * substatements (which is the usual case).
33 private static final @NonNull EmptyOrderedByStatement EMPTY_SYSTEM_DECL =
34 new EmptyOrderedByStatement(Ordering.SYSTEM);
35 private static final @NonNull EmptyOrderedByStatement EMPTY_USER_DECL =
36 new EmptyOrderedByStatement(Ordering.USER);
37 private static final @NonNull EmptyOrderedByEffectiveStatement EMPTY_SYSTEM_EFF =
38 new EmptyOrderedByEffectiveStatement(EMPTY_SYSTEM_DECL);
39 private static final @NonNull EmptyOrderedByEffectiveStatement EMPTY_USER_EFF =
40 new EmptyOrderedByEffectiveStatement(EMPTY_USER_DECL);
42 private OrderedByStatementSupport() {
43 super(YangStmtMapping.ORDERED_BY, CopyPolicy.CONTEXT_INDEPENDENT);
46 public static OrderedByStatementSupport getInstance() {
51 public Ordering parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
53 return Ordering.forArgument(value);
54 } catch (IllegalArgumentException e) {
55 throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid ordered-by argument '%s'", value);
60 public String internArgument(final String rawArgument) {
61 if ("user".equals(rawArgument)) {
63 } else if ("system".equals(rawArgument)) {
71 protected SubstatementValidator getSubstatementValidator() {
72 return SUBSTATEMENT_VALIDATOR;
76 protected OrderedByStatement createDeclared(final StmtContext<Ordering, OrderedByStatement, ?> ctx,
77 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
78 return new RegularOrderedByStatement(ctx.coerceStatementArgument(), substatements);
82 protected OrderedByStatement createEmptyDeclared(final StmtContext<Ordering, OrderedByStatement, ?> ctx) {
83 final Ordering argument = ctx.coerceStatementArgument();
86 return EMPTY_SYSTEM_DECL;
88 return EMPTY_USER_DECL;
90 throw new IllegalStateException("Unhandled argument " + argument);
95 protected OrderedByEffectiveStatement createEffective(
96 final StmtContext<Ordering, OrderedByStatement, OrderedByEffectiveStatement> ctx,
97 final OrderedByStatement declared,
98 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
99 return new RegularOrderedByEffectiveStatement(declared, substatements);
103 protected OrderedByEffectiveStatement createEmptyEffective(
104 final StmtContext<Ordering, OrderedByStatement, OrderedByEffectiveStatement> ctx,
105 final OrderedByStatement declared) {
106 // Aggressively reuse effective instances which are backed by the corresponding empty declared instance, as this
107 // is the case unless there is a weird extension in use.
108 if (EMPTY_USER_DECL.equals(declared)) {
109 // Most likely to be seen (as system is the default)
110 return EMPTY_USER_EFF;
111 } else if (EMPTY_SYSTEM_DECL.equals(declared)) {
112 return EMPTY_SYSTEM_EFF;
114 return new EmptyOrderedByEffectiveStatement(declared);