cn.cliveyuan.robin.generator.util.GeneratorUtils Maven / Gradle / Ivy
package cn.cliveyuan.robin.generator.util;
import org.apache.commons.lang3.StringUtils;
import java.util.Objects;
/**
* 生成工具类
*
* @author Clive Yuan
* @date 2020/09/09
*/
public class GeneratorUtils {
/**
* 获取小写驼峰名
*
* user -> user
* User -> user
* USER -> user
* UsER -> usER
* TemplateEnterprise -> templateEnterprise
* t_user_info -> tUserInfo
* user_list_detail -> userListDetail
* USER_LIST_DETAIL -> userListDetail
*
* @param str 字符串
* @return
*/
public static String getLowerCamelName(String str) {
// 全大写, 包含下划线
Objects.requireNonNull(str, "str can't be null");
if (str.contains("_")) {
return StringUtils.uncapitalize(lineToHump(str));
}
if (StringUtils.isAllUpperCase(str)) {
return str.toLowerCase();
}
return StringUtils.uncapitalize(str);
}
public static String lineToHump(String name) {
// return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, str.toUpperCase());
if (null == name || name.length() == 0) {
return null;
}
int length = name.length();
StringBuilder sb = new StringBuilder(length);
boolean underLineNextChar = false;
for (int i = 0; i < length; ++i) {
char c = name.charAt(i);
if (c == '_') {
underLineNextChar = true;
} else if (underLineNextChar) {
sb.append(Character.toUpperCase(c));
underLineNextChar = false;
} else {
sb.append(c);
}
}
return sb.toString();
}
}