2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.icloud.internal.utilities;
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
24 * This class implements util methods for list handling.
26 * @author Simon Spielmann - Initial contribution
30 public abstract class ListUtil {
36 * Replace entries in the given originalList with entries from replacements, if the have an equal key.
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
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())) {
57 result.addAll(replacements);