Browse Source

added ludbo map support

tags/v1.0.0
Robin Thoni 7 years ago
parent
commit
bcac215e11

+ 39
- 14
luticateutils/src/main/java/com/luticate/utils/dbo/LuDbo.java View File

11
 import java.lang.reflect.Type;
11
 import java.lang.reflect.Type;
12
 import java.util.AbstractMap;
12
 import java.util.AbstractMap;
13
 import java.util.HashMap;
13
 import java.util.HashMap;
14
+import java.util.Iterator;
14
 import java.util.List;
15
 import java.util.List;
16
+import java.util.Objects;
15
 import java.util.Vector;
17
 import java.util.Vector;
16
 
18
 
17
 /**
19
 /**
25
         fromJson(json);
27
         fromJson(json);
26
     }
28
     }
27
 
29
 
28
-    public static String getFieldFromJson(String jsonName)
30
+    public String getFieldFromJson(String jsonName)
29
     {
31
     {
30
         if (!jsonName.startsWith("_")) {
32
         if (!jsonName.startsWith("_")) {
31
             return String.format("_%s", jsonName);
33
             return String.format("_%s", jsonName);
33
         return jsonName;
35
         return jsonName;
34
     }
36
     }
35
 
37
 
36
-    public static String getJsonFromField(String fieldName)
38
+    public String getJsonFromField(String fieldName)
37
     {
39
     {
38
         if (fieldName.startsWith("_")) {
40
         if (fieldName.startsWith("_")) {
39
             return fieldName.substring(1);
41
             return fieldName.substring(1);
41
         return fieldName;
43
         return fieldName;
42
     }
44
     }
43
 
45
 
44
-    public static Object getObjectFromJson(Class clazz, JSONObject json, String key) throws Exception {
46
+    public Object getObjectFromJson(Class clazz, JSONObject json, String key) throws Exception {
45
         if (json.isNull(key)) {
47
         if (json.isNull(key)) {
46
             return null;
48
             return null;
47
         }
49
         }
74
             return json.getString(key);
76
             return json.getString(key);
75
         }
77
         }
76
         if (LuDbo.class.isAssignableFrom(clazz)) {
78
         if (LuDbo.class.isAssignableFrom(clazz)) {
77
-            LuDbo dbo = (LuDbo) clazz.newInstance();
79
+            LuDbo dbo = (LuDbo) getInstance(clazz);
78
             dbo.fromJson(json.getJSONObject(key));
80
             dbo.fromJson(json.getJSONObject(key));
79
             return dbo;
81
             return dbo;
80
         }
82
         }
81
         return null;
83
         return null;
82
     }
84
     }
83
 
85
 
84
-    public static Object getObjectFromJson(Class clazz, JSONArray json, int key) throws Exception {
86
+    public Object getObjectFromJson(Class clazz, JSONArray json, int key) throws Exception {
85
         if (json.isNull(key)) {
87
         if (json.isNull(key)) {
86
             return null;
88
             return null;
87
         }
89
         }
114
             return json.getString(key);
116
             return json.getString(key);
115
         }
117
         }
116
         if (LuDbo.class.isAssignableFrom(clazz)) {
118
         if (LuDbo.class.isAssignableFrom(clazz)) {
117
-            LuDbo dbo = (LuDbo) clazz.newInstance();
119
+            LuDbo dbo = (LuDbo) getInstance(clazz);
118
             dbo.fromJson(json.getJSONObject(key));
120
             dbo.fromJson(json.getJSONObject(key));
119
             return dbo;
121
             return dbo;
120
         }
122
         }
121
         return null;
123
         return null;
122
     }
124
     }
123
 
125
 
126
+    public Object getInstance(Class clazz) throws Exception {
127
+        if (clazz == List.class) {
128
+            return new Vector<>();
129
+        }
130
+        if (clazz == AbstractMap.class) {
131
+            return new HashMap<>();
132
+        }
133
+        return clazz.newInstance();
134
+    }
135
+
124
     public void fromJson(JSONObject json) throws Exception
136
     public void fromJson(JSONObject json) throws Exception
125
     {
137
     {
126
         for (Field field : getClass().getDeclaredFields()) {
138
         for (Field field : getClass().getDeclaredFields()) {
131
                 if (List.class.isAssignableFrom(clazz)) {
143
                 if (List.class.isAssignableFrom(clazz)) {
132
                     ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
144
                     ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
133
                     Class type = (Class) parameterizedType.getActualTypeArguments()[0];
145
                     Class type = (Class) parameterizedType.getActualTypeArguments()[0];
134
-                    List<Object> list = (clazz == List.class ? new Vector<>() : (List<Object>) clazz.newInstance());
146
+                    List<Object> list = (List<Object>) getInstance(clazz);
135
                     JSONArray jsonArray = json.getJSONArray(key);
147
                     JSONArray jsonArray = json.getJSONArray(key);
136
                     for (int i = 0; i < jsonArray.length(); ++i) {
148
                     for (int i = 0; i < jsonArray.length(); ++i) {
137
                         list.add(getObjectFromJson(type, jsonArray, i));
149
                         list.add(getObjectFromJson(type, jsonArray, i));
147
                     }
159
                     }
148
                     value = array;
160
                     value = array;
149
                 }
161
                 }
150
-//                else if (AbstractMap.class.isAssignableFrom(clazz)) {
151
-//                    ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
152
-//                    Class type = (Class) parameterizedType.getActualTypeArguments()[0];
153
-//                    if (type == String.class) {
154
-//                        type = (Class) parameterizedType.getActualTypeArguments()[1];
155
-//                    }
156
-//                }
162
+                else if (AbstractMap.class.isAssignableFrom(clazz)) {
163
+                    ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
164
+                    Class type = (Class) parameterizedType.getActualTypeArguments()[0];
165
+                    if (type == String.class) {
166
+                        type = (Class) parameterizedType.getActualTypeArguments()[1];
167
+                        AbstractMap<String, Object> map = (AbstractMap<String, Object>) getInstance(clazz);
168
+                        JSONObject obj = json.getJSONObject(key);
169
+                        Iterator<String> it = obj.keys();
170
+                        while (it.hasNext()) {
171
+                            String k = it.next();
172
+                            map.put(k, getObjectFromJson(type, obj, k));
173
+                        }
174
+                        value = map;
175
+                    }
176
+                }
157
                 else {
177
                 else {
158
                     value = getObjectFromJson(clazz, json, key);
178
                     value = getObjectFromJson(clazz, json, key);
159
                 }
179
                 }
168
     {
188
     {
169
         HashMap<String, Object> map = new HashMap<>();
189
         HashMap<String, Object> map = new HashMap<>();
170
 
190
 
191
+        for (Field field : getClass().getDeclaredFields()) {
192
+            String key = getJsonFromField(field.getName());
193
+            Class clazz = field.getType();
194
+        }
195
+
171
         return map;
196
         return map;
172
     }
197
     }
173
 
198
 

+ 11
- 8
luticateutils/src/test/java/com/luticate/utils/NameDboTest.java View File

1
 package com.luticate.utils;
1
 package com.luticate.utils;
2
 
2
 
3
 import com.luticate.utils.dbo.LuDbo;
3
 import com.luticate.utils.dbo.LuDbo;
4
+import com.luticate.utils.dbo.LuVoidDbo;
4
 
5
 
5
 import org.junit.Test;
6
 import org.junit.Test;
6
 
7
 
15
     @Test
16
     @Test
16
     public void jsonFromFieldTest()
17
     public void jsonFromFieldTest()
17
     {
18
     {
18
-        assertEquals("byte", LuDbo.getJsonFromField("_byte"));
19
-        assertEquals("anotherVariable", LuDbo.getJsonFromField("_anotherVariable"));
20
-        assertEquals("byte", LuDbo.getJsonFromField("byte"));
21
-        assertEquals("anotherVariable", LuDbo.getJsonFromField("anotherVariable"));
19
+        LuDbo dbo = new LuVoidDbo();
20
+        assertEquals("byte", dbo.getJsonFromField("_byte"));
21
+        assertEquals("anotherVariable", dbo.getJsonFromField("_anotherVariable"));
22
+        assertEquals("byte", dbo.getJsonFromField("byte"));
23
+        assertEquals("anotherVariable", dbo.getJsonFromField("anotherVariable"));
22
     }
24
     }
23
 
25
 
24
     @Test
26
     @Test
25
     public void fieldFromJsonTest()
27
     public void fieldFromJsonTest()
26
     {
28
     {
27
-        assertEquals("_byte", LuDbo.getFieldFromJson("byte"));
28
-        assertEquals("_anotherVariable", LuDbo.getFieldFromJson("anotherVariable"));
29
-        assertEquals("_byte", LuDbo.getFieldFromJson("_byte"));
30
-        assertEquals("_anotherVariable", LuDbo.getFieldFromJson("_anotherVariable"));
29
+        LuDbo dbo = new LuVoidDbo();
30
+        assertEquals("_byte", dbo.getFieldFromJson("byte"));
31
+        assertEquals("_anotherVariable", dbo.getFieldFromJson("anotherVariable"));
32
+        assertEquals("_byte", dbo.getFieldFromJson("_byte"));
33
+        assertEquals("_anotherVariable", dbo.getFieldFromJson("_anotherVariable"));
31
     }
34
     }
32
 }
35
 }

+ 19
- 0
luticateutils/src/test/java/com/luticate/utils/Test2Dbo.java View File

27
     public void setaInt(int aInt) {
27
     public void setaInt(int aInt) {
28
         _aInt = aInt;
28
         _aInt = aInt;
29
     }
29
     }
30
+
31
+    @Override
32
+    public boolean equals(Object o) {
33
+        if (this == o) return true;
34
+        if (o == null || getClass() != o.getClass()) return false;
35
+
36
+        Test2Dbo test2Dbo = (Test2Dbo) o;
37
+
38
+        if (_aInt != test2Dbo._aInt) return false;
39
+        return _aString != null ? _aString.equals(test2Dbo._aString) : test2Dbo._aString == null;
40
+
41
+    }
42
+
43
+    @Override
44
+    public int hashCode() {
45
+        int result = _aString != null ? _aString.hashCode() : 0;
46
+        result = 31 * result + _aInt;
47
+        return result;
48
+    }
30
 }
49
 }

+ 22
- 4
luticateutils/src/test/java/com/luticate/utils/TestDbo.java View File

2
 
2
 
3
 import com.luticate.utils.dbo.LuDbo;
3
 import com.luticate.utils.dbo.LuDbo;
4
 
4
 
5
+import java.io.Serializable;
6
+import java.util.AbstractMap;
5
 import java.util.HashMap;
7
 import java.util.HashMap;
6
 import java.util.List;
8
 import java.util.List;
7
 
9
 
9
  * Created by robin on 10/20/16.
11
  * Created by robin on 10/20/16.
10
  */
