7695240d013a07ed88834cdb8a6330ba2f311b15
[aai/traversal.git] / aai-traversal / src / main / java / org / openecomp / aai / transforms / MapTraverser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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.aai.transforms;
22
23
24 import joptsimple.internal.Objects;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 public class MapTraverser {
32
33     private Converter converter;
34
35     public MapTraverser(Converter converter){
36         this.converter = converter;
37     }
38
39     public Map<String, Object> convertKeys(Map<String, Object> map){
40
41         Objects.ensureNotNull(map);
42
43         Map<String, Object> modifiedMap = new HashMap<String, Object>();
44         convertKeys(map, modifiedMap);
45
46         return modifiedMap;
47     }
48
49     private Map<String, Object> convertKeys(Map<String, Object> original, Map<String, Object> modified){
50
51         for(Map.Entry<String, Object> entry : original.entrySet()){
52             String key = entry.getKey();
53             key = converter.convert(key);
54             Object value = entry.getValue();
55             if(value instanceof Map){
56                 modified.put(key, convertKeys((Map<String, Object>)value, new HashMap<String, Object>()));
57             } else if(value instanceof List){
58                 modified.put(key, convertKeys((List<Object>) value));
59             } else {
60                 modified.put(key, value);
61             }
62         }
63
64         return modified;
65     }
66
67     public List<Object> convertKeys(List<Object> list){
68
69         List<Object> modifiedList = new ArrayList<Object>();
70         if(list != null && list.size() > 0){
71
72             for(Object o : list){
73                 if(o instanceof Map){
74                     Map<String, Object> map = (Map<String, Object>) o;
75                     modifiedList.add(convertKeys(map));
76                 } else if(o instanceof List){
77                     List<Object> l = (List<Object>) o;
78                     modifiedList.add(convertKeys(l));
79                 } else {
80                     modifiedList.add(o);
81                 }
82             }
83         }
84
85         return modifiedList;
86     }
87 }