Update MRI projects for Aluminium
[ovsdb.git] / utils / yang-utils / src / main / java / org / opendaylight / ovsdb / utils / yang / YangUtils.java
1 /*
2  * Copyright © 2016 Red Hat, Inc. and others.
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.ovsdb.utils.yang;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.HashMap;
13 import java.util.Map;
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;
19
20 /**
21  * YANG utility functions.
22  */
23 public final class YangUtils {
24     /**
25      * Prevent instantiation.
26      */
27     private YangUtils() {
28         // Nothing to do
29     }
30
31     /**
32      * Copies a list of YANG key-value items to the given map. Any {@code null} key or value will cause an error.
33      *
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.
41      * @return The map.
42      */
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));
51             }
52         }
53         return map;
54     }
55
56     /**
57      * Copies a list of YANG key-value items to the given map. Any {@code null} key or value will cause an error.
58      *
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.
66      * @return The map.
67      */
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);
74         }
75         return map;
76     }
77
78     /**
79      * Converts a list of YANG key-value items to a map.
80      *
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.
87      * @return The map.
88      */
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);
92     }
93
94     /**
95      * Converts a list of YANG key-value items to a map.
96      *
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.
103      * @return The map.
104      */
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);
109     }
110 }