Skip to content

Commit

Permalink
新增驼峰与蛇形命名互转方法以及 JSONResponse.IS_FORMAT_UNDERLINE 等配置
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyLemon committed Sep 9, 2023
1 parent 72524b2 commit 2b0d612
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 28 deletions.
67 changes: 67 additions & 0 deletions APIJSONORM/src/main/java/apijson/JSONRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.List;
import java.util.Map;

import static apijson.StringUtil.PATTERN_ALPHA_BIG;

/**wrapper for request
* @author Lemon
* @see #puts
Expand Down Expand Up @@ -196,4 +198,69 @@ public JSONRequest puts(String key, Object value) {
return this;
}


/**ABCdEfg => upper ? A-B-CD-EFG : a-b-cd-efg
* @param key
* @return
*/
public static String recoverHyphen(@NotNull String key, Boolean upper) {
return recoverDivider(key, "-", upper);
}

/**ABCdEfg => upper ? A_B_CD_EFG : a_b_cd_efg
* @param key
* @return
*/
public static String recoverUnderline(@NotNull String key, Boolean upper) {
return recoverDivider(key, "_", upper);
}

/**ABCdEfg => upper ? A$B$CD$EFG : a$b$cd$efg
* @param key
* @return
*/
public static String recoverDollar(@NotNull String key, Boolean upper) {
return recoverDivider(key, "$", upper);
}

/**ABCdEfg => upper ? A.B.CD.EFG : a.b.cd.efg
* @param key
* @return
*/
public static String recoverDot(@NotNull String key, Boolean upper) {
return recoverDivider(key, ".", upper);
}

/**ABCdEfg => upper ? A_B_CD_EFG : a/b/cd/efg
* @param key
* @return
*/
public static String recoverDivider(@NotNull String key, Boolean upper) {
return recoverDivider(key, "/", upper);
}

/**驼峰格式转为带分隔符的全大写或全小写格式
* @param key
* @param divider
* @param upper
* @return
*/
public static String recoverDivider(@NotNull String key, @NotNull String divider, Boolean upper) {
StringBuilder name = new StringBuilder();
char[] cs = key.toCharArray();
int len = key.length();
for (int i = 0; i < len; i++) {
String s = key.substring(i, i + 1);
if (i > 0 && PATTERN_ALPHA_BIG.matcher(s).matches()) {
name.append(divider);
}
if (upper != null) {
s = upper ? s.toUpperCase() : s.toLowerCase();
}
name.append(s);
}
return name.toString();
}


}
119 changes: 91 additions & 28 deletions APIJSONORM/src/main/java/apijson/JSONResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

package apijson;

import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Set;

/**parser for response
* @author Lemon
* @see #getObject
Expand All @@ -23,6 +22,17 @@
public class JSONResponse extends apijson.JSONObject {
private static final long serialVersionUID = 1L;

// 节约性能和减少 bug,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
/**格式化带 - 中横线的单词
*/
public static boolean IS_FORMAT_HYPHEN = false;
/**格式化带 _ 下划线的单词
*/
public static boolean IS_FORMAT_UNDERLINE = false;
/**格式化带 $ 美元符的单词
*/
public static boolean IS_FORMAT_DOLLAR = false;

private static final String TAG = "JSONResponse";

