Fix checkstyle violations in sdc-main/common
[sdc.git] / common / onap-tosca-datatype / src / main / java / org / onap / sdc / tosca / services / DataModelCloneUtil.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.sdc.tosca.services;
18
19
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.stream.Collectors;
26 import org.onap.sdc.tosca.datatypes.model.AttributeDefinition;
27 import org.onap.sdc.tosca.datatypes.model.Constraint;
28 import org.onap.sdc.tosca.datatypes.model.OperationDefinition;
29 import org.onap.sdc.tosca.datatypes.model.PropertyDefinition;
30 import org.onap.sdc.tosca.error.ToscaRuntimeException;
31
32 public class DataModelCloneUtil {
33
34     private DataModelCloneUtil() {
35         throw new IllegalStateException("Utility class");
36     }
37
38     /**
39      * Clone constraints list.
40      *
41      * @param constraints the constraints
42      * @return the list
43      */
44     public static List<Constraint> cloneConstraints(List<Constraint> constraints) {
45
46         if (constraints == null) {
47             return null;
48         }
49         return constraints.stream().map(Constraint::clone).collect(Collectors.toList());
50     }
51
52     /**
53      * Clone property definitions map.
54      *
55      * @param propertyDefinitions the property definitions
56      * @return the map
57      */
58     public static Map<String, PropertyDefinition> clonePropertyDefinitions(
59             Map<String, PropertyDefinition> propertyDefinitions) {
60         if (propertyDefinitions == null) {
61             return null;
62         }
63         Map<String, PropertyDefinition> clonedProperties = new HashMap<>();
64         for (Map.Entry<String, PropertyDefinition> propertyDefinitionEntry : propertyDefinitions.entrySet()) {
65             clonedProperties.put(propertyDefinitionEntry.getKey(), propertyDefinitionEntry.getValue().clone());
66         }
67
68         return clonedProperties;
69     }
70
71     /**
72      * Clone attribute definitions map.
73      *
74      * @param attributeDefinitions the attribute definitions
75      * @return the map
76      */
77     public static Map<String, AttributeDefinition> cloneAttributeDefinitions(
78             Map<String, AttributeDefinition> attributeDefinitions) {
79
80         if (attributeDefinitions == null) {
81             return null;
82         }
83         Map<String, AttributeDefinition> clonedAttributeDefinitions = new HashMap<>();
84         for (Map.Entry<String, AttributeDefinition> attributeDefinitionEntry : attributeDefinitions.entrySet()) {
85             clonedAttributeDefinitions
86                     .put(attributeDefinitionEntry.getKey(), attributeDefinitionEntry.getValue().clone());
87         }
88
89         return clonedAttributeDefinitions;
90     }
91
92     /**
93      * Clone Map of key String and value String .
94      *
95      * @param stringStringMap the map that will be cloned
96      * @return the cloned map
97      */
98     public static Map<String, String> cloneStringStringMap(Map<String, String> stringStringMap) {
99         if (Objects.isNull(stringStringMap)) {
100             return null;
101         }
102         return new HashMap<>(stringStringMap);
103     }
104
105     /**
106      * Clone Map of key String and value PropertyDefinition .
107      *
108      * @param stringPropertyDefinitionMap the map that will be cloned
109      * @return the cloned map
110      */
111     public static Map<String, PropertyDefinition> cloneStringPropertyDefinitionMap(
112             Map<String, PropertyDefinition> stringPropertyDefinitionMap) {
113         if (Objects.isNull(stringPropertyDefinitionMap)) {
114             return null;
115         }
116
117         Map<String, PropertyDefinition> cloneMap = new HashMap<>();
118         ToscaExtensionYamlUtil toscaExtYamlUtil = new ToscaExtensionYamlUtil();
119         for (Map.Entry<String, PropertyDefinition> mapEntry : stringPropertyDefinitionMap.entrySet()) {
120             PropertyDefinition propertyDefinition = toscaExtYamlUtil.yamlToObject(
121                     toscaExtYamlUtil.objectToYaml(mapEntry.getValue()), PropertyDefinition.class);
122             cloneMap.put(mapEntry.getKey(), propertyDefinition.clone());
123         }
124         return cloneMap;
125     }
126
127     /**
128      * Clone Map of key String and value OperationDefinition or class which is extends from OperationDefinition .
129      *
130      * @param input the map that will be cloned
131      * @return the cloned map
132      */
133     public static <T extends OperationDefinition> Map<String, T> cloneStringOperationDefinitionMap(
134             Map<String, T> input) {
135
136         if (Objects.isNull(input)) {
137             return null;
138         }
139
140         Map<String, OperationDefinition> cloneMap = new HashMap<>();
141         for (Map.Entry<String, T> mapEntry : input.entrySet()) {
142             cloneMap.put(mapEntry.getKey(), Objects.isNull(mapEntry.getValue()) ? null : mapEntry.getValue().clone());
143         }
144
145         return (Map<String, T>) cloneMap;
146     }
147
148     /**
149      * Clone Map of key String and value Object .
150      *
151      * @param stringObjectMap the map that will be cloned
152      * @return the cloned map
153      */
154     public static Map<String, Object> cloneStringObjectMap(Map<String, Object> stringObjectMap) {
155         if (Objects.isNull(stringObjectMap)) {
156             return null;
157         }
158
159         Map<String, Object> cloneMap = new HashMap<>();
160         for (Map.Entry<String, Object> mapEntry : stringObjectMap.entrySet()) {
161             YamlUtil yamlUtil = new YamlUtil();
162             if (mapEntry.getValue() instanceof Map) {
163                 Map cloneObj = yamlUtil.yamlToObject(yamlUtil.objectToYaml(mapEntry.getValue()), Map.class);
164                 cloneMap.put(mapEntry.getKey(), cloneObj);
165             } else if (mapEntry.getValue() instanceof List) {
166                 List cloneObj = yamlUtil.yamlToObject(yamlUtil.objectToYaml(mapEntry.getValue()), List.class);
167                 cloneMap.put(mapEntry.getKey(), cloneObj);
168             } else if (mapEntry.getValue() instanceof Cloneable) {
169                 throw new ToscaRuntimeException("Clone Not Supported Exception");
170             } else {
171                 cloneMap.put(mapEntry.getKey(), mapEntry.getValue());
172             }
173         }
174         return cloneMap;
175     }
176
177     /**
178      * Clone List of String.
179      *
180      * @param listString the list that will be cloned
181      * @return the cloned list
182      */
183     public static List<String> cloneListString(List<String> listString) {
184         if (Objects.isNull(listString)) {
185             return null;
186         }
187         return new ArrayList<>(listString);
188     }
189
190
191 }