From 77c6026b1e9fc4057129a179c94cc395d8ca949d Mon Sep 17 00:00:00 2001 From: Markus Spann Date: Fri, 16 Feb 2024 09:02:30 +0100 Subject: [PATCH] Improve code style - Prefer isEmpty() over length == 0 --- .../github/spannm/jackcess/impl/CompoundOleUtil.java | 2 +- .../io/github/spannm/jackcess/impl/DatabaseImpl.java | 4 ++-- .../spannm/jackcess/impl/General97IndexCodes.java | 2 +- .../spannm/jackcess/impl/GeneralLegacyIndexCodes.java | 2 +- .../spannm/jackcess/impl/expr/DefaultFunctions.java | 6 +++--- .../spannm/jackcess/impl/expr/Expressionator.java | 4 ++-- .../github/spannm/jackcess/impl/expr/FormatUtil.java | 2 +- .../github/spannm/jackcess/impl/expr/StringValue.java | 2 +- .../io/github/spannm/jackcess/impl/IndexCodesTest.java | 10 +++++----- .../io/github/spannm/jackcess/impl/JetFormatTest.java | 2 +- 10 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/github/spannm/jackcess/impl/CompoundOleUtil.java b/src/main/java/io/github/spannm/jackcess/impl/CompoundOleUtil.java index debebeb..6e969ca 100644 --- a/src/main/java/io/github/spannm/jackcess/impl/CompoundOleUtil.java +++ b/src/main/java/io/github/spannm/jackcess/impl/CompoundOleUtil.java @@ -87,7 +87,7 @@ public static DocumentEntry getDocumentEntry(String entryName, // split entry name into individual components and decode them List entryNames = new ArrayList<>(); for (String str : entryName.split(ENTRY_SEPARATOR)) { - if (str.length() == 0) { + if (str.isEmpty()) { continue; } entryNames.add(decodeEntryName(str)); diff --git a/src/main/java/io/github/spannm/jackcess/impl/DatabaseImpl.java b/src/main/java/io/github/spannm/jackcess/impl/DatabaseImpl.java index b87ef10..004c9f7 100644 --- a/src/main/java/io/github/spannm/jackcess/impl/DatabaseImpl.java +++ b/src/main/java/io/github/spannm/jackcess/impl/DatabaseImpl.java @@ -1953,7 +1953,7 @@ public static TimeZone getDefaultTimeZone() { String tzProp = System.getProperty(TIMEZONE_PROPERTY); if (tzProp != null) { tzProp = tzProp.trim(); - if (tzProp.length() > 0) { + if (!tzProp.isEmpty()) { return TimeZone.getTimeZone(tzProp); } } @@ -1971,7 +1971,7 @@ public static Charset getDefaultCharset(JetFormat format) { String csProp = System.getProperty(CHARSET_PROPERTY_PREFIX + format); if (csProp != null) { csProp = csProp.trim(); - if (csProp.length() > 0) { + if (!csProp.isEmpty()) { return Charset.forName(csProp); } } diff --git a/src/main/java/io/github/spannm/jackcess/impl/General97IndexCodes.java b/src/main/java/io/github/spannm/jackcess/impl/General97IndexCodes.java index d643114..6237b34 100644 --- a/src/main/java/io/github/spannm/jackcess/impl/General97IndexCodes.java +++ b/src/main/java/io/github/spannm/jackcess/impl/General97IndexCodes.java @@ -180,7 +180,7 @@ static short[] loadMappings(String mappingsFilePath, char firstChar, char lastCh String mappingLine = null; while ((mappingLine = reader.readLine()) != null) { mappingLine = mappingLine.trim(); - if (mappingLine.length() == 0) { + if (mappingLine.isEmpty()) { continue; } diff --git a/src/main/java/io/github/spannm/jackcess/impl/GeneralLegacyIndexCodes.java b/src/main/java/io/github/spannm/jackcess/impl/GeneralLegacyIndexCodes.java index c1f95f1..d758032 100644 --- a/src/main/java/io/github/spannm/jackcess/impl/GeneralLegacyIndexCodes.java +++ b/src/main/java/io/github/spannm/jackcess/impl/GeneralLegacyIndexCodes.java @@ -492,7 +492,7 @@ private static CharHandler parseSignificantCodes(String[] codeStrings) { * Converts a string of hex encoded bytes to a byte[], optionally throwing an exception if no codes are given. */ private static byte[] codesToBytes(String codes, boolean required) { - if (codes.length() == 0) { + if (codes.isEmpty()) { if (required) { throw new IllegalStateException("empty code bytes"); } diff --git a/src/main/java/io/github/spannm/jackcess/impl/expr/DefaultFunctions.java b/src/main/java/io/github/spannm/jackcess/impl/expr/DefaultFunctions.java index 600edd7..218da9d 100644 --- a/src/main/java/io/github/spannm/jackcess/impl/expr/DefaultFunctions.java +++ b/src/main/java/io/github/spannm/jackcess/impl/expr/DefaultFunctions.java @@ -63,7 +63,7 @@ protected Value eval3(EvalContext ctx, public static final Function HEX = registerStringFunc(new Func1NullIsNull("Hex") { @Override protected Value eval1(EvalContext ctx, Value param1) { - if (param1.getType().isString() && param1.getAsString(ctx).length() == 0) { + if (param1.getType().isString() && param1.getAsString(ctx).isEmpty()) { return ValueSupport.ZERO_VAL; } int lv = param1.getAsLongInt(ctx); @@ -116,7 +116,7 @@ protected Value evalVar(EvalContext ctx, Value[] params) { public static final Function OCT = registerStringFunc(new Func1NullIsNull("Oct") { @Override protected Value eval1(EvalContext ctx, Value param1) { - if (param1.getType().isString() && param1.getAsString(ctx).length() == 0) { + if (param1.getType().isString() && param1.getAsString(ctx).isEmpty()) { return ValueSupport.ZERO_VAL; } int lv = param1.getAsLongInt(ctx); @@ -416,7 +416,7 @@ protected Value eval1(EvalContext ctx, Value param1) { String str = ValueSupport.WHITESPACE_PAT.matcher(param1.getAsString(ctx)) .replaceAll(""); - if (str.length() == 0) { + if (str.isEmpty()) { return ValueSupport.ZERO_D_VAL; } diff --git a/src/main/java/io/github/spannm/jackcess/impl/expr/Expressionator.java b/src/main/java/io/github/spannm/jackcess/impl/expr/Expressionator.java index d036105..7ff46a3 100644 --- a/src/main/java/io/github/spannm/jackcess/impl/expr/Expressionator.java +++ b/src/main/java/io/github/spannm/jackcess/impl/expr/Expressionator.java @@ -1299,7 +1299,7 @@ public static Pattern likePatternToRegex(String pattern) { String charClass = pattern.substring(startPos, endPos); - if (charClass.length() > 0 && charClass.charAt(0) == '!') { + if (!charClass.isEmpty() && charClass.charAt(0) == '!') { // this is a negated char class charClass = '^' + charClass.substring(1); } @@ -1364,7 +1364,7 @@ private static boolean isLiteralDefaultValue( } return resultType == Value.Type.STRING - && (exprStr.length() == 0 || exprStr.charAt(0) != ExpressionTokenizer.QUOTED_STR_CHAR); + && (exprStr.isEmpty() || exprStr.charAt(0) != ExpressionTokenizer.QUOTED_STR_CHAR); } private interface LeftAssocExpr { diff --git a/src/main/java/io/github/spannm/jackcess/impl/expr/FormatUtil.java b/src/main/java/io/github/spannm/jackcess/impl/expr/FormatUtil.java index 3c61735..6fda1b1 100644 --- a/src/main/java/io/github/spannm/jackcess/impl/expr/FormatUtil.java +++ b/src/main/java/io/github/spannm/jackcess/impl/expr/FormatUtil.java @@ -924,7 +924,7 @@ private static BDFormat createCustomNumberFormat( String fmtStr = fmtStrs[fmtIdx]; if (!hasFmts[fmtIdx]) { // convert the literal string to a dummy number format - if (fmtStr.length() > 0) { + if (!fmtStr.isEmpty()) { // strip quoting StringBuilder sb = buf.getScratchBuffer().append(fmtStr) .deleteCharAt(fmtStr.length() - 1) diff --git a/src/main/java/io/github/spannm/jackcess/impl/expr/StringValue.java b/src/main/java/io/github/spannm/jackcess/impl/expr/StringValue.java index 6ecbd3d..2d26b7f 100644 --- a/src/main/java/io/github/spannm/jackcess/impl/expr/StringValue.java +++ b/src/main/java/io/github/spannm/jackcess/impl/expr/StringValue.java @@ -108,7 +108,7 @@ protected BigDecimal getNumber(LocaleContext ctx) { // ignore extraneous whitespace whitespace and handle "&[hH]" or // "&[oO]" prefix (only supports integers) String tmpVal = _val.trim(); - if (tmpVal.length() > 0) { + if (!tmpVal.isEmpty()) { if (tmpVal.charAt(0) != ValueSupport.NUMBER_BASE_PREFIX) { // convert to standard numeric support for parsing diff --git a/src/test/java/io/github/spannm/jackcess/impl/IndexCodesTest.java b/src/test/java/io/github/spannm/jackcess/impl/IndexCodesTest.java index 6af416d..685c220 100644 --- a/src/test/java/io/github/spannm/jackcess/impl/IndexCodesTest.java +++ b/src/test/java/io/github/spannm/jackcess/impl/IndexCodesTest.java @@ -413,7 +413,7 @@ public void x_testReverseIsoMdb2010() throws Exception { Character cc = c; String[] chars = inlineCodes.get(cc); if (chars != null) { - if (chars.length == 1 && chars[0].length() == 0) { + if (chars.length == 1 && chars[0].isEmpty()) { System.out.println("X"); } else { System.out.println("S" + toByteString(chars)); @@ -580,7 +580,7 @@ public void x_testReverseIsoMdb() throws Exception { Character cc = c; String[] chars = inlineCodes.get(cc); if (chars != null) { - if (chars.length == 1 && chars[0].length() == 0) { + if (chars.length == 1 && chars[0].isEmpty()) { System.out.println("X"); } else { System.out.println("S" + toByteString(chars)); @@ -638,7 +638,7 @@ public void x_testReverseIsoMdb() throws Exception { private static String toByteString(String[] chars) { String str = join(chars, "", ""); - if (str.length() > 0 && str.charAt(0) == '0') { + if (!str.isEmpty() && str.charAt(0) == '0') { str = str.substring(1); } return str; @@ -667,7 +667,7 @@ private static void handleInternational2Entry(String inlineCodes, String entryCo if (entryCodes != null) { inatExtraCodes.put(c, entryCodes.trim().split(" ")); } - if (crazyCodes != null && crazyCodes.length() > 0) { + if (crazyCodes != null && !crazyCodes.isEmpty()) { inatCrazyCodes.put(c, crazyCodes.trim().split(" ")); } } @@ -699,7 +699,7 @@ private static String join(String[] strs, String joinStr, String prefixStr) { } StringBuilder builder = new StringBuilder(); for (int i = 0; i < strs.length; ++i) { - if (strs[i].length() == 0) { + if (strs[i].isEmpty()) { continue; } builder.append(prefixStr).append(strs[i]); diff --git a/src/test/java/io/github/spannm/jackcess/impl/JetFormatTest.java b/src/test/java/io/github/spannm/jackcess/impl/JetFormatTest.java index e3b6b39..6043675 100644 --- a/src/test/java/io/github/spannm/jackcess/impl/JetFormatTest.java +++ b/src/test/java/io/github/spannm/jackcess/impl/JetFormatTest.java @@ -81,7 +81,7 @@ public String toString() { static { String testFormatStr = System.getProperty("io.github.spannm.jackcess.testFormats"); Set testFormats = EnumSet.allOf(FileFormat.class); - if (testFormatStr != null && testFormatStr.length() > 0) { + if (testFormatStr != null && !testFormatStr.isEmpty()) { testFormats.clear(); for (String tmp : testFormatStr.split(",")) { testFormats.add(FileFormat.valueOf(tmp.toUpperCase()));