Skip to content

Commit ee93bfc

Browse files
committed
Server:优化远程函数,传入request作为第一个参数可最大化灵活实现不同类型的取值,且不用再传参数类型
1 parent df9692c commit ee93bfc

File tree

4 files changed

+219
-200
lines changed

4 files changed

+219
-200
lines changed

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/server/DemoFunction.java

Lines changed: 118 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@
1414

1515
package apijson.demo.server;
1616

17-
import java.util.Collection;
18-
import java.util.HashMap;
19-
import java.util.Map;
20-
2117
import com.alibaba.fastjson.JSONArray;
2218
import com.alibaba.fastjson.JSONObject;
2319

2420
import apijson.demo.server.model.BaseModel;
2521
import zuo.biao.apijson.Log;
2622
import zuo.biao.apijson.server.Function;
23+
import zuo.biao.apijson.server.NotNull;
2724

2825

2926
/**可远程调用的函数类
@@ -35,228 +32,234 @@ public class DemoFunction extends Function implements FunctionList {
3532

3633
public static void test() throws Exception {
3734
int i0 = 1, i1 = -2;
38-
Map<String, Object> jsonMap = new HashMap<String, Object>();
39-
jsonMap.put("id", 10);
40-
jsonMap.put("i0", i0);
41-
jsonMap.put("i1", i1);
35+
JSONObject request = new JSONObject();
36+
request.put("id", 10);
37+
request.put("i0", i0);
38+
request.put("i1", i1);
4239
JSONArray arr = new JSONArray();
4340
arr.add(new JSONObject());
44-
jsonMap.put("arr", arr);
45-
46-
JSONArray collection = new JSONArray();
47-
collection.add(1);//new JSONObject());
48-
collection.add(2);//new JSONObject());
49-
collection.add(4);//new JSONObject());
50-
collection.add(10);//new JSONObject());
51-
jsonMap.put("collection", collection);
52-
53-
jsonMap.put("position", 1);
54-
jsonMap.put("@position", 0);
55-
56-
jsonMap.put("key", "key");
57-
Map<String, Boolean> map = new HashMap<String, Boolean>();
58-
map.put("key", true);
59-
jsonMap.put("map", map);
60-
61-
62-
Log.i(TAG, "plus(1, -2) = " + invoke(jsonMap, "plus(long:i0,long:i1)"));
63-
Log.i(TAG, "count([1,2,4,10]) = " + invoke(jsonMap, "count(Collection:collection)"));
64-
Log.i(TAG, "isContain([1,2,4,10], 10) = " + invoke(jsonMap, "isContain(Collection:collection,Object:id)"));
65-
Log.i(TAG, "get({key:true}, key) = " + invoke(jsonMap, "get(Map:map,key)"));
66-
Log.i(TAG, "get([1,2,4,10], 0) = " + invoke(jsonMap, "get(Collection:collection,int:@position)"));
67-
Log.i(TAG, "Integer:get({key:true}, key) = " + invoke(jsonMap, "Integer:get(Map:map,key)"));
41+
request.put("arr", arr);
42+
43+
JSONArray array = new JSONArray();
44+
array.add(1);//new JSONObject());
45+
array.add(2);//new JSONObject());
46+
array.add(4);//new JSONObject());
47+
array.add(10);//new JSONObject());
48+
request.put("array", array);
49+
50+
request.put("position", 1);
51+
request.put("@position", 0);
52+
53+
request.put("key", "key");
54+
JSONObject object = new JSONObject();
55+
object.put("key", true);
56+
request.put("object", object);
57+
58+
59+
Log.i(TAG, "plus(1,-2) = " + invoke(request, "plus(i0,i1)"));
60+
Log.i(TAG, "count([1,2,4,10]) = " + invoke(request, "countArray(array)"));
61+
Log.i(TAG, "isContain([1,2,4,10], 10) = " + invoke(request, "isContain(array,id)"));
62+
Log.i(TAG, "getFromArray([1,2,4,10], 0) = " + invoke(request, "getFromArray(array,@position)"));
63+
Log.i(TAG, "getFromObject({key:true}, key) = " + invoke(request, "getFromObject(object,key)"));
64+
65+
try {
66+
Log.i(TAG, "Object:getFromObject({key:true}, key) = " + invoke(request, "Object:getFromObject(object,key)")); //NoSuchMethodException
67+
} catch (Exception e) {
68+
e.printStackTrace();
69+
}
70+
try {
71+
Log.i(TAG, "getFromObject({key:true}, Object:key) = " + invoke(request, "getFromObject(object,Object:key)")); //NoSuchMethodException
72+
} catch (Exception e) {
73+
e.printStackTrace();
74+
}
6875
}
6976

7077

7178

72-
public static final DemoFunction instance = new DemoFunction();
79+
public static final DemoFunction instance;
80+
static {
81+
instance = new DemoFunction();
82+
}
7383
/**反射调用
74-
* @param jsonMap
75-
* @param function 例如get(Map:map,key),参数只允许引用,不能直接传值
84+
* @param request
85+
* @param function 例如get(object,key),参数只允许引用,不能直接传值
7686
* @return
7787
*/
78-
public static Object invoke(Map<String, Object> jsonMap, String function) throws Exception {
88+
public static Object invoke(JSONObject request, String function) throws Exception {
7989
//TODO 不允许调用invoke,避免死循环
8090
// if (function.startsWith("invoke(")) {
8191
//
8292
// }
83-
return invoke(instance, jsonMap, function);
93+
return invoke(instance, request, function);
8494
}
8595

8696

8797

8898

89-
public String search(String key) {
99+
public String search(@NotNull JSONObject request, String key) {
90100
return null;
91101
}
92102

93-
public long plus(long i0, long i1) {
94-
return i0 + i1;
103+
public double plus(@NotNull JSONObject request, String i0, String i1) {
104+
return request.getDoubleValue(i0) + request.getDoubleValue(i1);
105+
}
106+
public double minus(@NotNull JSONObject request, String i0, String i1) {
107+
return request.getDoubleValue(i0) - request.getDoubleValue(i1);
108+
}
109+
public double multiply(@NotNull JSONObject request, String i0, String i1) {
110+
return request.getDoubleValue(i0) * request.getDoubleValue(i1);
111+
}
112+
public double divide(@NotNull JSONObject request, String i0, String i1) {
113+
return request.getDoubleValue(i0) / request.getDoubleValue(i1);
95114
}
96-
97115

98116
//判断是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
99-
/**判断collection是否为空
100-
* @param collection
117+
/**判断array是否为空
118+
* @param request
119+
* @param array
101120
* @return
102121
*/
103122
@Override
104-
public <T> boolean isEmpty(Collection<T> collection) {
105-
return BaseModel.isEmpty(collection);
123+
public boolean isArrayEmpty(@NotNull JSONObject request, String array) {
124+
return BaseModel.isEmpty(request.getJSONArray(array));
106125
}
107-
/**判断map是否为空
108-
* @param <K>
109-
* @param <V>
110-
* @param map
126+
/**判断object是否为空
127+
* @param request
128+
* @param object
111129
* @return
112130
*/
113131
@Override
114-
public <K, V> boolean isEmpty(Map<K, V> map) {
115-
return BaseModel.isEmpty(map);
132+
public boolean isObjectEmpty(@NotNull JSONObject request, String object) {
133+
return BaseModel.isEmpty(request.getJSONObject(object));
116134
}
117135
//判断是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
118136

119137
//判断是否为包含 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
120-
/**判断collection是否包含object
121-
* @param collection
122-
* @param object
138+
/**判断array是否包含value
139+
* @param request
140+
* @param array
141+
* @param value
123142
* @return
124143
*/
125144
@Override
126-
public <T> boolean isContain(Collection<T> collection, T object) {
127-
return BaseModel.isContain(collection, object);
145+
public boolean isContain(@NotNull JSONObject request, String array, String value) {
146+
return BaseModel.isContain(request.getJSONArray(array), request.get(value));
128147
}
129-
/**判断map是否包含key
130-
* @param <K>
131-
* @param <V>
132-
* @param map
148+
/**判断object是否包含key
149+
* @param request
150+
* @param object
133151
* @param key
134152
* @return
135153
*/
136154
@Override
137-
public <K, V> boolean isContainKey(Map<K, V> map, K key) {
138-
return BaseModel.isContainKey(map, key);
155+
public boolean isContainKey(@NotNull JSONObject request, String object, String key) {
156+
return BaseModel.isContainKey(request.getJSONObject(object), request.getString(key));
139157
}
140-
/**判断map是否包含value
141-
* @param <K>
142-
* @param <V>
143-
* @param map
158+
/**判断object是否包含value
159+
* @param request
160+
* @param object
144161
* @param value
145162
* @return
146163
*/
147164
@Override
148-
public <K, V> boolean isContainValue(Map<K, V> map, V value) {
149-
return BaseModel.isContainValue(map, value);
165+
public boolean isContainValue(@NotNull JSONObject request, String object, String value) {
166+
return BaseModel.isContainValue(request.getJSONObject(object), request.get(value));
150167
}
151168
//判断是否为包含 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
152169

153170

154171
//获取集合长度 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
155172
/**获取数量
156-
* @param <T>
173+
* @param request
157174
* @param array
158175
* @return
159176
*/
160177
@Override
161-
public <T> int count(T[] array) {
162-
return BaseModel.count(array);
163-
}
164-
/**获取数量
165-
* @param <T>
166-
* @param collection List, Vector, Set等都是Collection的子类
167-
* @return
168-
*/
169-
@Override
170-
public <T> int count(Collection<T> collection) {
171-
return BaseModel.count(collection);
178+
public int countArray(@NotNull JSONObject request, String array) {
179+
return BaseModel.count(request.getJSONArray(array));
172180
}
173181
/**获取数量
174-
* @param <K>
175-
* @param <V>
176-
* @param map
182+
* @param request
183+
* @param object
177184
* @return
178185
*/
179186
@Override
180-
public <K, V> int count(Map<K, V> map) {
181-
return BaseModel.count(map);
187+
public int countObject(@NotNull JSONObject request, String object) {
188+
return BaseModel.count(request.getJSONObject(object));
182189
}
183190
//获取集合长度 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
184191

185192

186-
//获取集合长度 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
193+
//根据键获取值 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
187194
/**获取
188-
* @param <T>
195+
** @param request
189196
* @param array
197+
* @param position
190198
* @return
191199
*/
192200
@Override
193-
public <T> T get(T[] array, int position) {
194-
return BaseModel.get(array, position);
195-
}
196-
/**获取
197-
* @param <T>
198-
* @param collection List, Vector, Set等都是Collection的子类
199-
* @return
200-
*/
201-
@Override
202-
public <T> T get(Collection<T> collection, int position) {
203-
return BaseModel.get(collection, position);
201+
public Object getFromArray(@NotNull JSONObject request, String array, String position) {
202+
return BaseModel.get(request.getJSONArray(array), request.getIntValue(position));
204203
}
205204
/**获取
206-
* @param <K>
207-
* @param <V>
208-
* @param map null ? null
209-
* @param key null ? null : map.get(key);
205+
* @param request
206+
* @param object
207+
* @param key
210208
* @return
211209
*/
212210
@Override
213-
public <K, V> V get(Map<K, V> map, K key) {
214-
return BaseModel.get(map, key);
211+
public Object getFromObject(@NotNull JSONObject request, String object, String key) {
212+
return BaseModel.get(request.getJSONObject(object), request.getString(key));
215213
}
216-
//获取集合长度 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
214+
//根据键获取值 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
217215

218216

219217

220218
//获取非基本类型对应基本类型的非空值 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
221219
/**获取非空值
220+
* @param request
222221
* @param value
223222
* @return
224223
*/
225224
@Override
226-
public boolean value(Boolean value) {
227-
return BaseModel.value(value);
225+
public boolean booleanValue(@NotNull JSONObject request, String value) {
226+
return request.getBooleanValue(value);
228227
}
229228
/**获取非空值
229+
* @param request
230230
* @param value
231231
* @return
232232
*/
233233
@Override
234-
public int value(Integer value) {
235-
return BaseModel.value(value);
234+
public int intValue(@NotNull JSONObject request, String value) {
235+
return request.getIntValue(value);
236236
}
237237
/**获取非空值
238+
* @param request
238239
* @param value
239240
* @return
240241
*/
241242
@Override
242-
public long value(Long value) {
243-
return BaseModel.value(value);
243+
public long longValue(@NotNull JSONObject request, String value) {
244+
return request.getLongValue(value);
244245
}
245246
/**获取非空值
247+
* @param request
246248
* @param value
247249
* @return
248250
*/
249251
@Override
250-
public float value(Float value) {
251-
return BaseModel.value(value);
252+
public float floatValue(@NotNull JSONObject request, String value) {
253+
return request.getFloatValue(value);
252254
}
253255
/**获取非空值
256+
* @param request
254257
* @param value
255258
* @return
256259
*/
257260
@Override
258-
public double value(Double value) {
259-
return BaseModel.value(value);
261+
public double doubleValue(@NotNull JSONObject request, String value) {
262+
return request.getDoubleValue(value);
260263
}
261264
//获取非基本类型对应基本类型的非空值 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
262265

0 commit comments

Comments
 (0)