mirror of
https://github.com/NoCheatPlus/NoCheatPlus.git
synced 2025-01-15 03:51:20 +01:00
Spaces.
This commit is contained in:
parent
aa4ef31051
commit
0201e4178b
@ -13,156 +13,156 @@ import java.util.List;
|
||||
*
|
||||
*/
|
||||
public class StringUtil {
|
||||
|
||||
/** Decimal format for "#.###" */
|
||||
public static final DecimalFormat fdec3 = new DecimalFormat();
|
||||
/** Decimal format for "#.#" */
|
||||
public static final DecimalFormat fdec1 = new DecimalFormat();
|
||||
|
||||
static {
|
||||
// 3 digits.
|
||||
DecimalFormatSymbols sym = fdec3.getDecimalFormatSymbols();
|
||||
sym.setDecimalSeparator('.');
|
||||
fdec3.setDecimalFormatSymbols(sym);
|
||||
fdec3.setMaximumFractionDigits(3);
|
||||
fdec3.setMinimumIntegerDigits(1);
|
||||
// 1 digit.
|
||||
sym = fdec1.getDecimalFormatSymbols();
|
||||
sym.setDecimalSeparator('.');
|
||||
fdec1.setDecimalFormatSymbols(sym);
|
||||
fdec1.setMaximumFractionDigits(1);
|
||||
fdec1.setMinimumIntegerDigits(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Join parts with link.
|
||||
*
|
||||
* @param input
|
||||
* @param link
|
||||
* @return
|
||||
*/
|
||||
public static <O extends Object> String join(final Collection<O> input, final String link)
|
||||
{
|
||||
final StringBuilder builder = new StringBuilder(Math.max(300, input.size() * 10));
|
||||
boolean first = true;
|
||||
for (final Object obj : input) {
|
||||
if (!first) builder.append(link);
|
||||
builder.append(obj.toString());
|
||||
first = false;
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Split input by all characters given (convenience method).
|
||||
* @param input
|
||||
* @param chars
|
||||
* @return
|
||||
*/
|
||||
public static List<String> split(String input, Character... chars){
|
||||
List<String> out = new LinkedList<String>();
|
||||
out.add(input);
|
||||
List<String> queue = new LinkedList<String>();
|
||||
for (final char c : chars){
|
||||
String hex = Integer.toHexString((int) c);
|
||||
switch (hex.length()){
|
||||
case 1:
|
||||
hex = "000" + hex;
|
||||
break;
|
||||
case 2:
|
||||
hex = "00" + hex;
|
||||
break;
|
||||
case 3:
|
||||
hex = "0" + hex;
|
||||
}
|
||||
for (final String s : out){
|
||||
final String[] split = s.split("\\u" + hex);
|
||||
for (final String _s : split){
|
||||
queue.add(_s);
|
||||
}
|
||||
}
|
||||
List<String> temp = out;
|
||||
out = queue;
|
||||
queue = temp;
|
||||
queue.clear();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
/** Decimal format for "#.###" */
|
||||
public static final DecimalFormat fdec3 = new DecimalFormat();
|
||||
/** Decimal format for "#.#" */
|
||||
public static final DecimalFormat fdec1 = new DecimalFormat();
|
||||
|
||||
/**
|
||||
* Return if the two Strings are similar based on the given threshold.
|
||||
*
|
||||
* @param s
|
||||
* the first String, must not be null
|
||||
* @param t
|
||||
* the second String, must not be null
|
||||
* @param threshold
|
||||
* the minimum value of the correlation coefficient
|
||||
* @return result true if the two Strings are similar, false otherwise
|
||||
*/
|
||||
public static boolean isSimilar(final String s, final String t, final float threshold)
|
||||
{
|
||||
return 1.0f - (float) levenshteinDistance(s, t) / Math.max(1.0, Math.max(s.length(), t.length())) > threshold;
|
||||
}
|
||||
static {
|
||||
// 3 digits.
|
||||
DecimalFormatSymbols sym = fdec3.getDecimalFormatSymbols();
|
||||
sym.setDecimalSeparator('.');
|
||||
fdec3.setDecimalFormatSymbols(sym);
|
||||
fdec3.setMaximumFractionDigits(3);
|
||||
fdec3.setMinimumIntegerDigits(1);
|
||||
// 1 digit.
|
||||
sym = fdec1.getDecimalFormatSymbols();
|
||||
sym.setDecimalSeparator('.');
|
||||
fdec1.setDecimalFormatSymbols(sym);
|
||||
fdec1.setMaximumFractionDigits(1);
|
||||
fdec1.setMinimumIntegerDigits(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Join parts with link.
|
||||
*
|
||||
* @param input
|
||||
* @param link
|
||||
* @return
|
||||
*/
|
||||
public static <O extends Object> String join(final Collection<O> input, final String link)
|
||||
{
|
||||
final StringBuilder builder = new StringBuilder(Math.max(300, input.size() * 10));
|
||||
boolean first = true;
|
||||
for (final Object obj : input) {
|
||||
if (!first) builder.append(link);
|
||||
builder.append(obj.toString());
|
||||
first = false;
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Split input by all characters given (convenience method).
|
||||
* @param input
|
||||
* @param chars
|
||||
* @return
|
||||
*/
|
||||
public static List<String> split(String input, Character... chars){
|
||||
List<String> out = new LinkedList<String>();
|
||||
out.add(input);
|
||||
List<String> queue = new LinkedList<String>();
|
||||
for (final char c : chars){
|
||||
String hex = Integer.toHexString((int) c);
|
||||
switch (hex.length()){
|
||||
case 1:
|
||||
hex = "000" + hex;
|
||||
break;
|
||||
case 2:
|
||||
hex = "00" + hex;
|
||||
break;
|
||||
case 3:
|
||||
hex = "0" + hex;
|
||||
}
|
||||
for (final String s : out){
|
||||
final String[] split = s.split("\\u" + hex);
|
||||
for (final String _s : split){
|
||||
queue.add(_s);
|
||||
}
|
||||
}
|
||||
List<String> temp = out;
|
||||
out = queue;
|
||||
queue = temp;
|
||||
queue.clear();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if the two Strings are similar based on the given threshold.
|
||||
*
|
||||
* @param s
|
||||
* the first String, must not be null
|
||||
* @param t
|
||||
* the second String, must not be null
|
||||
* @param threshold
|
||||
* the minimum value of the correlation coefficient
|
||||
* @return result true if the two Strings are similar, false otherwise
|
||||
*/
|
||||
public static boolean isSimilar(final String s, final String t, final float threshold)
|
||||
{
|
||||
return 1.0f - (float) levenshteinDistance(s, t) / Math.max(1.0, Math.max(s.length(), t.length())) > threshold;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the Levenshtein distance between two Strings.
|
||||
*
|
||||
* This is the number of changes needed to change one String into another,
|
||||
* where each change is a single character modification (deletion, insertion or substitution).
|
||||
*
|
||||
* @param s
|
||||
* the first String, must not be null
|
||||
* @param t
|
||||
* the second String, must not be null
|
||||
* @return result distance
|
||||
*/
|
||||
public static int levenshteinDistance(CharSequence s, CharSequence t) {
|
||||
if (s == null || t == null) throw new IllegalArgumentException("Strings must not be null");
|
||||
|
||||
int n = s.length();
|
||||
int m = t.length();
|
||||
|
||||
if (n == 0) return m;
|
||||
else if (m == 0) return n;
|
||||
|
||||
if (n > m) {
|
||||
final CharSequence tmp = s;
|
||||
s = t;
|
||||
t = tmp;
|
||||
n = m;
|
||||
m = t.length();
|
||||
}
|
||||
|
||||
int p[] = new int[n + 1];
|
||||
int d[] = new int[n + 1];
|
||||
int _d[];
|
||||
|
||||
int i;
|
||||
int j;
|
||||
|
||||
char t_j;
|
||||
|
||||
int cost;
|
||||
|
||||
for (i = 0; i <= n; i++)
|
||||
p[i] = i;
|
||||
|
||||
for (j = 1; j <= m; j++) {
|
||||
t_j = t.charAt(j - 1);
|
||||
d[0] = j;
|
||||
|
||||
for (i = 1; i <= n; i++) {
|
||||
cost = s.charAt(i - 1) == t_j ? 0 : 1;
|
||||
d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
|
||||
}
|
||||
|
||||
_d = p;
|
||||
p = d;
|
||||
d = _d;
|
||||
}
|
||||
|
||||
return p[n];
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the Levenshtein distance between two Strings.
|
||||
*
|
||||
* This is the number of changes needed to change one String into another,
|
||||
* where each change is a single character modification (deletion, insertion or substitution).
|
||||
*
|
||||
* @param s
|
||||
* the first String, must not be null
|
||||
* @param t
|
||||
* the second String, must not be null
|
||||
* @return result distance
|
||||
*/
|
||||
public static int levenshteinDistance(CharSequence s, CharSequence t) {
|
||||
if (s == null || t == null) throw new IllegalArgumentException("Strings must not be null");
|
||||
|
||||
int n = s.length();
|
||||
int m = t.length();
|
||||
|
||||
if (n == 0) return m;
|
||||
else if (m == 0) return n;
|
||||
|
||||
if (n > m) {
|
||||
final CharSequence tmp = s;
|
||||
s = t;
|
||||
t = tmp;
|
||||
n = m;
|
||||
m = t.length();
|
||||
}
|
||||
|
||||
int p[] = new int[n + 1];
|
||||
int d[] = new int[n + 1];
|
||||
int _d[];
|
||||
|
||||
int i;
|
||||
int j;
|
||||
|
||||
char t_j;
|
||||
|
||||
int cost;
|
||||
|
||||
for (i = 0; i <= n; i++)
|
||||
p[i] = i;
|
||||
|
||||
for (j = 1; j <= m; j++) {
|
||||
t_j = t.charAt(j - 1);
|
||||
d[0] = j;
|
||||
|
||||
for (i = 1; i <= n; i++) {
|
||||
cost = s.charAt(i - 1) == t_j ? 0 : 1;
|
||||
d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
|
||||
}
|
||||
|
||||
_d = p;
|
||||
p = d;
|
||||
d = _d;
|
||||
}
|
||||
|
||||
return p[n];
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,95 +18,95 @@ import java.util.logging.Logger;
|
||||
*/
|
||||
public class StaticLogFile {
|
||||
|
||||
/**
|
||||
* The formatter that is used to format the log file.
|
||||
*/
|
||||
protected static class LogFileFormatter extends Formatter {
|
||||
|
||||
/**
|
||||
* Create a new instance of the log file formatter.
|
||||
*
|
||||
* @return the log file formatter
|
||||
*/
|
||||
public static LogFileFormatter newInstance() {
|
||||
return new LogFileFormatter();
|
||||
}
|
||||
|
||||
/** The date formatter. */
|
||||
private final SimpleDateFormat date;
|
||||
|
||||
/**
|
||||
* Instantiates a new log file formatter.
|
||||
*/
|
||||
private LogFileFormatter() {
|
||||
date = new SimpleDateFormat("yy.MM.dd HH:mm:ss");
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
|
||||
*/
|
||||
@Override
|
||||
public String format(final LogRecord record) {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
final Throwable ex = record.getThrown();
|
||||
|
||||
builder.append(date.format(record.getMillis()));
|
||||
builder.append(" [");
|
||||
builder.append(record.getLevel().getLocalizedName().toUpperCase());
|
||||
builder.append("] ");
|
||||
builder.append(record.getMessage());
|
||||
builder.append('\n');
|
||||
|
||||
if (ex != null) {
|
||||
final StringWriter writer = new StringWriter();
|
||||
ex.printStackTrace(new PrintWriter(writer));
|
||||
builder.append(writer);
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
/** The file logger. */
|
||||
public static Logger fileLogger = null;
|
||||
/** The file handler. */
|
||||
private static FileHandler fileHandler = null;
|
||||
/**
|
||||
* The formatter that is used to format the log file.
|
||||
*/
|
||||
protected static class LogFileFormatter extends Formatter {
|
||||
|
||||
/**
|
||||
* Cleanup.
|
||||
*/
|
||||
public static void cleanup() {
|
||||
fileHandler.flush();
|
||||
fileHandler.close();
|
||||
final Logger logger = Logger.getLogger("NoCheatPlus");
|
||||
logger.removeHandler(fileHandler);
|
||||
fileHandler = null;
|
||||
}
|
||||
public static void setupLogger(File logFile){
|
||||
// Setup the file logger.
|
||||
final Logger logger = Logger.getAnonymousLogger();
|
||||
logger.setLevel(Level.INFO);
|
||||
logger.setUseParentHandlers(false);
|
||||
for (final Handler h : logger.getHandlers())
|
||||
logger.removeHandler(h);
|
||||
if (fileHandler != null) {
|
||||
fileHandler.close();
|
||||
logger.removeHandler(fileHandler);
|
||||
fileHandler = null;
|
||||
}
|
||||
try {
|
||||
try {
|
||||
logFile.getParentFile().mkdirs();
|
||||
} catch (final Exception e) {
|
||||
LogUtil.logSevere(e);
|
||||
}
|
||||
fileHandler = new FileHandler(logFile.getCanonicalPath(), true);
|
||||
fileHandler.setLevel(Level.ALL);
|
||||
fileHandler.setFormatter(StaticLogFile.LogFileFormatter.newInstance());
|
||||
logger.addHandler(fileHandler);
|
||||
} catch (final Exception e) {
|
||||
LogUtil.logSevere(e);
|
||||
}
|
||||
fileLogger = logger;
|
||||
}
|
||||
/**
|
||||
* Create a new instance of the log file formatter.
|
||||
*
|
||||
* @return the log file formatter
|
||||
*/
|
||||
public static LogFileFormatter newInstance() {
|
||||
return new LogFileFormatter();
|
||||
}
|
||||
|
||||
/** The date formatter. */
|
||||
private final SimpleDateFormat date;
|
||||
|
||||
/**
|
||||
* Instantiates a new log file formatter.
|
||||
*/
|
||||
private LogFileFormatter() {
|
||||
date = new SimpleDateFormat("yy.MM.dd HH:mm:ss");
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
|
||||
*/
|
||||
@Override
|
||||
public String format(final LogRecord record) {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
final Throwable ex = record.getThrown();
|
||||
|
||||
builder.append(date.format(record.getMillis()));
|
||||
builder.append(" [");
|
||||
builder.append(record.getLevel().getLocalizedName().toUpperCase());
|
||||
builder.append("] ");
|
||||
builder.append(record.getMessage());
|
||||
builder.append('\n');
|
||||
|
||||
if (ex != null) {
|
||||
final StringWriter writer = new StringWriter();
|
||||
ex.printStackTrace(new PrintWriter(writer));
|
||||
builder.append(writer);
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
/** The file logger. */
|
||||
public static Logger fileLogger = null;
|
||||
/** The file handler. */
|
||||
private static FileHandler fileHandler = null;
|
||||
|
||||
/**
|
||||
* Cleanup.
|
||||
*/
|
||||
public static void cleanup() {
|
||||
fileHandler.flush();
|
||||
fileHandler.close();
|
||||
final Logger logger = Logger.getLogger("NoCheatPlus");
|
||||
logger.removeHandler(fileHandler);
|
||||
fileHandler = null;
|
||||
}
|
||||
public static void setupLogger(File logFile){
|
||||
// Setup the file logger.
|
||||
final Logger logger = Logger.getAnonymousLogger();
|
||||
logger.setLevel(Level.INFO);
|
||||
logger.setUseParentHandlers(false);
|
||||
for (final Handler h : logger.getHandlers())
|
||||
logger.removeHandler(h);
|
||||
if (fileHandler != null) {
|
||||
fileHandler.close();
|
||||
logger.removeHandler(fileHandler);
|
||||
fileHandler = null;
|
||||
}
|
||||
try {
|
||||
try {
|
||||
logFile.getParentFile().mkdirs();
|
||||
} catch (final Exception e) {
|
||||
LogUtil.logSevere(e);
|
||||
}
|
||||
fileHandler = new FileHandler(logFile.getCanonicalPath(), true);
|
||||
fileHandler.setLevel(Level.ALL);
|
||||
fileHandler.setFormatter(StaticLogFile.LogFileFormatter.newInstance());
|
||||
logger.addHandler(fileHandler);
|
||||
} catch (final Exception e) {
|
||||
LogUtil.logSevere(e);
|
||||
}
|
||||
fileLogger = logger;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user