TabooLib/src/main/scala/io/izzel/taboolib/util/Strings.java

171 lines
5.6 KiB
Java
Raw Normal View History

package io.izzel.taboolib.util;
import com.google.common.collect.Lists;
2018-02-06 06:46:14 +00:00
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
2020-05-05 17:39:21 +00:00
import java.util.Arrays;
2020-01-14 13:21:55 +00:00
import java.util.stream.Collectors;
import java.util.stream.IntStream;
2018-02-06 06:46:14 +00:00
public class Strings {
2020-01-14 13:21:55 +00:00
public static String copy(String text, int count) {
return IntStream.range(0, count).mapToObj(i -> text).collect(Collectors.joining());
}
public static boolean nonBlack(String var) {
return !isBlank(var);
}
public static boolean isBlank(String var) {
return var == null || var.trim().isEmpty();
}
public static boolean nonEmpty(CharSequence var) {
return !isEmpty(var);
}
public static boolean isEmpty(CharSequence var) {
return var == null || var.length() == 0;
}
public static String replaceWithOrder(String template, String... args) {
return replaceWithOrder(template, Lists.newArrayList(args).toArray());
}
/**
* String#replace 5
*
* @param template
* @param args
* @return
*/
public static String replaceWithOrder(String template, Object... args) {
if (args.length == 0 || template.length() == 0) {
return template;
}
char[] arr = template.toCharArray();
StringBuilder stringBuilder = new StringBuilder(template.length());
for (int i = 0; i < arr.length; i++) {
if (arr[i] == '{' && Character.isDigit(arr[Math.min(i + 1, arr.length - 1)])
&& arr[Math.min(i + 1, arr.length - 1)] - '0' < args.length
&& arr[Math.min(i + 2, arr.length - 1)] == '}') {
stringBuilder.append(args[arr[i + 1] - '0']);
i += 2;
} else {
stringBuilder.append(arr[i]);
}
}
return stringBuilder.toString();
}
public static String hashKeyForDisk(String key) {
2020-04-29 07:21:06 +00:00
return hashKeyForDisk(key, "MD5");
}
public static String hashKeyForDisk(String key, String type) {
2018-02-06 06:46:14 +00:00
String cacheKey;
try {
2020-04-29 07:21:06 +00:00
final MessageDigest mDigest = MessageDigest.getInstance(type);
2018-02-06 06:46:14 +00:00
mDigest.update(key.getBytes());
cacheKey = bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {
cacheKey = String.valueOf(key.hashCode());
}
return cacheKey;
}
2020-05-05 17:39:21 +00:00
public static String hashKeyForDisk(byte[] key, String type) {
String cacheKey;
try {
final MessageDigest mDigest = MessageDigest.getInstance(type);
mDigest.update(key);
cacheKey = bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {
cacheKey = String.valueOf(Arrays.hashCode(key));
}
return cacheKey;
}
2018-05-07 15:58:32 +00:00
public static double similarDegree(String strA, String strB){
String newStrA = removeSign(max(strA, strB));
String newStrB = removeSign(min(strA, strB));
try {
int temp = Math.max(newStrA.length(), newStrB.length());
int temp2 = longestCommonSubstring(newStrA, newStrB).length();
return temp2 * 1.0 / temp;
} catch (Exception ignored) {
return 0;
}
2018-05-07 15:58:32 +00:00
}
private static String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte aByte : bytes) {
String hex = Integer.toHexString(0xFF & aByte);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
2018-05-07 15:58:32 +00:00
private static String max(String strA, String strB) {
return strA.length() >= strB.length() ? strA : strB;
}
private static String min(String strA, String strB) {
return strA.length() < strB.length() ? strA : strB;
}
private static String removeSign(String str) {
StringBuilder builder = new StringBuilder();
2018-05-07 15:58:32 +00:00
for (char item : str.toCharArray()) {
if (charReg(item)){
builder.append(item);
2018-05-07 15:58:32 +00:00
}
}
return builder.toString();
2018-05-07 15:58:32 +00:00
}
private static boolean charReg(char charValue) {
return (charValue >= 0x4E00 && charValue <= 0X9FA5) || (charValue >= 'a' && charValue <= 'z') || (charValue >= 'A' && charValue <= 'Z') || (charValue >= '0' && charValue <= '9');
}
private static String longestCommonSubstring(String strA, String strB) {
char[] chars_strA = strA.toCharArray();
char[] chars_strB = strB.toCharArray();
int m = chars_strA.length;
int n = chars_strB.length;
int[][] matrix = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
2018-05-08 15:31:26 +00:00
if (chars_strA[i - 1] == chars_strB[j - 1]) {
2018-05-07 15:58:32 +00:00
matrix[i][j] = matrix[i - 1][j - 1] + 1;
2018-05-08 15:31:26 +00:00
} else {
2018-05-07 15:58:32 +00:00
matrix[i][j] = Math.max(matrix[i][j - 1], matrix[i - 1][j]);
2018-05-08 15:31:26 +00:00
}
2018-05-07 15:58:32 +00:00
}
}
char[] result = new char[matrix[m][n]];
int currentIndex = result.length - 1;
while (matrix[m][n] != 0) {
2018-05-08 15:31:26 +00:00
if (matrix[n] == matrix[n - 1]) {
2018-05-07 15:58:32 +00:00
n--;
2018-05-08 15:31:26 +00:00
} else if (matrix[m][n] == matrix[m - 1][n]) {
2018-05-07 15:58:32 +00:00
m--;
2018-05-08 15:31:26 +00:00
} else {
2018-05-07 15:58:32 +00:00
result[currentIndex] = chars_strA[m - 1];
currentIndex--;
n--;
m--;
}
}
return new String(result);
}
2018-02-06 06:46:14 +00:00
}