2015-07-06 01:44:10 +10:00
|
|
|
|
package com.intellectualcrafters.plot.util;
|
|
|
|
|
|
2015-08-02 04:01:41 +10:00
|
|
|
|
import java.lang.reflect.Array;
|
2015-08-07 02:05:15 +10:00
|
|
|
|
import java.util.Arrays;
|
2015-07-27 04:38:08 +10:00
|
|
|
|
import java.util.Collection;
|
2015-08-07 02:05:15 +10:00
|
|
|
|
import java.util.Comparator;
|
2015-07-06 01:44:10 +10:00
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
2015-09-13 14:04:31 +10:00
|
|
|
|
public class StringMan {
|
|
|
|
|
public static String replaceFromMap(final String string, final Map<String, String> replacements) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final StringBuilder sb = new StringBuilder(string);
|
2015-07-17 20:48:13 +10:00
|
|
|
|
int size = string.length();
|
2015-09-13 14:04:31 +10:00
|
|
|
|
for (final Entry<String, String> entry : replacements.entrySet()) {
|
|
|
|
|
if (size == 0) {
|
2015-07-17 20:48:13 +10:00
|
|
|
|
break;
|
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final String key = entry.getKey();
|
|
|
|
|
final String value = entry.getValue();
|
2015-07-06 01:44:10 +10:00
|
|
|
|
int start = sb.indexOf(key, 0);
|
2015-09-13 14:04:31 +10:00
|
|
|
|
while (start > -1) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final int end = start + key.length();
|
|
|
|
|
final int nextSearchStart = start + value.length();
|
2015-07-06 01:44:10 +10:00
|
|
|
|
sb.replace(start, end, value);
|
2015-07-17 20:48:13 +10:00
|
|
|
|
size -= end - start;
|
2015-07-06 01:44:10 +10:00
|
|
|
|
start = sb.indexOf(key, nextSearchStart);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static String getString(final Object obj) {
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return "null";
|
|
|
|
|
}
|
|
|
|
|
if (obj.getClass() == String.class) {
|
|
|
|
|
return (String) obj;
|
|
|
|
|
}
|
|
|
|
|
if (obj.getClass().isArray()) {
|
2015-08-02 04:01:41 +10:00
|
|
|
|
String result = "";
|
|
|
|
|
String prefix = "";
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Array.getLength(obj); i++) {
|
2015-08-02 04:01:41 +10:00
|
|
|
|
result += prefix + getString(Array.get(obj, i));
|
|
|
|
|
prefix = ",";
|
|
|
|
|
}
|
|
|
|
|
return "( " + result + " )";
|
2015-09-13 14:04:31 +10:00
|
|
|
|
} else if (obj instanceof Collection<?>) {
|
2015-08-02 04:01:41 +10:00
|
|
|
|
String result = "";
|
|
|
|
|
String prefix = "";
|
2015-09-13 14:04:31 +10:00
|
|
|
|
for (final Object element : (Collection<?>) obj) {
|
2015-08-02 04:01:41 +10:00
|
|
|
|
result += prefix + getString(element);
|
|
|
|
|
prefix = ",";
|
|
|
|
|
}
|
|
|
|
|
return "[ " + result + " ]";
|
2015-09-13 14:04:31 +10:00
|
|
|
|
} else {
|
2015-08-02 04:01:41 +10:00
|
|
|
|
return obj.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static String replaceFirst(final char c, final String s) {
|
|
|
|
|
if (s == null) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
if (s.isEmpty()) {
|
|
|
|
|
return s;
|
|
|
|
|
}
|
2015-07-27 14:30:50 +10:00
|
|
|
|
char[] chars = s.toCharArray();
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final char[] newChars = new char[chars.length];
|
2015-07-27 14:30:50 +10:00
|
|
|
|
int used = 0;
|
|
|
|
|
boolean found = false;
|
2015-09-13 14:04:31 +10:00
|
|
|
|
for (final char cc : chars) {
|
|
|
|
|
if (!found && (c == cc)) {
|
2015-07-27 14:30:50 +10:00
|
|
|
|
found = true;
|
2015-09-13 14:04:31 +10:00
|
|
|
|
} else {
|
2015-07-27 14:30:50 +10:00
|
|
|
|
newChars[used++] = cc;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
if (found) {
|
2015-07-27 14:30:50 +10:00
|
|
|
|
chars = new char[newChars.length - 1];
|
|
|
|
|
System.arraycopy(newChars, 0, chars, 0, chars.length);
|
|
|
|
|
return String.valueOf(chars);
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static String replaceAll(final String string, final Object... pairs) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final StringBuilder sb = new StringBuilder(string);
|
2015-09-13 14:04:31 +10:00
|
|
|
|
for (int i = 0; i < pairs.length; i += 2) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final String key = pairs[i] + "";
|
|
|
|
|
final String value = pairs[i + 1] + "";
|
2015-07-17 20:48:13 +10:00
|
|
|
|
int start = sb.indexOf(key, 0);
|
2015-09-13 14:04:31 +10:00
|
|
|
|
while (start > -1) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final int end = start + key.length();
|
|
|
|
|
final int nextSearchStart = start + value.length();
|
2015-07-17 20:48:13 +10:00
|
|
|
|
sb.replace(start, end, value);
|
|
|
|
|
start = sb.indexOf(key, nextSearchStart);
|
|
|
|
|
}
|
2015-07-17 00:44:27 +10:00
|
|
|
|
}
|
2015-07-17 20:48:13 +10:00
|
|
|
|
return sb.toString();
|
2015-07-17 00:44:27 +10:00
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static boolean isAlphanumeric(final String str) {
|
|
|
|
|
for (int i = 0; i < str.length(); i++) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final char c = str.charAt(i);
|
2015-09-13 14:04:31 +10:00
|
|
|
|
if ((c < 0x30) || ((c >= 0x3a) && (c <= 0x40)) || ((c > 0x5a) && (c <= 0x60)) || (c > 0x7a)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-07-27 04:38:08 +10:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static boolean isAlphanumericUnd(final String str) {
|
|
|
|
|
for (int i = 0; i < str.length(); i++) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final char c = str.charAt(i);
|
2015-09-13 14:04:31 +10:00
|
|
|
|
if ((c < 0x30) || ((c >= 0x3a) && (c <= 0x40)) || ((c > 0x5a) && (c <= 0x60)) || (c > 0x7a) || (c == '_')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-08-26 14:21:48 +10:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static boolean isAlpha(final String str) {
|
|
|
|
|
for (int i = 0; i < str.length(); i++) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final char c = str.charAt(i);
|
2015-09-13 14:04:31 +10:00
|
|
|
|
if ((c <= 0x40) || ((c > 0x5a) && (c <= 0x60)) || (c > 0x7a)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-07-27 04:38:08 +10:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static String join(final Collection<?> collection, final String delimiter) {
|
2015-07-27 04:38:08 +10:00
|
|
|
|
return join(collection.toArray(), delimiter);
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static String joinOrdered(final Collection<?> collection, final String delimiter) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final Object[] array = collection.toArray();
|
2015-09-13 14:04:31 +10:00
|
|
|
|
Arrays.sort(array, new Comparator<Object>() {
|
2015-08-07 02:05:15 +10:00
|
|
|
|
@Override
|
2015-09-13 14:04:31 +10:00
|
|
|
|
public int compare(final Object a, final Object b) {
|
2015-08-07 02:05:15 +10:00
|
|
|
|
return a.hashCode() - b.hashCode();
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
2015-08-07 02:05:15 +10:00
|
|
|
|
});
|
|
|
|
|
return join(array, delimiter);
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static String join(final Collection<?> collection, final char delimiter) {
|
2015-07-27 04:38:08 +10:00
|
|
|
|
return join(collection.toArray(), delimiter + "");
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static boolean isAsciiPrintable(final char c) {
|
2015-07-27 04:38:08 +10:00
|
|
|
|
return (c >= ' ') && (c < '');
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static boolean isAsciiPrintable(final String s) {
|
|
|
|
|
for (final char c : s.toCharArray()) {
|
|
|
|
|
if (!isAsciiPrintable(c)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-07-27 04:38:08 +10:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static int getLevenshteinDistance(String s, String t) {
|
2015-07-27 04:38:08 +10:00
|
|
|
|
int n = s.length();
|
|
|
|
|
int m = t.length();
|
2015-09-13 14:04:31 +10:00
|
|
|
|
if (n == 0) {
|
2015-07-27 04:38:08 +10:00
|
|
|
|
return m;
|
2015-09-13 14:04:31 +10:00
|
|
|
|
} else if (m == 0) {
|
|
|
|
|
return n;
|
2015-07-27 04:38:08 +10:00
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
if (n > m) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final String tmp = s;
|
2015-07-27 04:38:08 +10:00
|
|
|
|
s = t;
|
|
|
|
|
t = tmp;
|
|
|
|
|
n = m;
|
|
|
|
|
m = t.length();
|
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
|
int p[] = new int[n + 1];
|
|
|
|
|
int d[] = new int[n + 1];
|
2015-07-27 04:38:08 +10:00
|
|
|
|
int _d[];
|
|
|
|
|
int i;
|
|
|
|
|
int j;
|
|
|
|
|
char t_j;
|
|
|
|
|
int cost;
|
2015-09-13 14:04:31 +10:00
|
|
|
|
for (i = 0; i <= n; i++) {
|
2015-07-27 04:38:08 +10:00
|
|
|
|
p[i] = i;
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
for (j = 1; j <= m; j++) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
t_j = t.charAt(j - 1);
|
2015-07-27 04:38:08 +10:00
|
|
|
|
d[0] = j;
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
for (i = 1; i <= n; i++) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
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);
|
2015-07-27 04:38:08 +10:00
|
|
|
|
}
|
|
|
|
|
_d = p;
|
|
|
|
|
p = d;
|
|
|
|
|
d = _d;
|
|
|
|
|
}
|
|
|
|
|
return p[n];
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static String join(final Object[] array, final String delimiter) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final StringBuilder result = new StringBuilder();
|
2015-09-13 14:04:31 +10:00
|
|
|
|
for (int i = 0, j = array.length; i < j; i++) {
|
|
|
|
|
if (i > 0) {
|
2015-07-27 04:38:08 +10:00
|
|
|
|
result.append(delimiter);
|
|
|
|
|
}
|
|
|
|
|
result.append(array[i]);
|
|
|
|
|
}
|
|
|
|
|
return result.toString();
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static String join(final int[] array, final String delimiter) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
final Integer[] wrapped = new Integer[array.length];
|
2015-09-13 14:04:31 +10:00
|
|
|
|
for (int i = 0; i < array.length; i++) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
wrapped[i] = array[i];
|
|
|
|
|
}
|
2015-07-27 23:10:14 +10:00
|
|
|
|
return join(wrapped, delimiter);
|
2015-07-27 16:20:24 +10:00
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static boolean isEqualToAny(final String a, final String... args) {
|
|
|
|
|
for (final String arg : args) {
|
|
|
|
|
if (StringMan.isEqual(a, arg)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-07-27 23:10:14 +10:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static boolean isEqualIgnoreCaseToAny(final String a, final String... args) {
|
|
|
|
|
for (final String arg : args) {
|
|
|
|
|
if (StringMan.isEqualIgnoreCase(a, arg)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-07-27 23:10:14 +10:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static boolean isEqual(final String a, final String b) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
return ((a == b) || ((a != null) && (b != null) && (a.length() == b.length()) && (a.hashCode() == b.hashCode()) && a.equals(b)));
|
2015-07-22 03:08:51 +10:00
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static boolean isEqualIgnoreCase(final String a, final String b) {
|
2015-09-11 20:09:22 +10:00
|
|
|
|
return ((a == b) || ((a != null) && (b != null) && (a.length() == b.length()) && a.equalsIgnoreCase(b)));
|
2015-07-27 23:10:14 +10:00
|
|
|
|
}
|
2015-09-13 14:04:31 +10:00
|
|
|
|
|
|
|
|
|
public static String repeat(final String s, final int n) {
|
2015-07-27 04:38:08 +10:00
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
2015-09-13 14:04:31 +10:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2015-07-27 04:38:08 +10:00
|
|
|
|
sb.append(s);
|
|
|
|
|
}
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
2015-07-06 01:44:10 +10:00
|
|
|
|
}
|