Skip to content

Commit c732f26

Browse files
authored
Merge pull request #224 from Rkyzzy/master
使用entrySet迭代器替代keySet迭代器提高效率 #48
2 parents c40b48c + 0fcbdfa commit c732f26

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

APIJSONORM/src/main/java/apijson/StringUtil.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,13 @@ public static String getString(Object[] array, boolean ignoreEmptyItem) {
111111
public static String getString(Object[] array, String split) {
112112
return getString(array, split, false);
113113
}
114+
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182
114115
/**获取string,为null则返回""
115-
* @param array
116-
* @param split
117-
* @param ignoreEmptyItem
118-
* @return
116+
* @param array -the str array given
117+
* @param split -the token used to split
118+
* @param ignoreEmptyItem -whether to ignore empty item or not
119+
* @return {@link #getString(Object[], String, boolean)}
120+
* <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p>
119121
*/
120122
public static String getString(Object[] array, String split, boolean ignoreEmptyItem) {
121123
StringBuilder s = new StringBuilder("");
@@ -537,10 +539,13 @@ public static String getNumber(CharSequence cs) {
537539
public static String getNumber(String s) {
538540
return getNumber(s, false);
539541
}
542+
543+
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182
540544
/**去掉string内所有非数字类型字符
541-
* @param s
545+
* @param s -string passed in
542546
* @param onlyStart 中间有非数字时只获取前面的数字
543-
* @return
547+
* @return limit String
548+
* <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p>
544549
*/
545550
public static String getNumber(String s, boolean onlyStart) {
546551
if (isNotEmpty(s, true) == false) {
@@ -638,10 +643,12 @@ public static String getCorrectEmail(String email) {
638643
public static String getPrice(String price) {
639644
return getPrice(price, PRICE_FORMAT_DEFAULT);
640645
}
646+
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182
641647
/**获取价格,保留两位小数
642-
* @param price
648+
* @param price -price passed in
643649
* @param formatType 添加单位(元)
644-
* @return
650+
* @return limit String
651+
* <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p>
645652
*/
646653
public static String getPrice(String price, int formatType) {
647654
if (isNotEmpty(price, true) == false) {

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -1428,9 +1428,11 @@ public synchronized void putQueryResult(String path, Object result) {
14281428
queryResultMap.put(path, result);
14291429
// }
14301430
}
1431+
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/48
14311432
/**根据路径获取值
1432-
* @param valuePath
1433+
* @param valuePath -the path need to get value
14331434
* @return parent == null ? valuePath : parent.get(keys[keys.length - 1])
1435+
* <p>use entrySet+getValue() to replace keySet+get() to enhance efficiency</p>
14341436
*/
14351437
@Override
14361438
public Object getValueByPath(String valuePath) {
@@ -1445,13 +1447,13 @@ public Object getValueByPath(String valuePath) {
14451447
}
14461448

14471449
//取出key被valuePath包含的result,再从里面获取key对应的value
1448-
Set<String> set = queryResultMap.keySet();
14491450
JSONObject parent = null;
14501451
String[] keys = null;
1451-
for (String path : set) {
1452+
for (Map.Entry<String,Object> entry : queryResultMap.entrySet()){
1453+
String path = entry.getKey();
14521454
if (valuePath.startsWith(path + "/")) {
14531455
try {
1454-
parent = (JSONObject) queryResultMap.get(path);
1456+
parent = (JSONObject) entry.getValue();
14551457
} catch (Exception e) {
14561458
Log.e(TAG, "getValueByPath try { parent = (JSONObject) queryResultMap.get(path); } catch { "
14571459
+ "\n parent not instanceof JSONObject!");

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

+23-17
Original file line numberDiff line numberDiff line change
@@ -1527,10 +1527,12 @@ public AbstractSQLConfig setCombine(Map<String, List<String>> combine) {
15271527
public Object getWhere(String key) {
15281528
return getWhere(key, false);
15291529
}
1530+
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/48
15301531
/**
1531-
* @param key
1532-
* @param exactMatch
1532+
* @param key - the key passed in
1533+
* @param exactMatch - whether it is exact match
15331534
* @return
1535+
* <p>use entrySet+getValue() to replace keySet+get() to enhance efficiency</p>
15341536
*/
15351537
@JSONField(serialize = false)
15361538
@Override
@@ -1539,16 +1541,17 @@ public Object getWhere(String key, boolean exactMatch) {
15391541
return where == null ? null : where.get(key);
15401542
}
15411543

1542-
Set<String> set = key == null || where == null ? null : where.keySet();
1543-
if (set != null) {
1544-
synchronized (where) {
1545-
if (where != null) {
1546-
int index;
1547-
for (String k : set) {
1548-
index = k.indexOf(key);
1549-
if (index >= 0 && StringUtil.isName(k.substring(index)) == false) {
1550-
return where.get(k);
1551-
}
1544+
if (key == null || where == null){
1545+
return null;
1546+
}
1547+
synchronized (where) {
1548+
if (where != null) {
1549+
int index;
1550+
for (Map.Entry<String,Object> entry : where.entrySet()) {
1551+
String k = entry.getKey();
1552+
index = k.indexOf(key);
1553+
if (index >= 0 && StringUtil.isName(k.substring(index)) == false) {
1554+
return entry.getValue();
15521555
}
15531556
}
15541557
}
@@ -2480,11 +2483,13 @@ public static JSONArray newJSONArray(Object obj) {
24802483
public String getSetString() throws Exception {
24812484
return getSetString(getMethod(), getContent(), ! isTest());
24822485
}
2486+
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/48
24832487
/**获取SET
2484-
* @param method
2485-
* @param content
2488+
* @param method -the method used
2489+
* @param content -the content map
24862490
* @return
2487-
* @throws Exception
2491+
* @throws Exception
2492+
* <p>use entrySet+getValue() to replace keySet+get() to enhance efficiency</p>
24882493
*/
24892494
@JSONField(serialize = false)
24902495
public String getSetString(RequestMethod method, Map<String, Object> content, boolean verifyName) throws Exception {
@@ -2497,7 +2502,8 @@ public String getSetString(RequestMethod method, Map<String, Object> content, bo
24972502
Object value;
24982503

24992504
String idKey = getIdKey();
2500-
for (String key : set) {
2505+
for (Map.Entry<String,Object> entry : content.entrySet()) {
2506+
String key = entry.getKey();
25012507
//避免筛选到全部 value = key == null ? null : content.get(key);
25022508
if (key == null || idKey.equals(key)) {
25032509
continue;
@@ -2510,7 +2516,7 @@ public String getSetString(RequestMethod method, Map<String, Object> content, bo
25102516
} else {
25112517
keyType = 0; //注意重置类型,不然不该加减的字段会跟着加减
25122518
}
2513-
value = content.get(key);
2519+
value = entry.getValue();
25142520
key = getRealKey(method, key, false, true, verifyName);
25152521

25162522
setString += (isFirst ? "" : ", ") + (getKey(key) + " = " + (keyType == 1 ? getAddString(key, value) : (keyType == 2

0 commit comments

Comments
 (0)