Remove deprecated AbstractEffectiveSimpleDataNodeContainer subclasses
[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         extends AbstractModelStatement<A> implements EffectiveStatement<A, D> {
39     @Override
40     public final <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends V> get(final Class<N> namespace,
41             final K identifier) {
42         return Optional.ofNullable(getAll(namespace).get(requireNonNull(identifier)));
43     }
44
45     @Override
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();
49     }
50
51     @Override
52     public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
53         return ImmutableList.of();
54     }
55
56     /**
57      * Return the statement-specific contents of specified namespace, if available.
58      *
59      * @param namespace Requested namespace
60      * @return Namespace contents, if available.
61      */
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();
65     }
66
67     /**
68      * Utility method for recovering singleton lists squashed by {@link #maskList(ImmutableList)}.
69      *
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
74      */
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);
79     }
80 }