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 java.util.HashMap;
12 import java.util.function.Function;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
17 import com.google.common.base.Preconditions;
20 * YANG utility functions.
22 public final class YangUtils {
24 * Prevent instantiation.
31 * Copies a list of YANG key-value items to the given map. Any {@code null} key or value will cause an error.
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.
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);
59 * Converts a list of YANG key-value items to a map.
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.
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);