Change nexus values to properties
[appc.git] / app-c / appc / appc-common / src / main / java / org / openecomp / appc / util / ObjectMapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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  */
21
22 package org.openecomp.appc.util;
23
24 import java.lang.reflect.Array;
25 import java.util.Map;
26
27 public class ObjectMapper {
28
29         private ObjectMapper() {
30         }
31
32         private static void dispatch(PathContext context, Object obj) {
33
34                 if (obj == null) {
35                         return;
36                 }
37
38                 final Class<?> cls = obj.getClass();
39
40                 if (cls.isPrimitive()
41                                 || String.class.isAssignableFrom(cls)
42                                 || Number.class.isAssignableFrom(cls)
43                                 || Boolean.class.isAssignableFrom(cls)) {
44                         handlePrimitive(context, obj);
45                 } else if (cls.isArray()) {
46                         handleArray(context, obj);
47                 } else if (Map.class.isAssignableFrom(cls)) {
48                         handleMap(context, (Map<?, ?>) obj);
49                 } else if (Iterable.class.isAssignableFrom(cls)) {
50                         handleCollection(context, Iterable.class.cast(obj));
51                 } else {
52                         throw new IllegalArgumentException(obj.getClass().getName());
53                 }
54         }
55
56         public static Map<String, String> map(Object obj) {
57                 PathContext context = new PathContext();
58                 dispatch(context, obj);
59                 return context.entries();
60         }
61
62         private static void handleMap(PathContext context, Map<?, ?> val) {
63                 for (Map.Entry<?, ?> entry : val.entrySet()) {
64                         context.pushToken(entry.getKey().toString());
65                         dispatch(context, entry.getValue());
66                         context.popToken();
67                 }
68         }
69
70         private static void handleCollection(PathContext context, Iterable<?> val) {
71                 int index = 0;
72                 for (Object elem : val) {
73                         handleElement(context, index++, elem);
74                 }
75         }
76
77         private static void handleArray(PathContext context, Object val) {
78                 for (int i = 0, n = Array.getLength(val); i < n; i++) {
79                         handleElement(context, i, Array.get(val, i));
80                 }
81         }
82
83         private static void handleElement(PathContext context, int index, Object val) {
84                 if (val == null) {
85                         return;
86                 }
87
88                 String modifier = new StringBuilder().append('[').append(Integer.valueOf(index)).append(']').toString();
89
90                 context.pushModifier(modifier);
91                 dispatch(context, val);
92                 context.popModifier();
93         }
94
95         private static void handlePrimitive(PathContext context, Object val) {
96                 context.entry(context.getPath(), val.toString());
97         }
98 }