Add default effectiveSubstatements() implementation
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / AbstractEffectiveStatement.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableMap;
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
22
23 /**
24  * Baseline stateless implementation of an EffectiveStatement. This class adds a few default implementations and
25  * namespace dispatch, but does not actually force any state on its subclasses. This approach is different from
26  * {@link EffectiveStatementBase} in that it adds requirements for an implementation, but it leaves it up to the final
27  * class to provide object layout.
28  *
29  * <p>
30  * This finds immense value in catering the common case, for example effective statements which can, but typically
31  * do not, contain substatements.
32  *
33  * @param <A> Argument type ({@link Void} if statement does not have argument.)
34  * @param <D> Class representing declared version of this statement.
35  */
36 @Beta
37 public abstract class AbstractEffectiveStatement<A, D extends DeclaredStatement<A>>
38         implements EffectiveStatement<A, D> {
39     @Override
40     public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
41         return getAll(namespace).get(requireNonNull(identifier));
42     }
43
44     @Override
45     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
46         final Optional<? extends Map<K, V>> ret = getNamespaceContents(requireNonNull(namespace));
47         return ret.isPresent() ? ret.get() : ImmutableMap.of();
48     }
49
50     @Override
51     public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
52         return ImmutableList.of();
53     }
54
55     /**
56      * Return the statement-specific contents of specified namespace, if available.
57      *
58      * @param namespace Requested namespace
59      * @return Namespace contents, if available.
60      */
61     protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
62             final @NonNull Class<N> namespace) {
63         return Optional.empty();
64     }
65 }