|
@@ -9,6 +9,7 @@ package net.ranides.assira.text;
|
|
|
import java.text.MessageFormat;
|
|
import java.text.MessageFormat;
|
|
|
import java.util.Formatter;
|
|
import java.util.Formatter;
|
|
|
import java.util.Locale;
|
|
import java.util.Locale;
|
|
|
|
|
+import net.ranides.assira.annotations.PMD;
|
|
|
import net.ranides.assira.math.MathUtils;
|
|
import net.ranides.assira.math.MathUtils;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -19,7 +20,7 @@ import net.ranides.assira.math.MathUtils;
|
|
|
public final class Strings {
|
|
public final class Strings {
|
|
|
|
|
|
|
|
private Strings() { /* utility class */ }
|
|
private Strings() { /* utility class */ }
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static String sprintf(Locale locale, String format, Object... args) {
|
|
public static String sprintf(Locale locale, String format, Object... args) {
|
|
|
return new Formatter().format(locale, format, args).toString();
|
|
return new Formatter().format(locale, format, args).toString();
|
|
|
}
|
|
}
|
|
@@ -35,7 +36,7 @@ public final class Strings {
|
|
|
public static String format(String format, Object... args) {
|
|
public static String format(String format, Object... args) {
|
|
|
return format(Locale.ROOT, format, args);
|
|
return format(Locale.ROOT, format, args);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static String or(Object... values) {
|
|
public static String or(Object... values) {
|
|
|
for(Object value : values) {
|
|
for(Object value : values) {
|
|
|
if(null!=value) { return value.toString(); }
|
|
if(null!=value) { return value.toString(); }
|
|
@@ -80,7 +81,7 @@ public final class Strings {
|
|
|
public static boolean isAssigned(CharSequence value) {
|
|
public static boolean isAssigned(CharSequence value) {
|
|
|
return (null!=value) && value.length() != 0;
|
|
return (null!=value) && value.length() != 0;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static boolean isEqual(Object valueA, Object valueB) {
|
|
public static boolean isEqual(Object valueA, Object valueB) {
|
|
|
if(null == valueA) {
|
|
if(null == valueA) {
|
|
|
return (null == valueB);
|
|
return (null == valueB);
|
|
@@ -96,19 +97,96 @@ public final class Strings {
|
|
|
return valueA.compareTo(valueB);
|
|
return valueA.compareTo(valueB);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public static boolean contains(String text, char value) {
|
|
|
|
|
+ return text!=null && text.indexOf(value) != -1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static boolean contains(String text, CharSequence value) {
|
|
|
|
|
+ return text!=null && text.contains(value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static int length(String value) {
|
|
public static int length(String value) {
|
|
|
return (null == value) ? 0 : value.length();
|
|
return (null == value) ? 0 : value.length();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static String substring(String text, int first) {
|
|
public static String substring(String text, int first) {
|
|
|
if(null == text) { return ""; }
|
|
if(null == text) { return ""; }
|
|
|
return substring(text, first, text.length());
|
|
return substring(text, first, text.length());
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static String substring(String text, int first, int last) {
|
|
public static String substring(String text, int first, int last) {
|
|
|
if(null == text || first >= last || first>=text.length()) { return ""; }
|
|
if(null == text || first >= last || first>=text.length()) { return ""; }
|
|
|
return text.substring(first, MathUtils.clip(last, first, text.length()));
|
|
return text.substring(first, MathUtils.clip(last, first, text.length()));
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public static CharSequence asSequence(char[] data) {
|
|
|
|
|
+ return new ArrayWrapper(data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static final class SliceWrapper implements CharSequence {
|
|
|
|
|
+
|
|
|
|
|
+ private final char[] data;
|
|
|
|
|
+ private final int start;
|
|
|
|
|
+ private final int end;
|
|
|
|
|
+
|
|
|
|
|
+ public SliceWrapper(char[] data, int start, int end) {
|
|
|
|
|
+ this.data = data;
|
|
|
|
|
+ this.start = start;
|
|
|
|
|
+ this.end = end;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int length() {
|
|
|
|
|
+ return end - start;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public char charAt(int index) {
|
|
|
|
|
+ return data[index + start];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CharSequence subSequence(int start, int end) {
|
|
|
|
|
+ return new SliceWrapper(data, this.start+start, this.start+end);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String toString() {
|
|
|
|
|
+ return String.valueOf(data, start, length());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @PMD.ArrayWrapper
|
|
|
|
|
+ private static final class ArrayWrapper implements CharSequence {
|
|
|
|
|
+
|
|
|
|
|
+ private final char[] data;
|
|
|
|
|
+
|
|
|
|
|
+ public ArrayWrapper(char[] data) { // NOPMD
|
|
|
|
|
+ this.data = data;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int length() {
|
|
|
|
|
+ return data.length;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public char charAt(int index) {
|
|
|
|
|
+ return data[index];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CharSequence subSequence(int start, int end) {
|
|
|
|
|
+ return new SliceWrapper(data, start, end);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String toString() {
|
|
|
|
|
+ return String.valueOf(data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|