提交 027787d0 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Use String.isEmpty() in some places

上级 46b7cd0f
......@@ -74,7 +74,7 @@ public class CreateUser extends DefineCommand {
char[] passwordChars = pwd == null ? new char[0] : pwd.toCharArray();
byte[] userPasswordHash;
String userName = user.getName();
if (userName.length() == 0 && passwordChars.length == 0) {
if (userName.isEmpty() && passwordChars.length == 0) {
userPasswordHash = new byte[0];
} else {
userPasswordHash = SHA256.getKeyPasswordHash(userName, passwordChars);
......
......@@ -252,7 +252,7 @@ public class ConnectionInfo implements Cloneable {
url = url.substring(0, idx);
String[] list = StringUtils.arraySplit(settings, ';', false);
for (String setting : list) {
if (setting.length() == 0) {
if (setting.isEmpty()) {
continue;
}
int equal = setting.indexOf('=');
......@@ -326,7 +326,7 @@ public class ConnectionInfo implements Cloneable {
if (passwordHash) {
return StringUtils.convertHexToBytes(new String(password));
}
if (userName.length() == 0 && password.length == 0) {
if (userName.isEmpty() && password.length == 0) {
return new byte[0];
}
return SHA256.getKeyPasswordHash(userName, password);
......@@ -643,7 +643,7 @@ public class ConnectionInfo implements Cloneable {
private static String remapURL(String url) {
String urlMap = SysProperties.URL_MAP;
if (urlMap != null && urlMap.length() > 0) {
if (urlMap != null && !urlMap.isEmpty()) {
try {
SortedProperties prop;
prop = SortedProperties.loadProperties(urlMap);
......@@ -653,7 +653,7 @@ public class ConnectionInfo implements Cloneable {
prop.store(urlMap);
} else {
url2 = url2.trim();
if (url2.length() > 0) {
if (!url2.isEmpty()) {
return url2;
}
}
......
......@@ -642,7 +642,7 @@ public class Database implements DataHandler {
n = tokenizer.nextToken();
}
}
if (n == null || n.length() == 0) {
if (n == null || n.isEmpty()) {
n = "unnamed";
}
return dbSettings.databaseToUpper ? StringUtils.toUpperEnglish(n) : n;
......@@ -2274,7 +2274,7 @@ public class Database implements DataHandler {
}
public void setEventListenerClass(String className) {
if (className == null || className.length() == 0) {
if (className == null || className.isEmpty()) {
eventListener = null;
} else {
try {
......
......@@ -72,7 +72,7 @@ public class CompareLike extends Condition {
}
private static Character getEscapeChar(String s) {
return s == null || s.length() == 0 ? null : s.charAt(0);
return s == null || s.isEmpty() ? null : s.charAt(0);
}
@Override
......
......@@ -681,7 +681,7 @@ public class Function extends Expression implements FunctionCall {
// string
case ASCII: {
String s = v0.getString();
if (s.length() == 0) {
if (s.isEmpty()) {
result = ValueNull.INSTANCE;
} else {
result = ValueInt.get(s.charAt(0));
......@@ -2710,19 +2710,17 @@ public class Function extends Expression implements FunctionCall {
String fieldDelimiter, String escapeCharacter) {
if (fieldSeparator != null) {
csv.setFieldSeparatorWrite(fieldSeparator);
if (fieldSeparator.length() > 0) {
if (!fieldSeparator.isEmpty()) {
char fs = fieldSeparator.charAt(0);
csv.setFieldSeparatorRead(fs);
}
}
if (fieldDelimiter != null) {
char fd = fieldDelimiter.length() == 0 ?
0 : fieldDelimiter.charAt(0);
char fd = fieldDelimiter.isEmpty() ? 0 : fieldDelimiter.charAt(0);
csv.setFieldDelimiter(fd);
}
if (escapeCharacter != null) {
char ec = escapeCharacter.length() == 0 ?
0 : escapeCharacter.charAt(0);
char ec = escapeCharacter.isEmpty() ? 0 : escapeCharacter.charAt(0);
csv.setEscapeCharacter(ec);
}
}
......
......@@ -3093,14 +3093,14 @@ public class JdbcDatabaseMetaData extends TraceObject implements
}
private static String getSchemaPattern(String pattern) {
return pattern == null ? "%" : pattern.length() == 0 ?
return pattern == null ? "%" : pattern.isEmpty() ?
Constants.SCHEMA_MAIN : pattern;
}
private static String getCatalogPattern(String catalogPattern) {
// Workaround for OpenOffice: getColumns is called with "" as the
// catalog
return catalogPattern == null || catalogPattern.length() == 0 ?
return catalogPattern == null || catalogPattern.isEmpty() ?
"%" : catalogPattern;
}
......
......@@ -180,7 +180,7 @@ public class PgServerThread implements Runnable {
" (" + (version >> 16) + "." + (version & 0xff) + ")");
while (true) {
String param = readString();
if (param.length() == 0) {
if (param.isEmpty()) {
break;
}
String value = readString();
......
......@@ -240,14 +240,15 @@ public class PageParser {
if (s == null) {
return null;
}
int length = s.length();
if (convertBreakAndSpace) {
if (s.length() == 0) {
if (length == 0) {
return " ";
}
}
StringBuilder buff = new StringBuilder(s.length());
StringBuilder buff = new StringBuilder(length);
boolean convertSpace = true;
for (int i = 0; i < s.length(); i++) {
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
if (c == ' ' || c == '\t') {
// convert tabs into spaces
......@@ -312,11 +313,12 @@ public class PageParser {
if (s == null) {
return null;
}
if (s.length() == 0) {
int length = s.length();
if (length == 0) {
return "";
}
StringBuilder buff = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
StringBuilder buff = new StringBuilder(length);
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
switch (c) {
case '"':
......
......@@ -167,7 +167,7 @@ class WebSession {
return;
}
sql = sql.trim();
if (sql.length() == 0) {
if (sql.isEmpty()) {
return;
}
if (commandHistory.size() > MAX_HISTORY) {
......
......@@ -51,7 +51,7 @@ public class FilePathZip extends FilePath {
public boolean exists() {
try {
String entryName = getEntryName();
if (entryName.length() == 0) {
if (entryName.isEmpty()) {
return true;
}
try (ZipFile file = openZipFile()) {
......@@ -88,7 +88,7 @@ public class FilePathZip extends FilePath {
public boolean isDirectory() {
try {
String entryName = getEntryName();
if (entryName.length() == 0) {
if (entryName.isEmpty()) {
return true;
}
try (ZipFile file = openZipFile()) {
......
......@@ -108,7 +108,7 @@ public class Backup extends Tool {
private void process(String zipFileName, String directory, String db,
boolean quiet) throws SQLException {
List<String> list;
boolean allFiles = db != null && db.length() == 0;
boolean allFiles = db != null && db.isEmpty();
if (allFiles) {
list = FileUtils.newDirectoryStream(directory);
} else {
......
......@@ -228,7 +228,7 @@ public class Csv implements SimpleRowSource {
for (int i = 0; i < columnNames.length; i++) {
StringBuilder buff = new StringBuilder();
String n = columnNames[i];
if (n == null || n.length() == 0) {
if (n == null || n.isEmpty()) {
buff.append('C').append(i + 1);
} else {
buff.append(n);
......@@ -350,7 +350,7 @@ public class Csv implements SimpleRowSource {
list.add(v);
}
} else {
if (v.length() == 0) {
if (v.isEmpty()) {
v = "COLUMN" + list.size();
} else if (!caseSensitiveColumnNames && isSimpleColumnName(v)) {
v = StringUtils.toUpperEnglish(v);
......@@ -827,13 +827,13 @@ public class Csv implements SimpleRowSource {
String charset = null;
String[] keyValuePairs = StringUtils.arraySplit(options, ' ', false);
for (String pair : keyValuePairs) {
if (pair.length() == 0) {
if (pair.isEmpty()) {
continue;
}
int index = pair.indexOf('=');
String key = StringUtils.trim(pair.substring(0, index), true, true, " ");
String value = pair.substring(index + 1);
char ch = value.length() == 0 ? 0 : value.charAt(0);
char ch = value.isEmpty() ? 0 : value.charAt(0);
if (isParam(key, "escape", "esc", "escapeCharacter")) {
setEscapeCharacter(ch);
} else if (isParam(key, "fieldDelimiter", "fieldDelim")) {
......
......@@ -205,7 +205,7 @@ public class RunScript extends Tool {
break;
}
String trim = sql.trim();
if (trim.length() == 0) {
if (trim.isEmpty()) {
continue;
}
if (trim.startsWith("@") && StringUtils.toUpperEnglish(trim).
......
......@@ -220,7 +220,7 @@ public class Shell extends Tool implements Runnable {
break;
}
String trimmed = line.trim();
if (trimmed.length() == 0) {
if (trimmed.isEmpty()) {
continue;
}
boolean end = trimmed.endsWith(";");
......@@ -366,7 +366,7 @@ public class Shell extends Tool implements Runnable {
println("[Enter] Hide");
print("Password ");
String password = readLine();
if (password.length() == 0) {
if (password.isEmpty()) {
password = readPassword();
}
conn = JdbcUtils.getConnection(driver, url, user, password);
......@@ -433,7 +433,7 @@ public class Shell extends Tool implements Runnable {
private String readLine(String defaultValue) throws IOException {
String s = readLine();
return s.length() == 0 ? defaultValue : s;
return s.isEmpty() ? defaultValue : s;
}
private String readLine() throws IOException {
......
......@@ -155,7 +155,7 @@ public class NetUtils {
*/
private static InetAddress getBindAddress() throws UnknownHostException {
String host = SysProperties.BIND_ADDRESS;
if (host == null || host.length() == 0) {
if (host == null || host.isEmpty()) {
return null;
}
synchronized (NetUtils.class) {
......
......@@ -238,10 +238,11 @@ public class ParserUtil {
* @return true if it is a keyword
*/
public static boolean isKeyword(String s) {
if (s.length() == 0) {
int length = s.length();
if (length == 0) {
return false;
}
return getSaveTokenType(s, false, 0, s.length(), false) != IDENTIFIER;
return getSaveTokenType(s, false, 0, length, false) != IDENTIFIER;
}
/**
......@@ -252,7 +253,8 @@ public class ParserUtil {
* @throws NullPointerException if s is {@code null}
*/
public static boolean isSimpleIdentifier(String s) {
if (s.length() == 0) {
int length = s.length();
if (length == 0) {
return false;
}
char c = s.charAt(0);
......@@ -260,13 +262,13 @@ public class ParserUtil {
if ((UPPER_OR_OTHER_LETTER >>> Character.getType(c) & 1) == 0 && c != '_') {
return false;
}
for (int i = 1, length = s.length(); i < length; i++) {
for (int i = 1; i < length; i++) {
c = s.charAt(i);
if ((UPPER_OR_OTHER_LETTER_OR_DIGIT >>> Character.getType(c) & 1) == 0 && c != '_') {
return false;
}
}
return getSaveTokenType(s, false, 0, s.length(), true) == IDENTIFIER;
return getSaveTokenType(s, false, 0, length, true) == IDENTIFIER;
}
/**
......
......@@ -777,7 +777,7 @@ public class StringUtils {
* @return true if it is null or empty
*/
public static boolean isNullOrEmpty(String s) {
return s == null || s.length() == 0;
return s == null || s.isEmpty();
}
/**
......@@ -810,7 +810,7 @@ public class StringUtils {
return string;
}
char paddingChar;
if (padding == null || padding.length() == 0) {
if (padding == null || padding.isEmpty()) {
paddingChar = ' ';
} else {
paddingChar = padding.charAt(0);
......@@ -941,7 +941,7 @@ public class StringUtils {
}
if (s == null) {
return s;
} else if (s.length() == 0) {
} else if (s.isEmpty()) {
return "";
}
int hash = s.hashCode();
......@@ -1151,7 +1151,7 @@ public class StringUtils {
* @return the escaped pattern
*/
public static String escapeMetaDataPattern(String pattern) {
if (pattern == null || pattern.length() == 0) {
if (pattern == null || pattern.isEmpty()) {
return pattern;
}
return replaceAll(pattern, "\\", "\\\\");
......
......@@ -362,13 +362,14 @@ public class ToChar {
}
int i = idx + 1;
boolean allZeroes = true;
for (; i < numberStr.length(); i++) {
int length = numberStr.length();
for (; i < length; i++) {
if (numberStr.charAt(i) != '0') {
allZeroes = false;
break;
}
}
final char[] zeroes = new char[allZeroes ? numberStr.length() - idx - 1: i - 1 - idx];
final char[] zeroes = new char[allZeroes ? length - idx - 1: i - 1 - idx];
Arrays.fill(zeroes, '0');
return String.valueOf(zeroes);
}
......@@ -688,7 +689,7 @@ public class ToChar {
StringBuilder output = new StringBuilder();
boolean fillMode = true;
for (int i = 0; i < format.length();) {
for (int i = 0, length = format.length(); i < length;) {
Capitalization cap;
......
......@@ -249,7 +249,7 @@ public class ToDateParser {
}
private boolean hasToParseData() {
return formatStr.length() > 0;
return !formatStr.isEmpty();
}
private void removeFirstChar() {
......
......@@ -651,7 +651,7 @@ class ToDateTokenizer {
* @return the list of tokens, or {@code null}
*/
static List<FormatTokenEnum> getTokensInQuestion(String formatStr) {
if (formatStr != null && formatStr.length() > 0) {
if (formatStr != null && !formatStr.isEmpty()) {
char key = Character.toUpperCase(formatStr.charAt(0));
if (key >= 'A' && key <= 'Y') {
List<FormatTokenEnum>[] tokens = TOKENS;
......
......@@ -222,12 +222,13 @@ public class CompareMode implements Comparator<Value> {
} else if (name.startsWith(CHARSET)) {
return new CharsetCollator(Charset.forName(name.substring(CHARSET.length())));
}
if (name.length() == 2) {
int length = name.length();
if (length == 2) {
Locale locale = new Locale(StringUtils.toLowerEnglish(name), "");
if (compareLocaleNames(locale, name)) {
result = Collator.getInstance(locale);
}
} else if (name.length() == 5) {
} else if (length == 5) {
// LL_CC (language_country)
int idx = name.indexOf('_');
if (idx >= 0) {
......
......@@ -49,12 +49,13 @@ public class CompareModeIcu4J extends CompareMode {
"com.ibm.icu.text.Collator");
Method getInstanceMethod = collatorClass.getMethod(
"getInstance", Locale.class);
if (name.length() == 2) {
int length = name.length();
if (length == 2) {
Locale locale = new Locale(StringUtils.toLowerEnglish(name), "");
if (compareLocaleNames(locale, name)) {
result = (Comparator<String>) getInstanceMethod.invoke(null, locale);
}
} else if (name.length() == 5) {
} else if (length == 5) {
// LL_CC (language_country)
int idx = name.indexOf('_');
if (idx >= 0) {
......
......@@ -52,7 +52,7 @@ public final class ExtTypeInfoEnum extends ExtTypeInfo {
}
result.append('\'');
String s = enumerators[i];
for (int j = 0; j < s.length(); j++) {
for (int j = 0, length = s.length(); j < length; j++) {
char c = s.charAt(j);
if (c == '\'') {
result.append('\'');
......
......@@ -275,7 +275,7 @@ public class ValueLob extends Value {
private static int getNewObjectId(DataHandler h) {
String path = h.getDatabasePath();
if ((path != null) && (path.length() == 0)) {
if (path != null && path.isEmpty()) {
path = new File(Utils.getProperty("java.io.tmpdir", "."),
SysProperties.PREFIX_TEMP_FILE).getAbsolutePath();
}
......
......@@ -176,7 +176,7 @@ public class ValueLobDb extends Value {
private static String createTempLobFileName(DataHandler handler)
throws IOException {
String path = handler.getDatabasePath();
if (path.length() == 0) {
if (path.isEmpty()) {
path = SysProperties.PREFIX_TEMP_FILE;
}
return FileUtils.createTempFile(path, Constants.SUFFIX_TEMP_FILE, true, true);
......
......@@ -47,13 +47,13 @@ public class ValueStringFixed extends ValueString {
}
private static String rightPadWithSpaces(String s, int length) {
int pad = length - s.length();
if (pad <= 0) {
int used = s.length();
if (length <= used) {
return s;
}
char[] res = new char[length];
s.getChars(0, s.length(), res, 0);
Arrays.fill(res, s.length(), length, ' ');
s.getChars(0, used, res, 0);
Arrays.fill(res, used, length, ' ');
return new String(res);
}
......@@ -109,11 +109,12 @@ public class ValueStringFixed extends ValueString {
// Default behaviour of H2
s = trimRight(s);
}
if (s.length() == 0) {
int length = s.length();
if (length == 0) {
return EMPTY;
}
ValueStringFixed obj = new ValueStringFixed(StringUtils.cache(s));
if (s.length() > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
if (length > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
return obj;
}
return (ValueStringFixed) Value.cache(obj);
......
......@@ -60,11 +60,12 @@ public class ValueStringIgnoreCase extends ValueString {
* @return the value
*/
public static ValueStringIgnoreCase get(String s) {
if (s.length() == 0) {
int length = s.length();
if (length == 0) {
return EMPTY;
}
ValueStringIgnoreCase obj = new ValueStringIgnoreCase(StringUtils.cache(s));
if (s.length() > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
if (length > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
return obj;
}
ValueStringIgnoreCase cache = (ValueStringIgnoreCase) Value.cache(obj);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论