12
  */
11
 
13
 
12
-public class TestDbo extends LuDbo {
14
+public class TestDbo extends LuDbo implements Serializable {
13
     protected byte _byte;
15
     protected byte _byte;
14
     protected char _char;
16
     protected char _char;
15
     protected short _short;
17
     protected short _short;
31
     protected String _string;
33
     protected String _string;
32
 
34
 
33
     protected Test2Dbo _test2Dbo;
35
     protected Test2Dbo _test2Dbo;
34
-
35
     protected Test2Dbo _test2Dbo2;
36
     protected Test2Dbo _test2Dbo2;
36
 
37
 
37
     protected List<Integer> _integers;
38
     protected List<Integer> _integers;
38
-
39
     protected int[] _ints;
39
     protected int[] _ints;
40
 
40
 
41
     protected List<Test2Dbo> _test2Dbos;
41
     protected List<Test2Dbo> _test2Dbos;
42
-
43
     protected Test2Dbo[] _dbos;
42
     protected Test2Dbo[] _dbos;
44
 
43
 
44
+    protected HashMap<String, Integer> _hashMap;
45
+    protected AbstractMap<String, Test2Dbo> _hashMap2;
46
+
45
     public byte getByte() {
47
     public byte getByte() {
46
         return _byte;
48
         return _byte;
47
     }
49
     }
225
     public void setDbos(Test2Dbo[] dbos) {
227
     public void setDbos(Test2Dbo[] dbos) {
226
         _dbos = dbos;
228
         _dbos = dbos;
227
     }
229
     }
230
+
231
+    public HashMap<String, Integer> getHashMap() {
232
+        return _hashMap;
233
+    }
234
+
235
+    public void setHashMap(HashMap<String, Integer> hashMap) {
236
+        _hashMap = hashMap;
237
+    }
238
+
239
+    public AbstractMap<String, Test2Dbo> getHashMap2() {
240
+        return _hashMap2;
241
+    }
242
+
243
+    public void setHashMap2(AbstractMap<String, Test2Dbo> hashMap2) {
244
+        _hashMap2 = hashMap2;
245
+    }
228
 }
246
 }

