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 com.google.common.base.Preconditions;
11 import java.util.HashMap;
13 import java.util.function.Function;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
18 * YANG utility functions.
20 public final class YangUtils {
22 * Prevent instantiation.
29 * Copies a list of YANG key-value items to the given map. Any {@code null} key or value will cause an error.
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.
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);
57 * Converts a list of YANG key-value items to a map.
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.
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);