[SDC] rebase 1710 code
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / MapDataDefinition.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.HashMap;
24 import java.util.Map;
25
26 import org.codehaus.jackson.annotate.JsonCreator;
27 import org.codehaus.jackson.annotate.JsonValue;
28 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
29
30 public  class MapDataDefinition <T extends ToscaDataDefinition>  extends ToscaDataDefinition  {
31         
32         protected Map<String, T > mapToscaDataDefinition;
33         
34         public MapDataDefinition(MapDataDefinition<T> cdt) {
35                 super();
36                 mapToscaDataDefinition = cdt.mapToscaDataDefinition;    
37                 
38         }
39         @JsonCreator
40         public MapDataDefinition(Map<String, T > mapToscaDataDefinition) {
41                 super();
42                 this.mapToscaDataDefinition = mapToscaDataDefinition;   
43         }
44
45         public MapDataDefinition() {
46                 super();
47         }
48         @JsonValue
49         public Map<String, T > getMapToscaDataDefinition() {
50                 return mapToscaDataDefinition;
51         }
52         
53         public void put(String key, T value){
54                 if(mapToscaDataDefinition == null){
55                         mapToscaDataDefinition = new HashMap<String, T>();
56                 }
57                 mapToscaDataDefinition.put(key, value);
58         }
59         
60         public void delete(String key){
61                 if(mapToscaDataDefinition != null && mapToscaDataDefinition.containsKey(key)){
62                         mapToscaDataDefinition.remove(key);
63                 }
64         }
65         
66         public T findByKey(String key){
67                 T value = null;
68                 if(mapToscaDataDefinition != null && mapToscaDataDefinition.containsKey(key)){
69                         value = mapToscaDataDefinition.get(key);
70                 }
71                 return value;
72         }
73         @Override
74         public void setOwnerIdIfEmpty(String ownerId) {
75                 if ( mapToscaDataDefinition != null ){
76                         mapToscaDataDefinition.entrySet().forEach(e -> e.getValue().setOwnerIdIfEmpty(ownerId));
77                 }
78         }
79
80
81         public String findKeyByItemUidMatch(String uid){
82                 if(null == mapToscaDataDefinition)
83                         return null;
84                 Map.Entry<String, T> entry = mapToscaDataDefinition.entrySet().stream().filter(e ->
85                                 e.getValue().findUidMatch(uid))
86                                 .findAny().orElse(null);
87                 if(null == entry)
88                         return null;
89                 return entry.getKey();
90         }
91         
92 }