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