[SDC] rebase 1710 code
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / ListDataDefinition.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.datatypes.elements;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.function.Predicate;
29 import java.util.stream.Collectors;
30
31 import org.codehaus.jackson.annotate.JsonCreator;
32 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
33 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
34
35
36 public class ListDataDefinition<T extends ToscaDataDefinition> extends ToscaDataDefinition {
37
38         protected List<T> listToscaDataDefinition;
39
40         public ListDataDefinition(ListDataDefinition<T> cdt) {
41                 super();
42                 listToscaDataDefinition = cdt.listToscaDataDefinition;
43
44         }
45
46         @JsonCreator
47         public ListDataDefinition(List<T> listToscaDataDefinition) {
48                 super();
49                 this.listToscaDataDefinition = listToscaDataDefinition;
50         }
51
52         public ListDataDefinition() {
53                 super();
54         }
55
56         public List<T> getListToscaDataDefinition() {
57                 return listToscaDataDefinition;
58         }
59
60         public void add(T value) {
61                 if (listToscaDataDefinition == null) {
62                         listToscaDataDefinition = new ArrayList<T>();
63                 }
64                 listToscaDataDefinition.add(value);
65         }
66
67         public void delete(T value) {
68                 if (listToscaDataDefinition != null) {
69                         listToscaDataDefinition.remove(value);
70                 }
71         }
72
73         @Override
74         public void setOwnerIdIfEmpty(String ownerId) {
75                 if (listToscaDataDefinition != null) {
76                         listToscaDataDefinition.forEach(e -> e.setOwnerIdIfEmpty(ownerId));
77                 }
78         }
79         
80         @Override
81         public <S extends ToscaDataDefinition> S mergeFunction(S other, boolean allowDefaultValueOverride){
82                 Map<String, T> mapByName = listToMapByName(listToscaDataDefinition);
83                 List<T> otherList = ((ListDataDefinition)other).getListToscaDataDefinition();
84                 for(T item : otherList){
85                         mapByName.merge((String)item.getToscaPresentationValue(JsonPresentationFields.NAME), item, (thisItem, otherItem) -> thisItem.mergeFunction(otherItem, allowDefaultValueOverride));
86                 }
87                 ((ListDataDefinition)other).listToscaDataDefinition = mapByName.values().stream().collect(Collectors.toList());
88                 return other;   
89         }
90
91         @Override
92         public boolean findUidMatch(String uid){
93                 return listToscaDataDefinition.stream()
94                                 .anyMatch(p -> p.findUidMatch(uid));
95         }
96
97 }