Skip to content

Commit

Permalink
Merge pull request #224 from Rkyzzy/master
Browse files Browse the repository at this point in the history
使用entrySet迭代器替代keySet迭代器提高效率 #48
  • Loading branch information
TommyLemon authored Apr 25, 2021
2 parents c40b48c + 0fcbdfa commit c732f26
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
23 changes: 15 additions & 8 deletions APIJSONORM/src/main/java/apijson/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,13 @@ public static String getString(Object[] array, boolean ignoreEmptyItem) {
public static String getString(Object[] array, String split) {
return getString(array, split, false);
}
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182
/**获取string,为null则返回""
* @param array
* @param split
* @param ignoreEmptyItem
* @return
* @param array -the str array given
* @param split -the token used to split
* @param ignoreEmptyItem -whether to ignore empty item or not
* @return {@link #getString(Object[], String, boolean)}
* <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p>
*/
public static String getString(Object[] array, String split, boolean ignoreEmptyItem) {
StringBuilder s = new StringBuilder("");
Expand Down Expand Up @@ -537,10 +539,13 @@ public static String getNumber(CharSequence cs) {
public static String getNumber(String s) {
return getNumber(s, false);
}

//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182
/**去掉string内所有非数字类型字符
* @param s
* @param s -string passed in
* @param onlyStart 中间有非数字时只获取前面的数字
* @return
* @return limit String
* <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p>
*/
public static String getNumber(String s, boolean onlyStart) {
if (isNotEmpty(s, true) == false) {
Expand Down Expand Up @@ -638,10 +643,12 @@ public static String getCorrectEmail(String email) {
public static String getPrice(String price) {
return getPrice(price, PRICE_FORMAT_DEFAULT);
}
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182
/**获取价格,保留两位小数
* @param price
* @param price -price passed in
* @param formatType 添加单位(元)
* @return
* @return limit String
* <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p>
*/
public static String getPrice(String price, int formatType) {
if (isNotEmpty(price, true) == false) {
Expand Down
10 changes: 6 additions & 4 deletions APIJSONORM/src/main/java/apijson/orm/AbstractParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1428,9 +1428,11 @@ public synchronized void putQueryResult(String path, Object result) {
queryResultMap.put(path, result);
// }
}
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/48
/**根据路径获取值
* @param valuePath
* @param valuePath -the path need to get value
* @return parent == null ? valuePath : parent.get(keys[keys.length - 1])
* <p>use entrySet+getValue() to replace keySet+get() to enhance efficiency</p>
*/
@Override
public Object getValueByPath(String valuePath) {
Expand All @@ -1445,13 +1447,13 @@ public Object getValueByPath(String valuePath) {
}

//取出key被valuePath包含的result,再从里面获取key对应的value
Set<String> set = queryResultMap.keySet();
JSONObject parent = null;
String[] keys = null;
for (String path : set) {
for (Map.Entry<String,Object> entry : queryResultMap.entrySet()){
String path = entry.getKey();
if (valuePath.startsWith(path + "/")) {
try {
parent = (JSONObject) queryResultMap.get(path);
parent = (JSONObject) entry.getValue();
} catch (Exception e) {
Log.e(TAG, "getValueByPath try { parent = (JSONObject) queryResultMap.get(path); } catch { "
+ "\n parent not instanceof JSONObject!");
Expand Down
40 changes: 23 additions & 17 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -1527,10 +1527,12 @@ public AbstractSQLConfig setCombine(Map<String, List<String>> combine) {
public Object getWhere(String key) {
return getWhere(key, false);
}
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/48
/**
* @param key
* @param exactMatch
* @param key - the key passed in
* @param exactMatch - whether it is exact match
* @return
* <p>use entrySet+getValue() to replace keySet+get() to enhance efficiency</p>
*/
@JSONField(serialize = false)
@Override
Expand All @@ -1539,16 +1541,17 @@ public Object getWhere(String key, boolean exactMatch) {
return where == null ? null : where.get(key);
}

Set<String> set = key == null || where == null ? null : where.keySet();
if (set != null) {
synchronized (where) {
if (where != null) {
int index;
for (String k : set) {
index = k.indexOf(key);
if (index >= 0 && StringUtil.isName(k.substring(index)) == false) {
return where.get(k);
}
if (key == null || where == null){
return null;
}
synchronized (where) {
if (where != null) {
int index;
for (Map.Entry<String,Object> entry : where.entrySet()) {
String k = entry.getKey();
index = k.indexOf(key);
if (index >= 0 && StringUtil.isName(k.substring(index)) == false) {
return entry.getValue();
}
}
}
Expand Down Expand Up @@ -2480,11 +2483,13 @@ public static JSONArray newJSONArray(Object obj) {
public String getSetString() throws Exception {
return getSetString(getMethod(), getContent(), ! isTest());
}
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/48
/**获取SET
* @param method
* @param content
* @param method -the method used
* @param content -the content map
* @return
* @throws Exception
* @throws Exception
* <p>use entrySet+getValue() to replace keySet+get() to enhance efficiency</p>
*/
@JSONField(serialize = false)
public String getSetString(RequestMethod method, Map<String, Object> content, boolean verifyName) throws Exception {
Expand All @@ -2497,7 +2502,8 @@ public String getSetString(RequestMethod method, Map<String, Object> content, bo
Object value;

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

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

0 comments on commit c732f26

Please sign in to comment.