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.datatypes.elements;
23 import com.fasterxml.jackson.annotation.JsonCreator;
24 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
25 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
28 import java.util.stream.Collectors;
30 public class ListDataDefinition<T extends ToscaDataDefinition> extends ToscaDataDefinition {
32 protected List<T> listToscaDataDefinition;
34 public ListDataDefinition(ListDataDefinition<T> cdt) {
35 listToscaDataDefinition = cdt.listToscaDataDefinition;
39 public ListDataDefinition(List<T> listToscaDataDefinition) {
40 this.listToscaDataDefinition = listToscaDataDefinition;
44 public ListDataDefinition() {
45 this.listToscaDataDefinition = new ArrayList<>();
48 public List<T> getListToscaDataDefinition() {
49 return listToscaDataDefinition;
52 public void add(T value) {
53 if (listToscaDataDefinition == null) {
54 listToscaDataDefinition = new ArrayList<>();
56 listToscaDataDefinition.add(value);
59 public void delete(T value) {
60 if (listToscaDataDefinition != null) {
61 listToscaDataDefinition.remove(value);
66 public void setOwnerIdIfEmpty(String ownerId) {
67 if (listToscaDataDefinition != null) {
68 listToscaDataDefinition.forEach(e -> e.setOwnerIdIfEmpty(ownerId));
73 public <S extends ToscaDataDefinition> S mergeFunction(S other, boolean allowDefaultValueOverride) {
74 Map<String, T> mapByName = listToMapByName(listToscaDataDefinition);
75 List<T> otherList = ((ListDataDefinition) other).getListToscaDataDefinition();
76 for (T item : otherList) {
77 mapByName.merge((String) item.getToscaPresentationValue(JsonPresentationFields.NAME), item, (thisItem, otherItem) -> thisItem.mergeFunction(otherItem, allowDefaultValueOverride));
79 ((ListDataDefinition) other).listToscaDataDefinition = mapByName.values().stream().collect(Collectors.toList());
84 public boolean findUidMatch(String uid) {
85 return listToscaDataDefinition.stream().anyMatch(p -> p.findUidMatch(uid));
89 public <T extends ToscaDataDefinition> T removeByOwnerId(Set<String> ownerIdList) {
90 List<T> collect1 = (List<T>) listToscaDataDefinition.stream().filter(e -> ownerIdList.contains(e.getOwnerId())).collect(Collectors.toList());
91 ListDataDefinition listDef = new ListDataDefinition(collect1);
93 listToscaDataDefinition.removeIf(e -> ownerIdList.contains(e.getOwnerId()));
98 public <T extends ToscaDataDefinition> T updateIfExist(T other, boolean allowDefaultValueOverride) {
100 List<T> list = ((ListDataDefinition)other).getListToscaDataDefinition();
102 String nameFromPrev = (String)e.getToscaPresentationValue(JsonPresentationFields.NAME);
103 if ( nameFromPrev != null ){
104 Optional<T> findAny = (Optional<T>) listToscaDataDefinition.stream().filter(o->nameFromPrev.equals(e.getToscaPresentationValue(JsonPresentationFields.NAME))).findAny();
105 if ( findAny.isPresent() ){
106 e.mergeFunction(findAny.get(), allowDefaultValueOverride);
113 public boolean isEmpty(){
114 return listToscaDataDefinition == null || listToscaDataDefinition.isEmpty();