2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.dao.utils;
24 import java.util.Map.Entry;
26 public final class CollectionUtils {
27 private CollectionUtils() {
31 * Add the content of the 'source' Set to the 'target' set and return the
34 * If 'source' is null then a new set is created and returned. If 'target'
35 * is null then no content is added to the 'source' Set or newly created
38 * @param source The Set to merge in the target Set.
39 * @param target The Set in which the source set will be merged (through
41 * @return The target Set with addition of source Set elements, or a new Set
42 * (including content of source set) if target was null.
44 public static <T> Set<T> merge(Set<T> source, Set<T> target) {
45 Set<T> merged = new HashSet<>();
47 merged.addAll(target);
50 merged.addAll(source);
52 return merged.isEmpty() ? null : merged;
57 * Add the content of the 'source' Map to the 'target' set and return the
61 * If 'source' is null then a new Map is created and returned. If 'target'
62 * is null then no content is added to the 'source' Map or newly created
66 * @param source The Map to merge in the target Map.
67 * @param target The Map in which the source Map will be merged (through
69 * @param override If an key from the source map already exists in the target
70 * map, should it override (true) or not (false) the value.
71 * @return The target Map with addition of source Map elements, or a new Map
72 * (including content of source set) if target was null.
74 public static <T, V> Map<T, V> merge(Map<T, ? extends V> source, Map<T, V> target, boolean override) {
76 target = new HashMap();
80 for (Entry<T, ? extends V> entry : source.entrySet()) {
81 if (override || !target.containsKey(entry.getKey())) {
82 target.put(entry.getKey(), entry.getValue());
86 return target.isEmpty() ? null : target;
90 * Merge two lists, the merge is performed based on the contains method so
91 * elements presents both in source and target are not added twice to the
94 * @param source The source list.
95 * @param target The target list.
96 * @return A list that represents the merged collections.
98 public static <T> List<T> merge(List<T> source, List<T> target) {
99 List<T> merged = target == null ? new ArrayList<>() : target;
101 if (source == null) {
106 if (!merged.contains(t)) {
115 * Returns a new list containing the second list appended to the
116 * first list. The {@link List#addAll(Collection)} operation is
117 * used to append the two given lists into a new list.
119 * @param list1 the first list
120 * @param list2 the second list
121 * @return a new list containing the union of those lists
122 * @throws NullPointerException if either list is null
124 public static <T> List<T> union(List<T> list1, List<T> list2) {
125 List<T> result = new ArrayList<>(list1);
126 result.addAll(list2);