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