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.rfc7950.stmt;
10 import static java.util.Objects.requireNonNull;
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;
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;
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.
30 * This finds immense value in catering the common case, for example effective statements which can, but typically
31 * do not, contain substatements.
33 * @param <A> Argument type ({@link Void} if statement does not have argument.)
34 * @param <D> Class representing declared version of this statement.
37 public abstract class AbstractEffectiveStatement<A, D extends DeclaredStatement<A>>
38 extends AbstractModelStatement<A> implements EffectiveStatement<A, D> {
40 public final <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends V> get(final Class<N> namespace,
42 return Optional.ofNullable(getAll(namespace).get(requireNonNull(identifier)));
46 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
47 final Optional<? extends Map<K, V>> ret = getNamespaceContents(requireNonNull(namespace));
48 return ret.isPresent() ? ret.get() : ImmutableMap.of();
52 public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
53 return ImmutableList.of();
57 * Return the statement-specific contents of specified namespace, if available.
59 * @param namespace Requested namespace
60 * @return Namespace contents, if available.
62 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
63 final @NonNull Class<N> namespace) {
64 return Optional.empty();
68 * Utility method for recovering singleton lists squashed by {@link #maskList(ImmutableList)}.
70 * @param masked list to unmask
71 * @return Unmasked list
72 * @throws NullPointerException if masked is null
73 * @throws ClassCastException if masked object does not match EffectiveStatement
75 @SuppressWarnings({ "rawtypes", "unchecked" })
76 protected static final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> unmaskList(
77 final @NonNull Object masked) {
78 return (ImmutableList) unmaskList(masked, EffectiveStatement.class);