a0abdcefbb02c7de44aebd796a250a253f4364e6
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / rt / DefaultGroupingRuntimeType.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.rt;
9
10 import java.util.Arrays;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Objects;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
16 import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType;
17 import org.opendaylight.mdsal.binding.runtime.api.GroupingRuntimeType;
18 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
19 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
20
21 public final class DefaultGroupingRuntimeType extends AbstractCompositeRuntimeType<GroupingEffectiveStatement>
22         implements GroupingRuntimeType {
23     /**
24      * These are vectors towards concrete instantiations of this type -- i.e. the manifestation in the effective data
25      * tree. Each item in this list represents either:
26      * <ul>
27      *   <li>a concrete instantiation, or<li>
28      *   <li>another {@link GroupingRuntimeType}</li>
29      * </ul>
30      * We use these vectors to create {@link #instantiations()}.
31      */
32     private final @Nullable Object instantiationVectors;
33
34     public DefaultGroupingRuntimeType(final GeneratedType bindingType, final GroupingEffectiveStatement statement,
35             final List<RuntimeType> children, final List<? extends CompositeRuntimeType> instantiationVectors) {
36         super(bindingType, statement, children);
37         this.instantiationVectors = switch (instantiationVectors.size()) {
38             case 0 -> null;
39             case 1 -> Objects.requireNonNull(instantiationVectors.get(0));
40             default -> instantiationVectors.stream().map(Objects::requireNonNull).toArray(CompositeRuntimeType[]::new);
41         };
42     }
43
44     @Override
45     public List<CompositeRuntimeType> directUsers() {
46         final var local = instantiationVectors;
47         if (local == null) {
48             return List.of();
49         } else if (local instanceof CompositeRuntimeType[] array) {
50             return Collections.unmodifiableList(Arrays.asList(array));
51         } else {
52             return List.of((CompositeRuntimeType) local);
53         }
54     }
55 }