bc9a66ada5b10b339ec902adfb2e3f5fb56ac0ce
[appc.git] / appc-common / src / main / java / org / openecomp / appc / util / ObjectMapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.util;
24
25 import java.lang.reflect.Array;
26 import java.util.Map;
27
28 public class ObjectMapper {
29
30         private ObjectMapper() {
31         }
32
33         private static void dispatch(PathContext context, Object obj) {
34
35                 if (obj == null) {
36                         return;
37                 }
38
39                 final Class<?> cls = obj.getClass();
40
41                 if (cls.isPrimitive()
42                                 || String.class.isAssignableFrom(cls)
43                                 || Number.class.isAssignableFrom(cls)
44                                 || Boolean.class.isAssignableFrom(cls)) {
45                         handlePrimitive(context, obj);
46                 } else if (cls.isArray()) {
47                         handleArray(context, obj);
48                 } else if (Map.class.isAssignableFrom(cls)) {
49                         handleMap(context, (Map<?, ?>) obj);
50                 } else if (Iterable.class.isAssignableFrom(cls)) {
51                         handleCollection(context, Iterable.class.cast(obj));
52                 } else {
53                         throw new IllegalArgumentException(obj.getClass().getName());
54                 }
55         }
56
57         public static Map<String, String> map(Object obj) {
58                 PathContext context = new PathContext();
59                 dispatch(context, obj);
60                 return context.entries();
61         }
62
63         private static void handleMap(PathContext context, Map<?, ?> val) {
64                 for (Map.Entry<?, ?> entry : val.entrySet()) {
65                         context.pushToken(entry.getKey().toString());
66                         dispatch(context, entry.getValue());
67                         context.popToken();
68                 }
69         }
70
71         private static void handleCollection(PathContext context, Iterable<?> val) {
72                 int index = 0;
73                 for (Object elem : val) {
74                         handleElement(context, index++, elem);
75                 }
76         }
77
78         private static void handleArray(PathContext context, Object val) {
79                 for (int i = 0, n = Array.getLength(val); i < n; i++) {
80                         handleElement(context, i, Array.get(val, i));
81                 }
82         }
83
84         private static void handleElement(PathContext context, int index, Object val) {
85                 if (val == null) {
86                         return;
87                 }
88
89                 String modifier = new StringBuilder().append('[').append(Integer.valueOf(index)).append(']').toString();
90
91                 context.pushModifier(modifier);
92                 dispatch(context, val);
93                 context.popModifier();
94         }
95
96         private static void handlePrimitive(PathContext context, Object val) {
97                 context.entry(context.getPath(), val.toString());
98         }
99 }