Bump byte-buddy to 1.14.16
[mdsal.git] / binding / mdsal-binding-model-api / src / main / java / org / opendaylight / mdsal / binding / model / api / YangSourceDefinition.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, 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.model.api;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.ImmutableList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
25
26 /**
27  * DTO capturing the YANG source definition which lead to a {@link GeneratedType} being emitted.
28  */
29 @Beta
30 @NonNullByDefault
31 public abstract sealed class YangSourceDefinition {
32     public static final class Multiple extends YangSourceDefinition {
33         private final List<? extends SchemaNode> nodes;
34
35         Multiple(final ModuleEffectiveStatement module, final Collection<? extends SchemaNode> nodes) {
36             super(module);
37             this.nodes = ImmutableList.copyOf(nodes);
38         }
39
40         /**
41          * Return the defining SchemaNodes. Each node is guaranteed to implement {@link EffectiveStatement} and have
42          * a corresponding declared statement.
43          *
44          * @return defining SchemaNodes, guaranteed to be non-empty
45          */
46         public List<? extends SchemaNode> getNodes() {
47             return nodes.stream()
48                 .filter(YangSourceDefinition::hasDeclaredStatement)
49                 .collect(Collectors.toList());
50         }
51     }
52
53     public static final class Single extends YangSourceDefinition {
54         private final DocumentedNode node;
55
56         Single(final ModuleEffectiveStatement module, final DocumentedNode node) {
57             super(module);
58             this.node = requireNonNull(node);
59         }
60
61         /**
62          * Return the defining DocumentedNode. The node is guaranteed to implement {@link EffectiveStatement} and have
63          * a corresponding declared statement.
64          *
65          * @return defining SchemaNodes, guaranteed to be non-empty
66          */
67         public DocumentedNode getNode() {
68             return node;
69         }
70     }
71
72     private final ModuleEffectiveStatement module;
73
74     private YangSourceDefinition(final ModuleEffectiveStatement module) {
75         this.module = requireNonNull(module);
76     }
77
78     public static Optional<YangSourceDefinition> of(final Module module) {
79         final ModuleEffectiveStatement effective = module.asEffectiveStatement();
80         return effective.getDeclared() != null ? Optional.of(new Single(effective, module)) : Optional.empty();
81     }
82
83     public static Optional<YangSourceDefinition> of(final Module module, final SchemaNode node) {
84         return of(module.asEffectiveStatement(), node);
85     }
86
87     public static Optional<YangSourceDefinition> of(final ModuleEffectiveStatement module, final SchemaNode node) {
88         return hasDeclaredStatement(node) ? Optional.of(new Single(module, node)) : Optional.empty();
89     }
90
91     public static Optional<YangSourceDefinition> of(final ModuleEffectiveStatement module,
92             final EffectiveStatement<?, ?> effective) {
93         return effective instanceof DocumentedNode node && effective.getDeclared() != null
94                 ? Optional.of(new Single(module, node)) : Optional.empty();
95     }
96
97     public static Optional<YangSourceDefinition> of(final Module module, final Collection<? extends SchemaNode> nodes) {
98         checkArgument(!nodes.isEmpty());
99
100         return nodes.stream().anyMatch(YangSourceDefinition::hasDeclaredStatement)
101             ? Optional.of(new Multiple(module.asEffectiveStatement(), nodes))
102                 : Optional.empty();
103     }
104
105     /**
106      * Return the defining YANG module.
107      *
108      * @return Defining YANG module.
109      */
110     public final ModuleEffectiveStatement getModule() {
111         return module;
112     }
113
114     private static boolean hasDeclaredStatement(final SchemaNode schemaNode) {
115         return schemaNode instanceof EffectiveStatement<?, ?> effective && effective.getDeclared() != null;
116     }
117 }