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