+ 49
- 8
luticateutils/src/test/java/com/luticate/utils/TestDboTest.java View File

16
         TestDbo test = new TestDbo();
16
         TestDbo test = new TestDbo();
17
         test.fromString("{" +
17
         test.fromString("{" +
18
                 "\"byte\": 42," +
18
                 "\"byte\": 42," +
19
-                "\"byteObject\": 24," +
20
                 "\"char\": \"h\"," +
19
                 "\"char\": \"h\"," +
21
-                "\"charObject\": \"w\"," +
22
                 "\"short\": 4200," +
20
                 "\"short\": 4200," +
23
-                "\"shortObject\": 2400," +
24
                 "\"int\": 420042," +
21
                 "\"int\": 420042," +
25
-                "\"intObject\": 240024," +
26
                 "\"long\": 42004200," +
22
                 "\"long\": 42004200," +
27
-                "\"longObject\": 24002400," +
28
                 "\"float\": 42.24," +
23
                 "\"float\": 42.24," +
29
-                "\"floatObject\": 24.42," +
30
                 "\"double\": 4200.0024," +
24
                 "\"double\": 4200.0024," +
31
-                "\"doubleObject\": 2400.0042," +
32
                 "\"boolean\": true," +
25
                 "\"boolean\": true," +
26
+
27
+                "\"byteObject\": 24," +
28
+                "\"charObject\": \"w\"," +
29
+                "\"shortObject\": 2400," +
30
+                "\"intObject\": 240024," +
31
+                "\"longObject\": 24002400," +
32
+                "\"floatObject\": 24.42," +
33
+                "\"doubleObject\": 2400.0042," +
33
                 "\"booleanObject\": false," +
34
                 "\"booleanObject\": false," +
35
+
34
                 "\"string\": \"Hello World!\"," +
36
                 "\"string\": \"Hello World!\"," +
37
+
35
                 "\"test2Dbo\": {" +
38
                 "\"test2Dbo\": {" +
36
                 "\"aString\": \"a string\"," +
39
                 "\"aString\": \"a string\"," +
37
                 "\"aInt\": 4242" +
40
                 "\"aInt\": 4242" +
38
                 "}," +
41
                 "}," +
42
+
39
                 "\"integers\": [" +
43
                 "\"integers\": [" +
40
                 "0," +
44
                 "0," +
41
                 "42," +
45
                 "42," +
46
                 "42," +
50
                 "42," +
47
                 "0" +
51
                 "0" +
48
                 "]," +
52
                 "]," +
53
+
49
                 "test2Dbos: [" +
54
                 "test2Dbos: [" +
50
                 "{" +
55
                 "{" +
51
                 "\"aString\": \"42 42\"," +
56
                 "\"aString\": \"42 42\"," +
57
                 "\"aString\": \"24 24\"," +
62
                 "\"aString\": \"24 24\"," +
58
                 "\"aInt\": 2424" +
63
                 "\"aInt\": 2424" +
59
                 "}" +
64
                 "}" +
60
-                "]" +
65
+                "]," +
66
+
67
+                "\"hashMap\": {" +
68
+                "\"forty-two\": 42," +
69
+                "\"twenty-four\": 24" +
70
+                "}," +
71
+
72
+                "\"hashMap2\": {" +
73
+                "\"an object\": {" +
74
+                "\"aString\": \"42 24\"," +
75
+                "\"aInt\": 4224" +
76
+                "}" +
77
+                "}" +
78
+
61
                 "}");
79
                 "}");
