- Add configurable cooldown period after sending an email for /email recovery - Change ExpiringMap to remove expired entries (like ExpiringSet) - Create method to translate durations via the messages file
74 lines
2.0 KiB
Java
74 lines
2.0 KiB
Java
package fr.xephi.authme.util;
|
|
|
|
import fr.xephi.authme.ConsoleLogger;
|
|
|
|
import java.util.Collection;
|
|
import java.util.regex.Pattern;
|
|
|
|
/**
|
|
* Utility class for various operations used in the codebase.
|
|
*/
|
|
public final class Utils {
|
|
|
|
/** Number of milliseconds in a minute. */
|
|
public static final long MILLIS_PER_MINUTE = 60_000L;
|
|
/** Number of milliseconds in an hour. */
|
|
public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
|
|
|
|
// Utility class
|
|
private Utils() {
|
|
}
|
|
|
|
/**
|
|
* Compile Pattern sneaky without throwing Exception.
|
|
*
|
|
* @param pattern pattern string to compile
|
|
*
|
|
* @return the given regex compiled into Pattern object.
|
|
*/
|
|
public static Pattern safePatternCompile(String pattern) {
|
|
try {
|
|
return Pattern.compile(pattern);
|
|
} catch (Exception e) {
|
|
ConsoleLogger.warning("Failed to compile pattern '" + pattern + "' - defaulting to allowing everything");
|
|
return Pattern.compile(".*?");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns whether the class exists in the current class loader.
|
|
*
|
|
* @param className the class name to check
|
|
*
|
|
* @return true if the class is loaded, false otherwise
|
|
*/
|
|
public static boolean isClassLoaded(String className) {
|
|
try {
|
|
Class.forName(className);
|
|
return true;
|
|
} catch (ClassNotFoundException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Null-safe way to check whether a collection is empty or not.
|
|
*
|
|
* @param coll The collection to verify
|
|
* @return True if the collection is null or empty, false otherwise
|
|
*/
|
|
public static boolean isCollectionEmpty(Collection<?> coll) {
|
|
return coll == null || coll.isEmpty();
|
|
}
|
|
|
|
/**
|
|
* Return the available core count of the JVM.
|
|
*
|
|
* @return the core count
|
|
*/
|
|
public static int getCoreCount() {
|
|
return Runtime.getRuntime().availableProcessors();
|
|
}
|
|
|
|
}
|