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,7 +11,9 @@ import java.lang.reflect.ParameterizedType;
11 11
 import java.lang.reflect.Type;
12 12
 import java.util.AbstractMap;
13 13
 import java.util.HashMap;
14
+import java.util.Iterator;
14 15
 import java.util.List;
16
+import java.util.Objects;
15 17
 import java.util.Vector;
16 18
 
17 19
 /**
@@ -25,7 +27,7 @@ public abstract class LuDbo {
25 27
         fromJson(json);
26 28
     }
27 29
 
28
-    public static String getFieldFromJson(String jsonName)
30
+    public String getFieldFromJson(String jsonName)
29 31
     {
30 32
         if (!jsonName.startsWith("_")) {
31 33
             return String.format("_%s", jsonName);
@@ -33,7 +35,7 @@ public abstract class LuDbo {
33 35
         return jsonName;
34 36
     }
35 37
 
36
-    public static String getJsonFromField(String fieldName)
38
+    public String getJsonFromField(String fieldName)
37 39
     {
38 40
         if (fieldName.startsWith("_")) {
39 41
             return fieldName.substring(1);
@@ -41,7 +43,7 @@ public abstract class LuDbo {
41 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 47
         if (json.isNull(key)) {
46 48
             return null;
47 49
         }
@@ -74,14 +76,14 @@ public abstract class LuDbo {
74 76
             return json.getString(key);
75 77
         }
76 78
         if (LuDbo.class.isAssignableFrom(clazz)) {
77
-            LuDbo dbo = (LuDbo) clazz.newInstance();
79
+            LuDbo dbo = (LuDbo) getInstance(clazz);
78 80
             dbo.fromJson(json.getJSONObject(key));
79 81
             return dbo;
80 82
         }
81 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 87
         if (json.isNull(key)) {
86 88
             return null;
87 89
         }
@@ -114,13 +116,23 @@ public abstract class LuDbo {
114 116
             return json.getString(key);
115 117
         }
116 118
         if (LuDbo.class.isAssignableFrom(clazz)) {
117
-            LuDbo dbo = (LuDbo) clazz.newInstance();
119
+            LuDbo dbo = (LuDbo) getInstance(clazz);
118 120
             dbo.fromJson(json.getJSONObject(key));
119 121
             return dbo;
120 122
         }
121 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 136
     public void fromJson(JSONObject json) throws Exception
125 137
     {
126 138
         for (Field field : getClass().getDeclaredFields()) {
@@ -131,7 +143,7 @@ public abstract class LuDbo {
131 143
                 if (List.class.isAssignableFrom(clazz)) {
132 144
                     ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
133 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 147
                     JSONArray jsonArray = json.getJSONArray(key);
136 148
                     for (int i = 0; i < jsonArray.length(); ++i) {
137 149
                         list.add(getObjectFromJson(type, jsonArray, i));
@@ -147,13 +159,21 @@ public abstract class LuDbo {
147 159
                     }
148 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 177
                 else {
158 178
                     value = getObjectFromJson(clazz, json, key);
159 179
                 }
@@ -168,6 +188,11 @@ public abstract class LuDbo {
168 188
     {
169 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 196
         return map;
172 197
     }
173 198
 

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

@@ -1,6 +1,7 @@
1 1
 package com.luticate.utils;
2 2
 
3 3
 import com.luticate.utils.dbo.LuDbo;
4
+import com.luticate.utils.dbo.LuVoidDbo;
4 5
 
5 6
 import org.junit.Test;
6 7
 
@@ -15,18 +16,20 @@ public class NameDboTest {
15 16
     @Test
16 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 26
     @Test
25 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,4 +27,23 @@ public class Test2Dbo extends LuDbo {
27 27
     public void setaInt(int aInt) {
28 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,6 +2,8 @@ package com.luticate.utils;
2 2
 
3 3
 import com.luticate.utils.dbo.LuDbo;
4 4
 
5
+import java.io.Serializable;
6
+import java.util.AbstractMap;
5 7
 import java.util.HashMap;
6 8
 import java.util.List;
7 9
 
@@ -9,7 +11,7 @@ import java.util.List;
9 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 15
     protected byte _byte;
14 16
     protected char _char;
15 17
     protected short _short;
@@ -31,17 +33,17 @@ public class TestDbo extends LuDbo {
31 33
     protected String _string;
32 34
 
33 35
     protected Test2Dbo _test2Dbo;
34
-
35 36
     protected Test2Dbo _test2Dbo2;
36 37
 
37 38
     protected List<Integer> _integers;
38
-
39 39
     protected int[] _ints;
40 40
 
41 41
     protected List<Test2Dbo> _test2Dbos;
42
-
43 42
     protected Test2Dbo[] _dbos;
44 43
 
44
+    protected HashMap<String, Integer> _hashMap;
45
+    protected AbstractMap<String, Test2Dbo> _hashMap2;
46
+
45 47
     public byte getByte() {
46 48
         return _byte;
47 49
     }
@@ -225,4 +227,20 @@ public class TestDbo extends LuDbo {
225 227
     public void setDbos(Test2Dbo[] dbos) {
226 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,26 +16,30 @@ public class TestDboTest {
16 16
         TestDbo test = new TestDbo();
17 17
         test.fromString("{" +
18 18
                 "\"byte\": 42," +
19
-                "\"byteObject\": 24," +
20 19
                 "\"char\": \"h\"," +
21
-                "\"charObject\": \"w\"," +
22 20
                 "\"short\": 4200," +
23
-                "\"shortObject\": 2400," +
24 21
                 "\"int\": 420042," +
25
-                "\"intObject\": 240024," +
26 22
                 "\"long\": 42004200," +
27
-                "\"longObject\": 24002400," +
28 23
                 "\"float\": 42.24," +
29
-                "\"floatObject\": 24.42," +
30 24
                 "\"double\": 4200.0024," +
31
-                "\"doubleObject\": 2400.0042," +
32 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 34
                 "\"booleanObject\": false," +
35
+
34 36
                 "\"string\": \"Hello World!\"," +
37
+
35 38
                 "\"test2Dbo\": {" +
36 39
                 "\"aString\": \"a string\"," +
37 40
                 "\"aInt\": 4242" +
38 41
                 "}," +
42
+
39 43
                 "\"integers\": [" +
40 44
                 "0," +
41 45
                 "42," +
@@ -46,6 +50,7 @@ public class TestDboTest {
46 50
                 "42," +
47 51
                 "0" +
48 52
                 "]," +
53
+
49 54
                 "test2Dbos: [" +
50 55
                 "{" +
51 56
                 "\"aString\": \"42 42\"," +
@@ -57,7 +62,20 @@ public class TestDboTest {
57 62
                 "\"aString\": \"24 24\"," +
58 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 80
         assertEquals(42, test.getByte());
63 81
         assertEquals(24, test.getByteObject().byteValue());
@@ -105,5 +123,28 @@ public class TestDboTest {
105 123
         assertNotNull(test.getDbos()[0]);
106 124
         assertEquals("24 24", test.getDbos()[0].getaString());
107 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