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;
27 import java.io.Serializable;
28 import java.util.ArrayList;
29 import java.util.List;
31 import java.util.Optional;
33 import java.util.stream.Collectors;
35 public class ListDataDefinition<T extends ToscaDataDefinition> extends ToscaDataDefinition implements Serializable {
37 protected List<T> listToscaDataDefinition;
39 public ListDataDefinition(ListDataDefinition<T> cdt) {
41 listToscaDataDefinition = cdt.listToscaDataDefinition;
45 public ListDataDefinition(List<T> listToscaDataDefinition) {
47 this.listToscaDataDefinition = listToscaDataDefinition;
51 public ListDataDefinition() {
53 this.listToscaDataDefinition = new ArrayList<>();
56 public List<T> getListToscaDataDefinition() {
57 return listToscaDataDefinition;
60 public void add(T value) {
61 if (listToscaDataDefinition == null) {
62 listToscaDataDefinition = new ArrayList<T>();
64 listToscaDataDefinition.add(value);
67 public void delete(T value) {
68 if (listToscaDataDefinition != null) {
69 listToscaDataDefinition.remove(value);
74 public void setOwnerIdIfEmpty(String ownerId) {
75 if (listToscaDataDefinition != null) {
76 listToscaDataDefinition.forEach(e -> e.setOwnerIdIfEmpty(ownerId));
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));
87 ((ListDataDefinition) other).listToscaDataDefinition = mapByName.values().stream().collect(Collectors.toList());
92 public boolean findUidMatch(String uid) {
93 return listToscaDataDefinition.stream().anyMatch(p -> p.findUidMatch(uid));
97 public <T extends ToscaDataDefinition> T removeByOwnerId(Set<String> ownerIdList) {
98 List<T> collect1 = (List<T>) listToscaDataDefinition.stream().filter(e -> ownerIdList.contains(e.getOwnerId())).collect(Collectors.toList());
99 ListDataDefinition listDef = new ListDataDefinition(collect1);
101 listToscaDataDefinition.removeIf(e -> ownerIdList.contains(e.getOwnerId()));
106 public <T extends ToscaDataDefinition> T updateIfExist(T other, boolean allowDefaultValueOverride) {
108 List<T> list = ((ListDataDefinition)other).getListToscaDataDefinition();
110 String nameFromPrev = (String)e.getToscaPresentationValue(JsonPresentationFields.NAME);
111 if ( nameFromPrev != null ){
112 Optional<T> findAny = (Optional<T>) listToscaDataDefinition.stream().filter(o->nameFromPrev.equals(e.getToscaPresentationValue(JsonPresentationFields.NAME))).findAny();
113 if ( findAny.isPresent() ){
114 e.mergeFunction(findAny.get(), allowDefaultValueOverride);
121 public boolean isEmpty(){
122 return listToscaDataDefinition == null || listToscaDataDefinition.isEmpty();