re base code
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / utils / CollectionUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.dao.utils;
22
23 import java.util.*;
24 import java.util.Map.Entry;
25
26 public final class CollectionUtils {
27     private CollectionUtils() {
28     }
29
30     /**
31      * Add the content of the 'source' Set to the 'target' set and return the
32      * union set.
33      * <p>
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
36      * set.
37      *
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
40      *               addAll).
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.
43      */
44     public static <T> Set<T> merge(Set<T> source, Set<T> target) {
45         Set<T> merged = new HashSet<>();
46         if (target != null) {
47             merged.addAll(target);
48         }
49         if (source != null) {
50             merged.addAll(source);
51         }
52         return merged.isEmpty() ? null : merged;
53     }
54
55     /**
56      * <p>
57      * Add the content of the 'source' Map to the 'target' set and return the
58      * union Map.
59      * </p>
60      * <p>
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
63      * Map.
64      * </p>
65      *
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
68      *                 addAll).
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.
73      */
74     public static <T, V> Map<T, V> merge(Map<T, ? extends V> source, Map<T, V> target, boolean override) {
75         if (target == null) {
76             target = new HashMap();
77         }
78
79         if (source != null) {
80             for (Entry<T, ? extends V> entry : source.entrySet()) {
81                 if (override || !target.containsKey(entry.getKey())) {
82                     target.put(entry.getKey(), entry.getValue());
83                 }
84             }
85         }
86         return target.isEmpty() ? null : target;
87     }
88
89     /**
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
92      * list.
93      *
94      * @param source The source list.
95      * @param target The target list.
96      * @return A list that represents the merged collections.
97      */
98     public static <T> List<T> merge(List<T> source, List<T> target) {
99         List<T> merged = target == null ? new ArrayList<>() : target;
100
101         if (source == null) {
102             return merged;
103         }
104
105         for (T t : source) {
106             if (!merged.contains(t)) {
107                 merged.add(t);
108             }
109         }
110
111         return merged;
112     }
113
114     /**
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.
118      *
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
123      */
124     public static <T> List<T> union(List<T> list1, List<T> list2) {
125         List<T> result = new ArrayList<>(list1);
126         result.addAll(list2);
127         return result;
128     }
129 }