Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用entrySet迭代器替代keySet迭代器提高效率 #48 #224

Merged
merged 3 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -530,10 +532,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 @@ -631,10 +636,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 @@ -1375,9 +1375,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 @@ -1392,13 +1394,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 @@ -1329,10 +1329,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 @@ -1341,16 +1343,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 @@ -2272,11 +2275,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 @@ -2289,7 +2294,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 @@ -2302,7 +2308,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