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