public JSONResponse() {
Expand Down Expand Up @@ -206,10 +216,6 @@ public JSONResponse getJSONResponse(String key) {
//状态信息,非GET请求获得的信息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>






/**
* key = clazz.getSimpleName()
* @param clazz
Expand Down Expand Up @@ -408,18 +414,18 @@ public static String getTableName(String fullName) {

/**获取变量名
* @param fullName
* @return {@link #formatKey(String, boolean, boolean, boolean, boolean)} formatColon = true, formatAt = true, formatHyphen = true, firstCase = true
* @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, boolean)} formatColon = true, formatAt = true, formatHyphen = true, firstCase = true
*/
public static String getVariableName(String fullName) {
if (isArrayKey(fullName)) {
fullName = StringUtil.addSuffix(fullName.substring(0, fullName.length() - 2), "list");
}
return formatKey(fullName, true, true, true, true);
return formatKey(fullName, true, true, true, true, false, true);
}

/**格式化数组的名称 key[] => keyList; key:alias[] => aliasList; Table-column[] => tableColumnList
* @param key empty ? "list" : key + "List" 且首字母小写
* @return {@link #formatKey(String, boolean, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = true, firstCase = true
* @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = true, firstCase = true
*/
public static String formatArrayKey(String key) {
if (isArrayKey(key)) {
Expand All @@ -430,28 +436,29 @@ public static String formatArrayKey(String key) {
return key.substring(index + 1); //不处理自定义的
}

return formatKey(key, false, true, true, true); //节约性能,除了数组对象 Table-column:alias[] ,一般都符合变量命名规范
return formatKey(key, false, true, true, IS_FORMAT_UNDERLINE, IS_FORMAT_DOLLAR, false); //节约性能,除了数组对象 Table-column:alias[] ,一般都符合变量命名规范
}

/**格式化对象的名称 name => name; name:alias => alias
* @param key name 或 name:alias
* @return {@link #formatKey(String, boolean, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = true
* @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = true
*/
public static String formatObjectKey(String key) {
int index = key == null ? -1 : key.indexOf(":");
if (index >= 0) {
return key.substring(index + 1); //不处理自定义的
return key.substring(index + 1); // 不处理自定义的
}

return formatKey(key, false, true, false, true); //节约性能,除了表对象 Table:alias ,一般都符合变量命名规范
return formatKey(key, false, true, IS_FORMAT_HYPHEN, IS_FORMAT_UNDERLINE, IS_FORMAT_DOLLAR, false); //节约性能,除了表对象 Table:alias ,一般都符合变量命名规范
}

/**格式化普通值的名称 name => name; name:alias => alias
* @param fullName name 或 name:alias
* @return {@link #formatKey(String, boolean, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = false
* @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = false
*/
public static String formatOtherKey(String fullName) {
return formatKey(fullName, false, true, false, false); //节约性能,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
return formatKey(fullName, false, true, IS_FORMAT_HYPHEN, IS_FORMAT_UNDERLINE, IS_FORMAT_DOLLAR
, IS_FORMAT_HYPHEN || IS_FORMAT_UNDERLINE || IS_FORMAT_DOLLAR ? false : null);
}


Expand All @@ -460,10 +467,13 @@ public static String formatOtherKey(String fullName) {
* @param formatAt 去除前缀 @ , @a => a
* @param formatColon 去除分隔符 : , A:b => b
* @param formatHyphen 去除分隔符 - , A-b-cd-Efg => aBCdEfg
* @param formatUnderline 去除分隔符 _ , A_b_cd_Efg => aBCdEfg
* @param formatDollar 去除分隔符 $ , A$b$cd$Efg => aBCdEfg
* @param firstCase 第一个单词首字母小写,后面的首字母大写, Ab => ab ; A-b-Cd => aBCd
* @return name => name; name:alias => alias
*/
public static String formatKey(String fullName, boolean formatColon, boolean formatAt, boolean formatHyphen, boolean firstCase) {
public static String formatKey(String fullName, boolean formatColon, boolean formatAt, boolean formatHyphen
, boolean formatUnderline, boolean formatDollar, Boolean firstCase) {
if (fullName == null) {
Log.w(TAG, "formatKey fullName == null >> return null;");
return null;
Expand All @@ -476,10 +486,17 @@ public static String formatKey(String fullName, boolean formatColon, boolean for
fullName = formatAt(fullName);
}
if (formatHyphen) {
fullName = formatHyphen(fullName, firstCase);
fullName = formatHyphen(fullName, firstCase != null);
}
if (formatUnderline) {
fullName = formatUnderline(fullName, firstCase != null);
}
if (formatDollar) {
fullName = formatDollar(fullName, firstCase != null);
}

return firstCase ? StringUtil.firstCase(fullName) : fullName; //不格式化普通 key:value (value 不为 [], {}) 的 key
// 默认不格式化普通 key:value (value 不为 [], {}) 的 key
return firstCase == null ? fullName : StringUtil.firstCase(fullName, firstCase);
}

/**"@key" => "key"
Expand All @@ -489,6 +506,7 @@ public static String formatKey(String fullName, boolean formatColon, boolean for
public static String formatAt(@NotNull String key) {
return key.startsWith("@") ? key.substring(1) : key;
}

/**key:alias => alias
* @param key
* @return
Expand All @@ -502,15 +520,60 @@ public static String formatColon(@NotNull String key) {
* @param key
* @return
*/
public static String formatHyphen(@NotNull String key, boolean firstCase) {
String name = "";
public static String formatHyphen(@NotNull String key, Boolean firstCase) {
return formatDivider(key, "-", firstCase);
}

/**A_b_cd_Efg => ABCdEfg
* @param key
* @return
*/
public static String formatUnderline(@NotNull String key, Boolean firstCase) {
return formatDivider(key, "_", firstCase);
}

StringTokenizer parts = new StringTokenizer(key, "-");
name += parts.nextToken();
while(parts.hasMoreTokens()) {
String part = parts.nextToken();
name += firstCase ? StringUtil.firstCase(part, true) : part;
/**A$b$cd$Efg => ABCdEfg
* @param key
* @return
*/
public static String formatDollar(@NotNull String key, Boolean firstCase) {
return formatDivider(key, "$", firstCase);
}

/**A.b.cd.Efg => ABCdEfg
* @param key
* @return
*/
public static String formatDot(@NotNull String key, Boolean firstCase) {
return formatDivider(key, ".", firstCase);
}

/**A/b/cd/Efg => ABCdEfg
* @param key
* @return
*/
public static String formatDivider(@NotNull String key, Boolean firstCase) {
return formatDivider(key, "/", firstCase);
}

/**去除分割符,返回驼峰格式
* @param key
* @param divider
* @param firstCase
* @return
*/
public static String formatDivider(@NotNull String key, @NotNull String divider, Boolean firstCase) {
String[] parts = StringUtil.split(key, divider);
StringBuilder name = new StringBuilder();
for (String part : parts) {
part = part.toLowerCase(); // 始终小写,也方便反过来 ABCdEfg -> A_b_cd_Efg
if (firstCase != null) {
// 始终小写, A_b_cd_Efg -> ABCdEfg, firstCase ? part.toLowerCase() : part.toUpperCase();
part = StringUtil.firstCase(part, firstCase);
}
name.append(part);
}
return name;
return name.toString();
}

}

0 comments on commit 2b0d612

Please sign in to comment.