Rename binding-lib
[yangtools.git] / model / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / meta / AbstractEffectiveStatement.java
1 /*
2  * Copyright (c) 2022 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.model.api.meta;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableList;
13 import java.util.Map;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.Empty;
17
18 /**
19  * Abstract base class for {@link EffectiveStatement} implementations.
20  *
21  * @param <A> Argument type ({@link Empty} if statement does not have argument.)
22  * @param <D> Class representing declared version of this statement.
23  */
24 public abstract non-sealed class AbstractEffectiveStatement<A, D extends DeclaredStatement<A>>
25         extends AbstractModelStatement<A> implements EffectiveStatement<A, D> {
26     /**
27      * Utility method for recovering singleton lists squashed by {@link #maskList(ImmutableList)}.
28      *
29      * @param masked list to unmask
30      * @return Unmasked list
31      * @throws NullPointerException if masked is null
32      * @throws ClassCastException if masked object does not match EffectiveStatement
33      */
34     @SuppressWarnings({ "rawtypes", "unchecked" })
35     protected static final @NonNull ImmutableList<? extends @NonNull EffectiveStatement<?, ?>> unmaskList(
36             final @NonNull Object masked) {
37         return (ImmutableList) unmaskList(masked, EffectiveStatement.class);
38     }
39
40     protected static final <E> @NonNull Optional<E> filterOptional(final @NonNull Optional<?> optional,
41             final @NonNull Class<E> type) {
42         return optional.filter(type::isInstance).map(type::cast);
43     }
44
45     protected static final <K, V> @NonNull Optional<V> findValue(final Map<K, V> map, final K key) {
46         return Optional.ofNullable(map.get(requireNonNull(key)));
47     }
48 }