Rename StatementSource to StatementOrigin
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / AbstractInternedStatementSupport.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.spi.meta;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
14 import com.google.common.collect.ImmutableList;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
20
21 /**
22  * A {@link AbstractStatementSupport} specialized for global interning. This base class is useful when the argument can
23  * be reasonably interned and it dominates the {@link EffectiveStatement} implementation. Typical examples include
24  * {@code position} and {@code value} statements, which typically do not have substatements and are based on simple
25  * types.
26  *
27  * <p>
28  * Note: use of this base class implies context-independence.
29  */
30 @Beta
31 public abstract class AbstractInternedStatementSupport<A, D extends DeclaredStatement<A>,
32         E extends EffectiveStatement<A, D>> extends AbstractStatementSupport<A, D, E> {
33     private final LoadingCache<A, D> declaredCache = CacheBuilder.newBuilder().weakValues()
34             .build(new CacheLoader<A, D>() {
35                 @Override
36                 public D load(final A key) {
37                     return createEmptyDeclared(key);
38                 }
39             });
40     private final LoadingCache<D, E> effectiveCache = CacheBuilder.newBuilder().weakValues()
41             .build(new CacheLoader<D, E>() {
42                 @Override
43                 public E load(final D key) {
44                     return createEmptyEffective(key);
45                 }
46             });
47
48     protected AbstractInternedStatementSupport(final StatementDefinition publicDefinition,
49             final StatementPolicy<A, D> policy) {
50         super(publicDefinition, policy);
51     }
52
53     @Override
54     protected final D createDeclared(final StmtContext<A, D, ?> ctx,
55             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
56         final A argument = ctx.getArgument();
57         return substatements.isEmpty() ? declaredCache.getUnchecked(ctx.getArgument())
58             : createDeclared(argument, substatements);
59     }
60
61     protected abstract @NonNull D createDeclared(@NonNull A argument,
62             @NonNull ImmutableList<? extends DeclaredStatement<?>> substatements);
63
64     protected abstract @NonNull D createEmptyDeclared(@NonNull A argument);
65
66     @Override
67     protected final E createEffective(final Current<A, D> stmt,
68             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
69         return substatements.isEmpty() ? effectiveCache.getUnchecked(stmt.declared())
70             : createEffective(stmt.declared(), substatements);
71     }
72
73     protected abstract @NonNull E createEffective(@NonNull D declared,
74         @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
75
76     protected abstract @NonNull E createEmptyEffective(@NonNull D declared);
77 }