com.sc.utils.beanmap.JavaBeanUtil Maven / Gradle / Ivy
package com.sc.utils.beanmap;
import java.util.Locale;
public class JavaBeanUtil {
private static final char SEPARATOR = '_';
public static String toUnderlineString(String inputString) {
if (inputString == null)
return null;
StringBuilder sb = new StringBuilder();
boolean upperCase = false;
for (int i = 0; i < inputString.length(); i++) {
char c = inputString.charAt(i);
boolean nextUpperCase = true;
if (i < (inputString.length() - 1)) {
nextUpperCase = Character.isUpperCase(inputString.charAt(i + 1));
}
if ((i >= 0) && Character.isUpperCase(c)) {
if (!upperCase || !nextUpperCase) {
if (i > 0)
sb.append(SEPARATOR);
}
upperCase = true;
} else {
upperCase = false;
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
}
public static String toCamelCaseString(String inputString) {
return toCamelCaseString(inputString, false);
}
public static String toCamelCaseString(String inputString, boolean firstCharacterUppercase) {
if (inputString == null)
return null;
StringBuilder sb = new StringBuilder();
boolean nextUpperCase = false;
for (int i = 0; i < inputString.length(); i++) {
char c = inputString.charAt(i);
switch (c) {
case '_':
case '-':
case '@':
case '$':
case '#':
case ' ':
case '/':
case '&':
if (sb.length() > 0) {
nextUpperCase = true;
}
break;
default:
if (nextUpperCase) {
sb.append(Character.toUpperCase(c));
nextUpperCase = false;
}else if(Character.isUpperCase(c)) {
sb.append(Character.toUpperCase(c));
}else{
sb.append(Character.toLowerCase(c));
}
break;
}
}
if (firstCharacterUppercase) {
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
}
return sb.toString();
}
public static String getValidPropertyName(String inputString) {
String answer;
if (inputString == null) {
answer = null;
} else if (inputString.length() < 2) {
answer = inputString.toLowerCase(Locale.US);
} else {
if (Character.isUpperCase(inputString.charAt(0)) && !Character.isUpperCase(inputString.charAt(1))) {
answer = inputString.substring(0, 1).toLowerCase(Locale.US) + inputString.substring(1);
} else {
answer = inputString;
}
}
return answer;
}
public static String getSetterMethodName(String property) {
StringBuilder sb = new StringBuilder();
sb.append(property);
if (Character.isLowerCase(sb.charAt(0))) {
if (sb.length() == 1 || !Character.isUpperCase(sb.charAt(1))) {
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
}
}
sb.insert(0, "set");
return sb.toString();
}
public static String getGetterMethodName(String property) {
StringBuilder sb = new StringBuilder();
sb.append(property);
if (Character.isLowerCase(sb.charAt(0))) {
if (sb.length() == 1 || !Character.isUpperCase(sb.charAt(1))) {
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
}
}
sb.insert(0, "get");
return sb.toString();
}
public static void main(String[] args) {
System.out.println(JavaBeanUtil.toUnderlineString("ISOCertifiedStaff"));
System.out.println(JavaBeanUtil.getValidPropertyName("CertifiedStaff"));
System.out.println(JavaBeanUtil.getSetterMethodName("userID"));
System.out.println(JavaBeanUtil.getGetterMethodName("userID"));
System.out.println(JavaBeanUtil.toCamelCaseString("iso_certified_staff", true));
System.out.println(JavaBeanUtil.getValidPropertyName("certified_staff"));
System.out.println(JavaBeanUtil.toCamelCaseString("site_Id"));
}
}