利用匿名函数优化重复代码

This commit is contained in:
makejava 2021-08-09 09:54:31 +08:00
parent 61916b04a5
commit 78535a099e
2 changed files with 39 additions and 50 deletions

View File

@ -26,11 +26,11 @@ public class ProjectUtils {
* @return 当前项目对象
*/
public static Project getCurrProject() {
ProjectManager projectManager = ProjectManager.getInstance();
Project[] openProjects = projectManager.getOpenProjects();
if (openProjects.length == 0) {
return projectManager.getDefaultProject();//正常情况下不会发生
// 在未打开任何项目进入到设置页面时会出现
return projectManager.getDefaultProject();
} else if (openProjects.length == 1) {
// 只存在一个打开的项目则使用打开的项目
return openProjects[0];

View File

@ -1,5 +1,8 @@
package com.sjhy.plugin.tool;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* 添加字符串工具类为了兼容JB的各种产品尽量不要用第三方工具包
*
@ -7,7 +10,39 @@ package com.sjhy.plugin.tool;
* @version 1.0.0
* @since 2018/08/07 11:52
*/
@SuppressWarnings("WeakerAccess")
public class StringUtils {
/**
* 首字母处理方法
*/
private static final BiFunction<String, Function<Integer, Integer>, String> FIRST_CHAR_HANDLER_FUN = (str, firstCharFun) -> {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return str;
}
final int firstCodepoint = str.codePointAt(0);
final int newCodePoint = firstCharFun.apply(firstCodepoint);
if (firstCodepoint == newCodePoint) {
// already capitalized
return str;
}
// cannot be longer than the char array
final int[] newCodePoints = new int[strLen];
int outOffset = 0;
// copy the first codepoint
newCodePoints[outOffset++] = newCodePoint;
for (int inOffset = Character.charCount(firstCodepoint); inOffset < strLen; ) {
final int codepoint = str.codePointAt(inOffset);
// copy the remaining ones
newCodePoints[outOffset++] = codepoint;
inOffset += Character.charCount(codepoint);
}
return new String(newCodePoints, 0, outOffset);
};
/**
* 判断是空字符串
*
@ -24,30 +59,7 @@ public class StringUtils {
* @return 首字母大写结果
*/
public static String capitalize(final String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return str;
}
final int firstCodepoint = str.codePointAt(0);
final int newCodePoint = Character.toTitleCase(firstCodepoint);
if (firstCodepoint == newCodePoint) {
// already capitalized
return str;
}
// cannot be longer than the char array
final int newCodePoints[] = new int[strLen];
int outOffset = 0;
// copy the first codepoint
newCodePoints[outOffset++] = newCodePoint;
for (int inOffset = Character.charCount(firstCodepoint); inOffset < strLen; ) {
final int codepoint = str.codePointAt(inOffset);
// copy the remaining ones
newCodePoints[outOffset++] = codepoint;
inOffset += Character.charCount(codepoint);
}
return new String(newCodePoints, 0, outOffset);
return FIRST_CHAR_HANDLER_FUN.apply(str, Character::toTitleCase);
}
/**
@ -56,29 +68,6 @@ public class StringUtils {
* @return 首字母小写结果
*/
public static String uncapitalize(final String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return str;
}
final int firstCodepoint = str.codePointAt(0);
final int newCodePoint = Character.toLowerCase(firstCodepoint);
if (firstCodepoint == newCodePoint) {
// already capitalized
return str;
}
// cannot be longer than the char array
final int newCodePoints[] = new int[strLen];
int outOffset = 0;
// copy the first codepoint
newCodePoints[outOffset++] = newCodePoint;
for (int inOffset = Character.charCount(firstCodepoint); inOffset < strLen; ) {
final int codepoint = str.codePointAt(inOffset);
// copy the remaining ones
newCodePoints[outOffset++] = codepoint;
inOffset += Character.charCount(codepoint);
}
return new String(newCodePoints, 0, outOffset);
return FIRST_CHAR_HANDLER_FUN.apply(str, Character::toLowerCase);
}
}