f20ad882aa49fca638dc9ce408b9cace0ecd1bff
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / LeafListGenerator.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.mdsal.binding.generator.impl.reactor;
9
10 import org.opendaylight.mdsal.binding.model.api.Type;
11 import org.opendaylight.mdsal.binding.model.ri.Types;
12 import org.opendaylight.yangtools.yang.common.Ordering;
13 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
15
16 /**
17  * Generator corresponding to a {@code leaf-list} statement.
18  */
19 final class LeafListGenerator extends AbstractTypeAwareGenerator<LeafListEffectiveStatement> {
20     LeafListGenerator(final LeafListEffectiveStatement statement, final AbstractCompositeGenerator<?> parent) {
21         super(statement, parent);
22     }
23
24     @Override
25     Type methodReturnType(final TypeBuilderFactory builderFactory) {
26         // If we are a leafref and the reference cannot be resolved, we need to generate a list wildcard, not
27         // List<Object>, we will try to narrow the return type in subclasses.
28         final Type type = super.methodReturnType(builderFactory);
29         final boolean isObject = Types.objectType().equals(type);
30         final Ordering ordering = statement().findFirstEffectiveSubstatementArgument(OrderedByEffectiveStatement.class)
31             .orElse(Ordering.SYSTEM);
32         switch (ordering) {
33             case SYSTEM:
34                 return isObject ? Types.setTypeWildcard() : Types.setTypeFor(type);
35             case USER:
36                 return isObject ? Types.listTypeWildcard() : Types.listTypeFor(type);
37             default:
38                 throw new IllegalStateException("Unexpected ordering " + ordering);
39         }
40     }
41 }