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