2 * Copyright © 2016 Red Hat, Inc. and others.
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.ovsdb.utils.yang;
10 import static java.util.Objects.requireNonNull;
12 import java.util.HashMap;
14 import java.util.function.Function;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.binding.Identifiable;
18 import org.opendaylight.yangtools.yang.binding.Identifier;
21 * YANG utility functions.
23 public final class YangUtils {
25 * Prevent instantiation.
32 * Copies a list of YANG key-value items to the given map. Any {@code null} key or value will cause an error.
34 * @param map The map to fill.
35 * @param yangList The list of YANG key-value items.
36 * @param keyExtractor The key extractor function to use.
37 * @param valueExtractor The value extractor function to use.
38 * @param <T> The YANG item type.
39 * @param <K> The key type.
40 * @param <V> The value type.
43 public static <T, K, V> @NonNull Map<K, V> copyYangKeyValueListToMap(@NonNull Map<K, V> map,
44 @Nullable Iterable<T> yangList, @NonNull Function<T, K> keyExtractor,
45 @NonNull Function<T, V> valueExtractor) {
46 if (yangList != null) {
47 for (T yangValue : yangList) {
48 K key = keyExtractor.apply(yangValue);
49 V value = valueExtractor.apply(yangValue);
50 map.put(requireNonNull(key), requireNonNull(value));
57 * Copies a list of YANG key-value items to the given map. Any {@code null} key or value will cause an error.
59 * @param map The map to fill.
60 * @param yangList The map of YANG key-value items.
61 * @param keyExtractor The key extractor function to use.
62 * @param valueExtractor The value extractor function to use.
63 * @param <T> The YANG item type.
64 * @param <K> The key type.
65 * @param <V> The value type.
68 public static <I extends Identifier<T>, T extends Identifiable<I>, K, V>
69 @NonNull Map<K, V> copyYangKeyValueListToMap(@NonNull Map<K, V> map,
70 @Nullable Map<I, T> yangList, @NonNull Function<T, K> keyExtractor,
71 @NonNull Function<T, V> valueExtractor) {
72 if (yangList != null) {
73 return copyYangKeyValueListToMap(map, yangList.values(), keyExtractor, valueExtractor);
79 * Converts a list of YANG key-value items to a map.
81 * @param yangList The list of YANG key-value items.
82 * @param keyExtractor The key extractor function to use.
83 * @param valueExtractor The value extractor function to use.
84 * @param <T> The YANG item type.
85 * @param <K> The key type.
86 * @param <V> The value type.
89 public static <T, K, V> @NonNull Map<K, V> convertYangKeyValueListToMap(@Nullable Iterable<T> yangList,
90 @NonNull Function<T, K> keyExtractor, @NonNull Function<T, V> valueExtractor) {
91 return copyYangKeyValueListToMap(new HashMap<>(), yangList, keyExtractor, valueExtractor);
95 * Converts a list of YANG key-value items to a map.
97 * @param yangList The map of YANG key-value items.
98 * @param keyExtractor The key extractor function to use.
99 * @param valueExtractor The value extractor function to use.
100 * @param <T> The YANG item type.
101 * @param <K> The key type.
102 * @param <V> The value type.
105 public static <I extends Identifier<T>, T extends Identifiable<I>, K, V> @NonNull Map<K, V>
106 convertYangKeyValueListToMap(@Nullable Map<I, T> yangList,
107 @NonNull Function<T, K> keyExtractor, @NonNull Function<T, V> valueExtractor) {
108 return copyYangKeyValueListToMap(new HashMap<>(), yangList, keyExtractor, valueExtractor);