007d9d6616b68d8f8ba524e612e31d4f2d618446
[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 com.google.common.base.Preconditions;
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.function.Function;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16
17 /**
18  * YANG utility functions.
19  */
20 public final class YangUtils {
21     /**
22      * Prevent instantiation.
23      */
24     private YangUtils() {
25         // Nothing to do
26     }
27
28     /**
29      * Copies a list of YANG key-value items to the given map. Any {@code null} key or value will cause an error.
30      *
31      * @param map The map to fill.
32      * @param yangList The list of YANG key-value items.
33      * @param keyExtractor The key extractor function to use.
34      * @param valueExtractor The value extractor function to use.
35      * @param <T> The YANG item type.
36      * @param <K> The key type.
37      * @param <V> The value type.
38      * @return The map.
39      */
40     @NonNull
41     public static <T, K, V> Map<K, V> copyYangKeyValueListToMap(@NonNull Map<K, V> map, @Nullable Iterable<T> yangList,
42                                                                 @NonNull Function<T, K> keyExtractor,
43                                                                 @NonNull Function<T, V> valueExtractor) {
44         if (yangList != null) {
45             for (T yangValue : yangList) {
46                 K key = keyExtractor.apply(yangValue);
47                 V value = valueExtractor.apply(yangValue);
48                 Preconditions.checkNotNull(key);
49                 Preconditions.checkNotNull(value);
50                 map.put(key, value);
51             }
52         }
53         return map;
54     }
55
56     /**
57      * Converts a list of YANG key-value items to a map.
58      *
59      * @param yangList The list of YANG key-value items.
60      * @param keyExtractor The key extractor function to use.
61      * @param valueExtractor The value extractor function to use.
62      * @param <T> The YANG item type.
63      * @param <K> The key type.
64      * @param <V> The value type.
65      * @return The map.
66      */
67     @NonNull
68     public static <T, K, V> Map<K, V> convertYangKeyValueListToMap(@Nullable Iterable<T> yangList,
69                                                                    @NonNull Function<T, K> keyExtractor,
70                                                                    @NonNull Function<T, V> valueExtractor) {
71         return copyYangKeyValueListToMap(new HashMap<>(), yangList, keyExtractor, valueExtractor);
72     }
73 }