Camelsplit for pre Java 8 compat

This commit is contained in:
Olof Larsson 2016-05-26 11:47:43 +02:00
parent 66df3fd086
commit 3259d3fa7d
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
4 changed files with 15 additions and 8 deletions

View File

@ -293,8 +293,8 @@ public class CommandEditAbstract<O, V> extends MassiveCommand
public String createCommandAlias() public String createCommandAlias()
{ {
// Split at uppercase letters // Split at uppercase letters
final String[] words = this.getClass().getSimpleName().split("(?=[A-Z])"); final List<String> words = Txt.camelsplit(this.getClass().getSimpleName());
String alias = words[words.length - 1]; String alias = words.get(words.size() - 1);
alias = alias.toLowerCase(); alias = alias.toLowerCase();
return alias; return alias;
} }

View File

@ -82,10 +82,8 @@ public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbst
public String createCommandAlias() public String createCommandAlias()
{ {
// Split at uppercase letters // Split at uppercase letters
String name = this.getClass().getSimpleName(); String alias = this.getClass().getSimpleName();
name = name.substring("CommandEditContainer".length()); alias = alias.substring("CommandEditContainer".length());
final String[] words = name.split("(?=[A-Z])");
String alias = Txt.implode(words, "");
alias = Txt.lowerCaseFirst(alias); alias = Txt.lowerCaseFirst(alias);
return alias; return alias;
} }

View File

@ -68,7 +68,7 @@ public abstract class TypeAbstract<T> implements Type<T>
name = name.substring(prefixLength); name = name.substring(prefixLength);
// We split at uppercase letters, because most class names are camel-case. // We split at uppercase letters, because most class names are camel-case.
final String[] words = Txt.PATTERN_UPPERCASE.split(name); final List<String> words = Txt.camelsplit(name);
return Txt.implode(words, " ").toLowerCase(); return Txt.implode(words, " ").toLowerCase();
} }

View File

@ -41,7 +41,7 @@ public class Txt
public static final Pattern PATTERN_WHITESPACE = Pattern.compile("\\s+"); public static final Pattern PATTERN_WHITESPACE = Pattern.compile("\\s+");
public static final Pattern PATTERN_NEWLINE = Pattern.compile("\\r?\\n"); public static final Pattern PATTERN_NEWLINE = Pattern.compile("\\r?\\n");
public static final Pattern PATTERN_UPPERCASE = Pattern.compile("(?=[A-Z])"); private static final Pattern PATTERN_UPPERCASE_ZEROWIDTH = Pattern.compile("(?=[A-Z])"); // NOTE: Use camelsplit instead for Java 6/7 compatibility.
public static final long millisPerSecond = 1000; public static final long millisPerSecond = 1000;
public static final long millisPerMinute = 60 * millisPerSecond; public static final long millisPerMinute = 60 * millisPerSecond;
@ -232,6 +232,15 @@ public class Txt
// Standard utils like UCFirst, implode and repeat. // Standard utils like UCFirst, implode and repeat.
// -------------------------------------------- // // -------------------------------------------- //
public static List<String> camelsplit(String string)
{
List<String> ret = Arrays.asList(PATTERN_UPPERCASE_ZEROWIDTH.split(string));
// In version before Java 8 zero width matches in the beginning created a leading empty string.
// We manually look for it and removes it to be compatible with Java 6 and 7.
if (ret.get(0).isEmpty()) ret = ret.subList(1, ret.size());
return ret;
}
public static String upperCaseFirst(String string) public static String upperCaseFirst(String string)
{ {
if (string == null) return null; if (string == null) return null;