2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.spi.meta;
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.eclipse.jdt.annotation.Nullable;
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.api.YangParserConfiguration;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
24 * A {@link AbstractStatementSupport} specialized for global interning. This base class is useful when the argument can
25 * be 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
30 * Note: use of this base class implies context-independence.
33 public abstract class AbstractInternedStatementSupport<A, D extends DeclaredStatement<A>,
34 E extends EffectiveStatement<A, D>> extends AbstractStatementSupport<A, D, E> {
35 private final LoadingCache<A, D> declaredCache = CacheBuilder.newBuilder().weakValues()
36 .build(new CacheLoader<A, D>() {
38 public D load(final A key) {
39 return createEmptyDeclared(key);
42 private final LoadingCache<D, E> effectiveCache = CacheBuilder.newBuilder().weakValues()
43 .build(new CacheLoader<D, E>() {
45 public E load(final D key) {
46 return createEmptyEffective(key);
50 protected AbstractInternedStatementSupport(final StatementDefinition publicDefinition,
51 final StatementPolicy<A, D> policy, final YangParserConfiguration config,
52 final @Nullable SubstatementValidator validator) {
53 super(publicDefinition, policy, config, validator);
57 protected final D createDeclared(final BoundStmtCtx<A> ctx,
58 final ImmutableList<DeclaredStatement<?>> substatements) {
59 final A argument = ctx.getArgument();
60 return substatements.isEmpty() ? declaredCache.getUnchecked(ctx.getArgument())
61 : createDeclared(argument, substatements);
64 protected abstract @NonNull D createDeclared(@NonNull A argument,
65 @NonNull ImmutableList<DeclaredStatement<?>> substatements);
67 protected abstract @NonNull D createEmptyDeclared(@NonNull A argument);
70 protected final E createEffective(final Current<A, D> stmt,
71 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
72 return substatements.isEmpty() ? effectiveCache.getUnchecked(stmt.declared())
73 : createEffective(stmt.declared(), substatements);
76 protected abstract @NonNull E createEffective(@NonNull D declared,
77 @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
79 protected abstract @NonNull E createEmptyEffective(@NonNull D declared);