]> git.basschouten.com Git - openhab-addons.git/blob
ab4061485e7cde833415a2c0b936d9ae7f0e74ee
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.icloud.internal.utilities;
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22
23 /**
24  * This class implements util methods for list handling.
25  *
26  * @author Simon Spielmann - Initial contribution
27  *
28  */
29 @NonNullByDefault
30 public abstract class ListUtil {
31
32     private ListUtil() {
33     };
34
35     /**
36      * Replace entries in the given originalList with entries from replacements, if the have an equal key.
37      *
38      * @param <K> Type of first pair element
39      * @param <V> Type of second pair element
40      * @param originalList List with entries to replace
41      * @param replacements Replacement entries
42      * @return New list with replaced entries
43      */
44     public static <K extends @NonNull Object, V extends @NonNull Object> List<Pair<K, V>> replaceEntries(
45             List<Pair<K, V>> originalList, @Nullable List<Pair<K, V>> replacements) {
46         List<Pair<K, V>> result = new ArrayList<>(originalList);
47         if (replacements != null) {
48             Iterator<Pair<K, V>> it = result.iterator();
49             while (it.hasNext()) {
50                 Pair<K, V> requestHeader = it.next();
51                 for (Pair<K, V> replacementHeader : replacements) {
52                     if (requestHeader.getKey().equals(replacementHeader.getKey())) {
53                         it.remove();
54                     }
55                 }
56             }
57             result.addAll(replacements);
58         }
59         return result;
60     }
61 }