001/* 002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.mybatisflex.processor.util; 018 019/** 020 * 字符串工具类。 021 * 022 * @author 王帅 023 * @since 2023-06-22 024 */ 025@SuppressWarnings("all") 026public class StrUtil { 027 028 private StrUtil() { 029 } 030 031 /** 032 * com.mybatisflex.test.entity.Account -> Account 033 */ 034 public static String getClassName(String str) { 035 return str.substring(str.lastIndexOf(".") + 1); 036 } 037 038 public static boolean isBlank(String str) { 039 return str == null || str.trim().length() == 0; 040 } 041 042 public static String camelToUnderline(String str) { 043 if (isBlank(str)) { 044 return ""; 045 } 046 int len = str.length(); 047 StringBuilder sb = new StringBuilder(len); 048 for (int i = 0; i < len; i++) { 049 char c = str.charAt(i); 050 if (Character.isUpperCase(c) && i > 0) { 051 sb.append('_'); 052 } 053 sb.append(Character.toLowerCase(c)); 054 } 055 return sb.toString(); 056 } 057 058 public static String firstCharToLowerCase(String str) { 059 char firstChar = str.charAt(0); 060 if (firstChar >= 'A' && firstChar <= 'Z') { 061 char[] arr = str.toCharArray(); 062 arr[0] += ('a' - 'A'); 063 return new String(arr); 064 } 065 return str; 066 } 067 068 public static String firstCharToUpperCase(String str) { 069 char firstChar = str.charAt(0); 070 if (firstChar >= 'a' && firstChar <= 'z') { 071 char[] arr = str.toCharArray(); 072 arr[0] -= ('a' - 'A'); 073 return new String(arr); 074 } 075 return str; 076 } 077 078 public static String buildFieldName(String name, String tableDefPropertiesNameStyle) { 079 if ("upperCase".equalsIgnoreCase(tableDefPropertiesNameStyle)) { 080 return camelToUnderline(name).toUpperCase(); 081 } else if ("lowerCase".equalsIgnoreCase(tableDefPropertiesNameStyle)) { 082 return camelToUnderline(name).toLowerCase(); 083 } else if ("upperCamelCase".equalsIgnoreCase(tableDefPropertiesNameStyle)) { 084 return firstCharToUpperCase(name); 085 } else { 086 //lowerCamelCase 087 return firstCharToLowerCase(name); 088 } 089 } 090 091 public static String buildTableDefPackage(String entityClass) { 092 StringBuilder guessPackage = new StringBuilder(); 093 if (!entityClass.contains(".")) { 094 guessPackage.append("table"); 095 } else { 096 guessPackage.append(entityClass, 0, entityClass.lastIndexOf(".")).append(".table"); 097 } 098 return guessPackage.toString(); 099 } 100 101 public static String buildMapperPackage(String entityClass) { 102 if (!entityClass.contains(".")) { 103 return "mapper"; 104 } else { 105 String entityPackage = entityClass.substring(0, entityClass.lastIndexOf(".")); 106 if (entityPackage.contains(".")) { 107 return entityPackage.substring(0, entityPackage.lastIndexOf(".")) + ".mapper"; 108 } else { 109 return "mapper"; 110 } 111 } 112 } 113 114 115 public static boolean isGetterMethod(String methodName, String property) { 116 if (methodName.startsWith("get") && methodName.length() > 3) { 117 return firstCharToUpperCase(property).concat("()").equals(methodName.substring(3)); 118 } else if (methodName.startsWith("is") && methodName.length() > 2) { 119 return firstCharToUpperCase(property).concat("()").equals(methodName.substring(2)); 120 } else { 121 return false; 122 } 123 } 124 125}