62
         assertEquals(42, test.getByte());
80
         assertEquals(42, test.getByte());
63
         assertEquals(24, test.getByteObject().byteValue());
81
         assertEquals(24, test.getByteObject().byteValue());
105
         assertNotNull(test.getDbos()[0]);
123
         assertNotNull(test.getDbos()[0]);
106
         assertEquals("24 24", test.getDbos()[0].getaString());
124
         assertEquals("24 24", test.getDbos()[0].getaString());
107
         assertEquals(2424, test.getDbos()[0].getaInt());
125
         assertEquals(2424, test.getDbos()[0].getaInt());
126
+
127
+        assertNotNull(test.getHashMap());
128
+        assertEquals(2, test.getHashMap().size());
129
+        assertTrue(test.getHashMap().containsKey("forty-two"));
130
+        assertEquals(42, (int)test.getHashMap().get("forty-two"));
131
+        assertTrue(test.getHashMap().containsKey("twenty-four"));
132
+        assertEquals(24, (int)test.getHashMap().get("twenty-four"));
133
+
134
+        assertNotNull(test.getHashMap2());
135
+        assertEquals(1, test.getHashMap2().size());
136
+        assertTrue(test.getHashMap2().containsKey("an object"));
137
+        assertNotNull(test.getHashMap2().get("an object"));
138
+        assertEquals("42 24", test.getHashMap2().get("an object").getaString());
139
+        assertEquals(4224, test.getHashMap2().get("an object").getaInt());
140
+    }
141
+
142
+    @Test
143
+    public void serializeTest()
144
+    {
145
+        Test2Dbo dbo = new Test2Dbo();
146
+        dbo.setaInt(2442);
147
+        dbo.setaString("24 42");
148
+
108
     }
149
     }
109
 }
150
 }

Loading…
Cancel
Save