Skip to content

Commit

Permalink
优化代码性能和可读性,修复潜在的多线程问题
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng committed May 28, 2024
1 parent 6e25a3a commit 6fb4bff
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/main/java/cc/baka9/catseedlogin/util/Util.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
package cc.baka9.catseedlogin.util;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
import java.util.regex.Pattern;

public class Util {
private static Pattern passwordDifficultyRegex = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
private static final Pattern passwordDifficultyRegex = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final Random random = new SecureRandom();

public static boolean passwordIsDifficulty(String pwd){
public static boolean passwordIsDifficulty(String pwd) {
return passwordDifficultyRegex.matcher(pwd).find();
}

private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public static String time2Str(long time){
return sdf.format(new Date(time));
public static String time2Str(long time) {
synchronized (sdf) {
return sdf.format(new Date(time));
}
}

public static boolean checkMail(String e_mail){
public static boolean checkMail(String e_mail) {
return e_mail.matches("[a-zA-Z0-9_]+@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)+");
}

public static String randomStr(){
public static String randomStr() {
int leftLimit = 97; // letter 'a'
int rightLimit = 122; // letter 'z'
int targetStringLength = 10;
Random random = new Random();
StringBuilder buffer = new StringBuilder(targetStringLength);
for (int i = 0; i < targetStringLength; i++) {
int randomLimitedInt = leftLimit + (int)
(random.nextFloat() * (rightLimit - leftLimit + 1));
int randomLimitedInt = leftLimit + random.nextInt(rightLimit - leftLimit + 1);
buffer.append((char) randomLimitedInt);
}

return buffer.toString();
}

public static boolean isOSLinux(){
Properties prop = System.getProperties();
String os = prop.getProperty("os.name");
public static boolean isOSLinux() {
String os = System.getProperty("os.name");
return os != null && os.toLowerCase().contains("linux");
}


}
}

0 comments on commit 6fb4bff

Please sign in to comment.