This commit is contained in:
Alexey Kudravtsev
2012-04-16 13:54:23 +04:00
parent c03471cc8c
commit 26299b6e46
@@ -44,6 +44,7 @@ public class StringUtil extends StringUtilRt {
@NonNls private static final Pattern EOL_SPLIT_PATTERN = Pattern.compile(" *(\r|\n|\r\n)+ *");
public static final NotNullFunction<String, String> QUOTER = new NotNullFunction<String, String>() {
@Override
@NotNull
public String fun(String s) {
return "\"" + s + "\"";
@@ -51,18 +52,20 @@ public class StringUtil extends StringUtilRt {
};
public static final NotNullFunction<String, String> SINGLE_QUOTER = new NotNullFunction<String, String>() {
@Override
@NotNull
public String fun(String s) {
return "'" + s + "'";
}
};
public static List<String> getWordsInStringLongestFirst(String find) {
@NotNull
public static List<String> getWordsInStringLongestFirst(@NotNull String find) {
List<String> words = getWordsIn(find);
// hope long words are rare
Collections.sort(words, new Comparator<String>() {
@Override
public int compare(final String o1, final String o2) {
public int compare(@NotNull final String o1, @NotNull final String o2) {
return o2.length() - o1.length();
}
});
@@ -70,31 +73,34 @@ public class StringUtil extends StringUtilRt {
}
@NotNull
public static String escapePattern(final @NotNull String text) {
public static String escapePattern(@NotNull final String text) {
return replace(replace(text, "'", "''"), "{", "'{'");
}
@NotNull
public static <T> Function<T, String> createToStringFunction(Class<T> cls) {
return new Function<T, String>() {
@Override
public String fun(T o) {
public String fun(@NotNull T o) {
return o.toString();
}
};
}
public static Function<String, String> TRIMMER = new Function<String, String>() {
@NotNull public static Function<String, String> TRIMMER = new Function<String, String>() {
@Nullable
@Override
public String fun(String s) {
public String fun(@Nullable String s) {
return s == null ? null : s.trim();
}
};
@Nullable
public static String replace(@NonNls @NotNull String text, @NonNls @NotNull String oldS, @NonNls @Nullable String newS) {
return replace(text, oldS, newS, false);
}
@Nullable
public static String replaceIgnoreCase(@NotNull String text, @NotNull String oldS, @Nullable String newS) {
return replace(text, oldS, newS, true);
}
@@ -128,6 +134,7 @@ public class StringUtil extends StringUtilRt {
return newBuffer == null ? buffer : newBuffer.toString();
}
@Nullable
public static String replace(@NotNull final String text, @NotNull final String oldS, @Nullable final String newS, boolean ignoreCase) {
if (text.length() < oldS.length()) return text;
@@ -222,6 +229,7 @@ public class StringUtil extends StringUtilRt {
return html.replaceAll("<(.|\n)*?>", "");
}
@Nullable
public static String toLowerCase(@Nullable final String str) {
//noinspection ConstantConditions
return str == null ? null : str.toLowerCase();
@@ -411,11 +419,12 @@ public class StringUtil extends StringUtilRt {
return false;
}
@NotNull
public static NotNullFunction<String, String> escaper(final boolean escapeSlash, @Nullable final String additionalChars) {
return new NotNullFunction<String, String>() {
@NotNull
@Override
public String fun(String dom) {
public String fun(@NotNull String dom) {
final StringBuilder builder = StringBuilderSpinAllocator.alloc();
try {
StringUtil.escapeStringCharacters(dom.length(), dom, additionalChars, escapeSlash, builder);
@@ -523,7 +532,8 @@ public class StringUtil extends StringUtilRt {
/**
* This is just an optimized version of Matcher.quoteReplacement
*/
public static String quoteReplacement(String s) {
@NotNull
public static String quoteReplacement(@NotNull String s) {
boolean needReplacements = false;
for (int i = 0; i < s.length(); i++) {
@@ -690,7 +700,7 @@ public class StringUtil extends StringUtilRt {
@NotNull
public static String capitalize(@NotNull String s) {
if (s.length() == 0) return s;
if (s.isEmpty()) return s;
if (s.length() == 1) return StringUtilRt.toUpperCase(s);
// Optimization
@@ -699,7 +709,7 @@ public class StringUtil extends StringUtilRt {
}
public static boolean isCapitalized(@Nullable String s) {
return s != null && s.length() > 0 && Character.isUpperCase(s.charAt(0));
return s != null && !s.isEmpty() && Character.isUpperCase(s.charAt(0));
}
@NotNull
@@ -710,7 +720,7 @@ public class StringUtil extends StringUtilRt {
return capitalize(s);
}
public static int stringHashCode(CharSequence chars) {
public static int stringHashCode(@NotNull CharSequence chars) {
if (chars instanceof String) return chars.hashCode();
if (chars instanceof CharSequenceWithStringHash) return chars.hashCode();
if (chars instanceof CharArrayCharSequence) return chars.hashCode();
@@ -718,7 +728,7 @@ public class StringUtil extends StringUtilRt {
return stringHashCode(chars, 0, chars.length());
}
public static int stringHashCode(CharSequence chars, int from, int to) {
public static int stringHashCode(@NotNull CharSequence chars, int from, int to) {
int h = 0;
for (int off = from; off < to; off++) {
h = 31 * h + chars.charAt(off);
@@ -734,7 +744,7 @@ public class StringUtil extends StringUtilRt {
return h;
}
public static int stringHashCodeInsensitive(char[] chars, int from, int to) {
public static int stringHashCodeInsensitive(@NotNull char[] chars, int from, int to) {
int h = 0;
for (int off = from; off < to; off++) {
h = 31 * h + toLowerCase(chars[off]);
@@ -742,7 +752,7 @@ public class StringUtil extends StringUtilRt {
return h;
}
public static int stringHashCodeInsensitive(CharSequence chars, int from, int to) {
public static int stringHashCodeInsensitive(@NotNull CharSequence chars, int from, int to) {
int h = 0;
for (int off = from; off < to; off++) {
h = 31 * h + toLowerCase(chars.charAt(off));
@@ -762,7 +772,7 @@ public class StringUtil extends StringUtilRt {
* @param secondPrefix
* @return
*/
public static boolean startsWithConcatenationOf(String testee, String firstPrefix, String secondPrefix) {
public static boolean startsWithConcatenationOf(@NotNull String testee, @NotNull String firstPrefix, @NotNull String secondPrefix) {
int l1 = firstPrefix.length();
int l2 = secondPrefix.length();
if (testee.length() < l1 + l2) return false;
@@ -777,7 +787,8 @@ public class StringUtil extends StringUtilRt {
return s;
}
public static String trimLog(final String text, final int limit) {
@NotNull
public static String trimLog(@NotNull final String text, final int limit) {
if (limit > 5 && text.length() > limit) {
return text.substring(0, limit - 5) + " ...\n";
}
@@ -833,11 +844,11 @@ public class StringUtil extends StringUtilRt {
}
public static boolean isNotEmpty(@Nullable String s) {
return s != null && s.length() > 0;
return s != null && !s.isEmpty();
}
public static boolean isEmpty(@Nullable String s) {
return s == null || s.length() == 0;
return s == null || s.isEmpty();
}
public static boolean isEmpty(@Nullable CharSequence cs) {
@@ -854,17 +865,17 @@ public class StringUtil extends StringUtilRt {
}
@NotNull
public static String notNullize(final String s, @NotNull String defaultValue) {
public static String notNullize(@Nullable final String s, @NotNull String defaultValue) {
return s == null ? defaultValue : s;
}
@Nullable
public static String nullize(final @Nullable String s) {
public static String nullize(@Nullable final String s) {
return nullize(s, false);
}
@Nullable
public static String nullize(final @Nullable String s, boolean nullizeSpaces) {
public static String nullize(@Nullable final String s, boolean nullizeSpaces) {
if (nullizeSpaces) {
if (isEmpty(s)) return null;
}
@@ -875,7 +886,7 @@ public class StringUtil extends StringUtilRt {
}
public static boolean isEmptyOrSpaces(@Nullable final String s) {
return s == null || s.trim().length() == 0;
return s == null || s.trim().isEmpty();
}
/**
@@ -960,7 +971,7 @@ public class StringUtil extends StringUtilRt {
@NotNull
public static List<String> split(@NotNull String s, @NotNull String separator,
boolean excludeSeparator, boolean excludeEmptyStrings) {
if (separator.length() == 0) {
if (separator.isEmpty()) {
return Collections.singletonList(s);
}
List<String> result = new ArrayList<String>();
@@ -970,7 +981,7 @@ public class StringUtil extends StringUtilRt {
if (index == -1) break;
final int nextPos = index + separator.length();
String token = s.substring(pos, excludeSeparator ? index : nextPos);
if (token.length() != 0 || !excludeEmptyStrings) {
if (!token.isEmpty() || !excludeEmptyStrings) {
result.add(token);
}
pos = nextPos;
@@ -985,16 +996,21 @@ public class StringUtil extends StringUtilRt {
public static Iterable<String> tokenize(@NotNull String s, @NotNull String separators) {
final com.intellij.util.text.StringTokenizer tokenizer = new com.intellij.util.text.StringTokenizer(s, separators);
return new Iterable<String>() {
@NotNull
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
@Override
public boolean hasNext() {
return tokenizer.hasMoreTokens();
}
@Override
public String next() {
return tokenizer.nextToken();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@@ -1004,18 +1020,23 @@ public class StringUtil extends StringUtilRt {
}
@NotNull
public static Iterable<String> tokenize(final StringTokenizer tokenizer) {
public static Iterable<String> tokenize(@NotNull final StringTokenizer tokenizer) {
return new Iterable<String>() {
@NotNull
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
@Override
public boolean hasNext() {
return tokenizer.hasMoreTokens();
}
@Override
public String next() {
return tokenizer.nextToken();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@@ -1119,7 +1140,7 @@ public class StringUtil extends StringUtilRt {
final StringBuilder result = new StringBuilder();
for (T item : items) {
String string = f.fun(item);
if (string != null && string.length() != 0) {
if (string != null && !string.isEmpty()) {
if (result.length() != 0) result.append(separator);
result.append(string);
}
@@ -1131,7 +1152,7 @@ public class StringUtil extends StringUtilRt {
public static String join(@NotNull Collection<? extends String> strings, @NotNull final String separator) {
final StringBuilder result = new StringBuilder();
for (String string : strings) {
if (string != null && string.length() != 0) {
if (string != null && !string.isEmpty()) {
if (result.length() != 0) result.append(separator);
result.append(string);
}
@@ -1150,7 +1171,7 @@ public class StringUtil extends StringUtilRt {
}
@NotNull
public static String join(final String... strings) {
public static String join(@Nullable final String... strings) {
if (strings == null || strings.length == 0) return "";
final StringBuilder builder = StringBuilderSpinAllocator.alloc();
@@ -1254,6 +1275,7 @@ public class StringUtil extends StringUtilRt {
return null;
}
@Nullable
private static String stripEnding(@NotNull String name, @NotNull String ending) {
if (name.endsWith(ending)) {
if (name.equals(ending)) return name; // do not return empty string
@@ -1277,13 +1299,13 @@ public class StringUtil extends StringUtilRt {
return false;
}
public static boolean containsChar(final String value, final char ch) {
public static boolean containsChar(@NotNull final String value, final char ch) {
return value.indexOf(ch) >= 0;
}
@Nullable
public static String firstLetterToUpperCase(@Nullable final String displayString) {
if (displayString == null || displayString.length() == 0) return displayString;
if (displayString == null || displayString.isEmpty()) return displayString;
char firstChar = displayString.charAt(0);
char uppedFirstChar = toUpperCase(firstChar);
@@ -1313,11 +1335,13 @@ public class StringUtil extends StringUtilRt {
return result.toString();
}
public static List<String> findMatches(String s, Pattern pattern) {
@NotNull
public static List<String> findMatches(@NotNull String s, @NotNull Pattern pattern) {
return findMatches(s, pattern, 1);
}
public static List<String> findMatches(String s, Pattern pattern, int groupIndex) {
@NotNull
public static List<String> findMatches(@NotNull String s, @NotNull Pattern pattern, int groupIndex) {
List<String> result = new SmartList<String>();
Matcher m = pattern.matcher(s);
while (m.find()) {
@@ -1435,7 +1459,7 @@ public class StringUtil extends StringUtilRt {
* @return <code>true</code> if given symbol is contained at the target range of the given char sequence;
* <code>false</code> otherwise
*/
public static boolean contains(CharSequence s, int start, int end, char c) {
public static boolean contains(@NotNull CharSequence s, int start, int end, char c) {
return indexOf(s, c, start, end) >= 0;
}
@@ -1476,17 +1500,18 @@ public class StringUtil extends StringUtilRt {
return i + subString.length();
}
public static int indexOfAny(final String s, final String chars) {
public static int indexOfAny(@NotNull final String s, @NotNull final String chars) {
return indexOfAny(s, chars, 0, s.length());
}
public static int indexOfAny(final String s, final String chars, final int start, final int end) {
public static int indexOfAny(@NotNull final String s, @NotNull final String chars, final int start, final int end) {
for (int i = start; i < end; i++) {
if (containsChar(chars, s.charAt(i))) return i;
}
return -1;
}
@Nullable
public static String substringAfter(@NotNull String text, @NotNull String subString) {
int i = text.indexOf(subString);
if (i == -1) return null;
@@ -1578,7 +1603,7 @@ public class StringUtil extends StringUtilRt {
}
}
private static void unescapeSlashes(StringBuilder buf, String str) {
private static void unescapeSlashes(@NotNull StringBuilder buf, @NotNull String str) {
final int length = str.length();
final int last = length - 1;
for (int i = 0; i < length; i++) {
@@ -1605,12 +1630,14 @@ public class StringUtil extends StringUtilRt {
@NonNls private static final String[] REPLACES_REFS = {"&lt;", "&gt;", "&amp;", "&#39;", "&quot;"};
@NonNls private static final String[] REPLACES_DISP = {"<", ">", "&", "'", "\""};
public static String unescapeXml(final String text) {
@Nullable
public static String unescapeXml(@Nullable final String text) {
if (text == null) return null;
return replace(text, REPLACES_REFS, REPLACES_DISP);
}
public static String escapeXml(final String text) {
@Nullable
public static String escapeXml(@Nullable final String text) {
if (text == null) return null;
return replace(text, REPLACES_DISP, REPLACES_REFS);
}
@@ -1659,14 +1686,14 @@ public class StringUtil extends StringUtilRt {
public static String[] filterEmptyStrings(@NotNull String[] strings) {
int emptyCount = 0;
for (String string : strings) {
if (string == null || string.length() == 0) emptyCount++;
if (string == null || string.isEmpty()) emptyCount++;
}
if (emptyCount == 0) return strings;
String[] result = ArrayUtil.newStringArray(strings.length - emptyCount);
int count = 0;
for (String string : strings) {
if (string == null || string.length() == 0) continue;
if (string == null || string.isEmpty()) continue;
result[count++] = string;
}
@@ -1715,6 +1742,7 @@ public class StringUtil extends StringUtilRt {
return r.toString();
}
@Nullable
public static String getPropertyName(@NonNls @NotNull String methodName) {
if (methodName.startsWith("get")) {
return Introspector.decapitalize(methodName.substring(3));
@@ -1829,8 +1857,8 @@ public class StringUtil extends StringUtilRt {
return escaped.toString();
}
public static String getQualifiedName(String packageName, String className) {
if (packageName == null || packageName.length() == 0) {
public static String getQualifiedName(@Nullable String packageName, String className) {
if (packageName == null || packageName.isEmpty()) {
return className;
}
return packageName + '.' + className;
@@ -1899,7 +1927,8 @@ public class StringUtil extends StringUtilRt {
return "a" + Character.toUpperCase(c) + name.substring(1);
}
public static String sanitizeJavaIdentifier(String name) {
@NotNull
public static String sanitizeJavaIdentifier(@NotNull String name) {
final StringBuilder result = new StringBuilder();
for (int i = 0; i < name.length(); i++) {
@@ -1983,11 +2012,11 @@ public class StringUtil extends StringUtilRt {
* Implementation of "Sorting for Humans: Natural Sort Order":
* http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html
*/
public static int naturalCompare(String string1, String string2) {
public static int naturalCompare(@NotNull String string1, @NotNull String string2) {
return naturalCompare(string1, string2, false);
}
private static int naturalCompare(String string1, String string2, boolean caseSensitive) {
private static int naturalCompare(@NotNull String string1, @NotNull String string2, boolean caseSensitive) {
final int string1Length = string1.length();
final int string2Length = string2.length();
for (int i = 0, j = 0; i < string1Length && j < string2Length; i++, j++) {
@@ -2058,7 +2087,7 @@ public class StringUtil extends StringUtilRt {
}
private static boolean isDigit(char c) {
return (c >= '0' && c <= '9');
return c >= '0' && c <= '9';
}
public static int compare(@Nullable String s1, @Nullable String s2, boolean ignoreCase) {
@@ -2143,7 +2172,7 @@ public class StringUtil extends StringUtilRt {
}
@NotNull
public static String shortenTextWithEllipsis(final String text, final int max_length, final int suffix_length) {
public static String shortenTextWithEllipsis(@NotNull final String text, final int max_length, final int suffix_length) {
final int prefix_length = max_length - suffix_length - 3;
assert prefix_length > 0;
final StringBuilder buffer = new StringBuilder();