Populate parser/ hierarchy
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / uses / RegularLocalUsesEffectiveStatement.java
1 /*
2  * Copyright (c) 2020 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.yangtools.yang.parser.rfc7950.stmt.uses;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.Map;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
14 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
17 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
18
19 /**
20  * An extension of {@link EmptyLocalUsesEffectiveStatement}, which adds substatements to the mix. Since this means we
21  * can also have refine statements, we keep a lazily-populated map of those.
22  */
23 final class RegularLocalUsesEffectiveStatement extends EmptyLocalUsesEffectiveStatement {
24     private final Object substatements;
25
26     private volatile Map<Descendant, SchemaNode> refines;
27
28     RegularLocalUsesEffectiveStatement(final UsesStatement declared, final GroupingDefinition sourceGrouping,
29             final int flags, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
30         super(declared, sourceGrouping, flags);
31         this.substatements = maskList(substatements);
32     }
33
34     @Override
35     public ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
36         return unmaskList(substatements);
37     }
38
39     @Override
40     public Map<Descendant, SchemaNode> getRefines() {
41         final Map<Descendant, SchemaNode> local;
42         return (local = refines) != null ? local : loadRefines();
43     }
44
45     private synchronized @NonNull Map<Descendant, SchemaNode> loadRefines() {
46         Map<Descendant, SchemaNode> local = refines;
47         if (local == null) {
48             refines = local = UsesStatementSupport.indexRefines(effectiveSubstatements());
49         }
50         return local;
51     }
52 }