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