From deadc9db42479409e4b745a8fa424cfa78069bde Mon Sep 17 00:00:00 2001 From: Sam Bishop Date: Mon, 10 Feb 2025 10:22:57 -0500 Subject: [PATCH] pmd :: CollapsibleIfStatement --- src/main/java/emissary/core/MobileAgent.java | 16 ++++++---------- .../emissary/directory/DirectoryEntryList.java | 8 +++----- .../java/emissary/directory/KeyManipulator.java | 6 ++---- src/main/java/emissary/kff/KffMemcached.java | 12 +++++------- src/main/java/emissary/kff/Ssdeep.java | 8 +++----- src/main/java/emissary/roll/Roller.java | 6 ++---- src/main/java/emissary/util/StringUtil.java | 6 ++---- 7 files changed, 23 insertions(+), 39 deletions(-) diff --git a/src/main/java/emissary/core/MobileAgent.java b/src/main/java/emissary/core/MobileAgent.java index 43639dc30a..068ee63df9 100755 --- a/src/main/java/emissary/core/MobileAgent.java +++ b/src/main/java/emissary/core/MobileAgent.java @@ -515,10 +515,8 @@ protected DirectoryEntry getNextKey(@Nullable final IServiceProviderPlace place, int startType = typeLookup(lastServiceType); // If we came from transform we can start at the beginning again - if (lastEntry != null && startType != 0) { - if ("TRANSFORM".equals(lastEntry.getServiceType())) { - startType = 0; - } + if (lastEntry != null && startType != 0 && "TRANSFORM".equals(lastEntry.getServiceType())) { + startType = 0; } DirectoryEntry curEntry = null; @@ -528,12 +526,10 @@ protected DirectoryEntry getNextKey(@Nullable final IServiceProviderPlace place, for (String form : dataForms) { // Test a full key form to see if it is the correct stage to be chosen - if (KeyManipulator.isKeyComplete(form)) { - if (KeyManipulator.getServiceType(form).equals(stageName)) { - logger.debug("Choosing cur form {} in stage {}", form, stageName); - payloadArg.pullFormToTop(form); - return new DirectoryEntry(form); - } + if (KeyManipulator.isKeyComplete(form) && KeyManipulator.getServiceType(form).equals(stageName)) { + logger.debug("Choosing cur form {} in stage {}", form, stageName); + payloadArg.pullFormToTop(form); + return new DirectoryEntry(form); } String formId = form + KeyManipulator.DATAIDSEPARATOR + stageName; diff --git a/src/main/java/emissary/directory/DirectoryEntryList.java b/src/main/java/emissary/directory/DirectoryEntryList.java index accda5f25b..f2a406cb2e 100755 --- a/src/main/java/emissary/directory/DirectoryEntryList.java +++ b/src/main/java/emissary/directory/DirectoryEntryList.java @@ -159,12 +159,10 @@ private boolean insert(final DirectoryEntry newEntry) { if (newEntry.isBetterThan(currEntry)) { super.add(i, newEntry); return true; - } else if (newEntry.getExpense() == currEntry.getExpense()) { + } else if (newEntry.getExpense() == currEntry.getExpense() && newEntry.getServiceName().compareTo(currEntry.getServiceName()) < 0) { // we could have an equal expense and if so, add lexicographically based on service name - if (newEntry.getServiceName().compareTo(currEntry.getServiceName()) < 0) { - super.add(i, newEntry); - return true; - } + super.add(i, newEntry); + return true; } } diff --git a/src/main/java/emissary/directory/KeyManipulator.java b/src/main/java/emissary/directory/KeyManipulator.java index fceb7779de..7f2cc7c29a 100755 --- a/src/main/java/emissary/directory/KeyManipulator.java +++ b/src/main/java/emissary/directory/KeyManipulator.java @@ -156,10 +156,8 @@ private static boolean gmatch2(final char[] s, final char[] p, final int spos, f } int spos2 = spos; while (s.length > ++spos2) { - if (p[ppos2] == s[spos2]) { - if (gmatch2(s, p, spos2, ppos2)) { - return true; - } + if (p[ppos2] == s[spos2] && gmatch2(s, p, spos2, ppos2)) { + return true; } } return false; diff --git a/src/main/java/emissary/kff/KffMemcached.java b/src/main/java/emissary/kff/KffMemcached.java index f59ef8336b..04dff4e12e 100644 --- a/src/main/java/emissary/kff/KffMemcached.java +++ b/src/main/java/emissary/kff/KffMemcached.java @@ -242,13 +242,11 @@ public boolean check(String id, ChecksumResults sums) throws Exception { Object result = future.get(opTimeoutMillis, TimeUnit.MILLISECONDS); if (result != null) { - if (storeIdDupe) { - if (!((String) result).equals(id)) { - // As long as the id is not the same as what was already stored, then - // store it on its own - var unused = client.set(id, ageoff, key); - // logger.debug("Storing duplicate Id: {} with value (hash) {}", id, key); - } + if (storeIdDupe && !result.equals(id)) { + // As long as the id is not the same as what was already stored, then + // store it on its own + var unused = client.set(id, ageoff, key); + // logger.debug("Storing duplicate Id: {} with value (hash) {}", id, key); } // logger.debug("Found key: {} with value {}", key, (String) result); // Found the key diff --git a/src/main/java/emissary/kff/Ssdeep.java b/src/main/java/emissary/kff/Ssdeep.java index 3e942f04d6..bb2383cc21 100644 --- a/src/main/java/emissary/kff/Ssdeep.java +++ b/src/main/java/emissary/kff/Ssdeep.java @@ -202,11 +202,9 @@ private void applyBytes(final RollingState rollState, final byte[] buffer, final // Therefore this secondary signature condition can // only potentially be true if the main signature // condition (which we've already checked) is true. - if ((rollingHash % (this.blockSize * 2)) == ((this.blockSize * 2) - 1)) { - if (this.fuzzLen2 < (SPAMSUM_LENGTH / 2 - 1)) { - this.fuzzHash2[this.fuzzLen2++] = b64EncodeLowBits(this.sumHash2); - this.sumHash2 = HASH_INIT; - } + if ((rollingHash % (this.blockSize * 2)) == ((this.blockSize * 2) - 1) && this.fuzzLen2 < (SPAMSUM_LENGTH / 2 - 1)) { + this.fuzzHash2[this.fuzzLen2++] = b64EncodeLowBits(this.sumHash2); + this.sumHash2 = HASH_INIT; } } } diff --git a/src/main/java/emissary/roll/Roller.java b/src/main/java/emissary/roll/Roller.java index 526d6bcd23..dcd1a89a86 100644 --- a/src/main/java/emissary/roll/Roller.java +++ b/src/main/java/emissary/roll/Roller.java @@ -154,11 +154,9 @@ private long getIntervalInMillis() { private boolean shouldRoll(long start) { // verify both time and progress are set // if we're below max we'll check the interval - if (period > 0 && max > 0 && progress.get() < max) { + if (period > 0 && max > 0 && progress.get() < max && ((start - lastRun + 100) < getIntervalInMillis())) { // we fired a progress run or delayed start due to starvation. add 100 for clock skew - if ((start - lastRun + 100) < getIntervalInMillis()) { - return false; - } + return false; } return true; } diff --git a/src/main/java/emissary/util/StringUtil.java b/src/main/java/emissary/util/StringUtil.java index f1c457a186..0d943edcbc 100755 --- a/src/main/java/emissary/util/StringUtil.java +++ b/src/main/java/emissary/util/StringUtil.java @@ -14,10 +14,8 @@ public static String escapeInvalidXml(String in) { // this is stolen from the implementation of Verifier.checkCharacterData(in); for (int i = 0, len = in.length(); i < len; i++) { char c = in.charAt(i); - if (!(c > 0x1F && c < 0xD800)) { // for performance - if (!Verifier.isXMLCharacter(c)) { - return in.substring(0, i) + "/0x" + Integer.toHexString(c) + escapeInvalidXml(in.substring(i + 1)); - } + if (!(c > 0x1F && c < 0xD800) && !Verifier.isXMLCharacter(c)) { + return in.substring(0, i) + "/0x" + Integer.toHexString(c) + escapeInvalidXml(in.substring(i + 1)); } } return in;