From e1831ba780da341a93552a440db4f24df02f2c2a Mon Sep 17 00:00:00 2001 From: Henning Gerhardt Date: Fri, 23 Feb 2024 10:42:52 +0100 Subject: [PATCH 1/2] Replace equals("") with more efficient isEmpty() calls --- .../filters/FileNameBeginsAndEndsWithFilter.java | 2 +- .../filters/FileNameEndsAndDoesNotBeginWithFilter.java | 2 +- .../api/filemanagement/filters/FileNameEndsWithFilter.java | 2 +- .../api/filemanagement/filters/FileNameMatchesFilter.java | 2 +- .../production/forms/dataeditor/AddDocStrucTypeDialog.java | 2 +- .../src/main/java/org/kitodo/production/helper/Helper.java | 2 +- .../src/main/java/org/kitodo/production/ldap/LdapUser.java | 2 +- .../kitodo/production/process/field/AdditionalField.java | 6 +++--- .../org/kitodo/production/services/file/FileService.java | 4 ++-- .../kitodo/production/services/index/IndexingService.java | 2 +- .../java/org/kitodo/selenium/testframework/pages/Page.java | 2 +- 11 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameBeginsAndEndsWithFilter.java b/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameBeginsAndEndsWithFilter.java index dfee3e3a9e4..fc746c1ff46 100644 --- a/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameBeginsAndEndsWithFilter.java +++ b/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameBeginsAndEndsWithFilter.java @@ -34,7 +34,7 @@ public class FileNameBeginsAndEndsWithFilter implements FilenameFilter { * Strings */ public FileNameBeginsAndEndsWithFilter(String begin, String end) { - if (begin == null || begin.equals("") || end == null || end.equals("")) { + if (begin == null || begin.isEmpty() || end == null || end.isEmpty()) { throw new IllegalArgumentException("No filter or empty filter for file begin or end is given."); } this.begin = begin; diff --git a/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameEndsAndDoesNotBeginWithFilter.java b/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameEndsAndDoesNotBeginWithFilter.java index c525d2a9232..089f7dd8311 100644 --- a/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameEndsAndDoesNotBeginWithFilter.java +++ b/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameEndsAndDoesNotBeginWithFilter.java @@ -34,7 +34,7 @@ public class FileNameEndsAndDoesNotBeginWithFilter implements FilenameFilter { * Strings */ public FileNameEndsAndDoesNotBeginWithFilter(String notBegin, String end) { - if (notBegin == null || notBegin.equals("") || end == null || end.equals("")) { + if (notBegin == null || notBegin.isEmpty() || end == null || end.isEmpty()) { throw new IllegalArgumentException("No filter or empty filter for file begin or end is given."); } this.notBegin = notBegin; diff --git a/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameEndsWithFilter.java b/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameEndsWithFilter.java index 0b504d2bfcc..4f103cc9a27 100644 --- a/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameEndsWithFilter.java +++ b/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameEndsWithFilter.java @@ -30,7 +30,7 @@ public class FileNameEndsWithFilter implements FilenameFilter { * it is thrown in case parameter is null or empty String */ public FileNameEndsWithFilter(String end) { - if (end == null || end.equals("")) { + if (end == null || end.isEmpty()) { throw new IllegalArgumentException("No filter or empty filter for file end is given."); } this.end = end; diff --git a/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameMatchesFilter.java b/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameMatchesFilter.java index 0b8de1038e5..093477d04d2 100644 --- a/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameMatchesFilter.java +++ b/Kitodo-API/src/main/java/org/kitodo/api/filemanagement/filters/FileNameMatchesFilter.java @@ -27,7 +27,7 @@ public class FileNameMatchesFilter implements FilenameFilter { * it is thrown in case parameter is null or empty String */ public FileNameMatchesFilter(String name) { - if (name == null || name.equals("")) { + if (name == null || name.isEmpty()) { throw new IllegalArgumentException("No filter or empty name is given."); } this.name = name; diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/AddDocStrucTypeDialog.java b/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/AddDocStrucTypeDialog.java index 8182b345435..05ebbc55fb5 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/AddDocStrucTypeDialog.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/AddDocStrucTypeDialog.java @@ -149,7 +149,7 @@ public void addDocStruc(boolean preview) { private void addMultiDocStruc() { Optional selectedStructure = dataEditor.getSelectedStructure(); if (selectedStructure.isPresent()) { - if (selectedMetadata != "") { + if (!selectedMetadata.isEmpty()) { MetadataViewInterface metadataView = getMetadataViewFromKey( docStructAddTypeSelectionSelectedItem, selectedMetadata); MetadataEditor.addMultipleStructuresWithMetadata(elementsToAddSpinnerValue, diff --git a/Kitodo/src/main/java/org/kitodo/production/helper/Helper.java b/Kitodo/src/main/java/org/kitodo/production/helper/Helper.java index ad51fbc98db..df0e162b955 100644 --- a/Kitodo/src/main/java/org/kitodo/production/helper/Helper.java +++ b/Kitodo/src/main/java/org/kitodo/production/helper/Helper.java @@ -412,7 +412,7 @@ public static String getDateAsFormattedString(Date date) { * @return the date or null if it can not be parsed */ public static Date parseDateFromFormattedString(String date) { - if (Objects.isNull(date) || date.equals("")) { + if (Objects.isNull(date) || date.isEmpty()) { return null; } try { diff --git a/Kitodo/src/main/java/org/kitodo/production/ldap/LdapUser.java b/Kitodo/src/main/java/org/kitodo/production/ldap/LdapUser.java index a57cb7e12da..1990e10774e 100644 --- a/Kitodo/src/main/java/org/kitodo/production/ldap/LdapUser.java +++ b/Kitodo/src/main/java/org/kitodo/production/ldap/LdapUser.java @@ -278,7 +278,7 @@ public static String toHexString(byte[] bytes) { @Override public Attributes getAttributes(String name) throws NamingException { - if (!name.equals("")) { + if (!name.isEmpty()) { throw new NameNotFoundException(); } return (Attributes) this.attributes.clone(); diff --git a/Kitodo/src/main/java/org/kitodo/production/process/field/AdditionalField.java b/Kitodo/src/main/java/org/kitodo/production/process/field/AdditionalField.java index e63be7dd68c..68f3989e174 100644 --- a/Kitodo/src/main/java/org/kitodo/production/process/field/AdditionalField.java +++ b/Kitodo/src/main/java/org/kitodo/production/process/field/AdditionalField.java @@ -329,16 +329,16 @@ public boolean isAutogenerated() { */ public boolean showDependingOnDoctype() { // if nothing was specified, then show - if (this.isDocType.equals("") && this.isNotDoctype.equals("")) { + if (this.isDocType.isEmpty() && this.isNotDoctype.isEmpty()) { return true; } // if obligatory was specified - if (!this.isDocType.equals("") && !StringUtils.containsIgnoreCase(this.isDocType, this.docType)) { + if (!this.isDocType.isEmpty() && !StringUtils.containsIgnoreCase(this.isDocType, this.docType)) { return false; } // if only "may not" was specified - return !(!this.isNotDoctype.equals("") && StringUtils.containsIgnoreCase(this.isNotDoctype, this.docType)); + return !(!this.isNotDoctype.isEmpty() && StringUtils.containsIgnoreCase(this.isNotDoctype, this.docType)); } } diff --git a/Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java b/Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java index a5fb16915b0..3b1fce697db 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java @@ -1183,14 +1183,14 @@ private List getCanonicalFileNamePartsAndSanitizeAbsoluteURIs(Workpiece physicalDivision.getMediaFiles().put(entry.getKey(), mediaFile); } String fileCanonical = subfolder.getCanonical(mediaFile); - if ("".equals(unitCanonical)) { + if (unitCanonical.isEmpty()) { unitCanonical = fileCanonical; } else if (!unitCanonical.equals(fileCanonical)) { throw new InvalidImagesException("Ambiguous canonical file name part in the same physical division: \"" + unitCanonical + "\" and \"" + fileCanonical + "\"!"); } } - if (physicalDivision.getMediaFiles().size() > 0 && "".equals(unitCanonical)) { + if (!physicalDivision.getMediaFiles().isEmpty() && unitCanonical.isEmpty()) { throw new InvalidImagesException("Missing canonical file name part in physical division " + physicalDivision); } canonicals.add(unitCanonical); diff --git a/Kitodo/src/main/java/org/kitodo/production/services/index/IndexingService.java b/Kitodo/src/main/java/org/kitodo/production/services/index/IndexingService.java index 5e529ecd0a6..ac3a02592e7 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/index/IndexingService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/index/IndexingService.java @@ -426,7 +426,7 @@ public int getProgress(ObjectType currentType, PushContext pollingChannel) throw public String createMapping() throws IOException, CustomResponseException { for (String mappingType : KitodoRestClient.MAPPING_TYPES) { String mapping = readMapping(mappingType); - if ("".equals(mapping)) { + if (mapping.isEmpty()) { if (indexRestClient.createIndex(null, mappingType)) { currentState = IndexStates.CREATING_MAPPING_SUCCESSFUL; } else { diff --git a/Kitodo/src/test/java/org/kitodo/selenium/testframework/pages/Page.java b/Kitodo/src/test/java/org/kitodo/selenium/testframework/pages/Page.java index b3fc4580c90..f29aa10188e 100644 --- a/Kitodo/src/test/java/org/kitodo/selenium/testframework/pages/Page.java +++ b/Kitodo/src/test/java/org/kitodo/selenium/testframework/pages/Page.java @@ -238,7 +238,7 @@ private String getWebElementText(WebElement webElement) { }; Predicate isInputValueNotEmpty = (webElement) -> { - return !webElement.getAttribute("value").equals(""); + return !webElement.getAttribute("value").isEmpty(); }; Predicate isFileDownloaded = (file) -> { From b598388abce560b44a69f8aa0a76f43a3ec41cfa Mon Sep 17 00:00:00 2001 From: Henning Gerhardt Date: Mon, 26 Feb 2024 10:32:54 +0100 Subject: [PATCH 2/2] Adjust calls to prevent null pointer exceptions --- .../src/main/java/org/kitodo/production/ldap/LdapUser.java | 5 +++-- .../org/kitodo/production/services/file/FileService.java | 4 ++-- .../kitodo/production/services/index/IndexingService.java | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Kitodo/src/main/java/org/kitodo/production/ldap/LdapUser.java b/Kitodo/src/main/java/org/kitodo/production/ldap/LdapUser.java index 1990e10774e..a9aad2e65dd 100644 --- a/Kitodo/src/main/java/org/kitodo/production/ldap/LdapUser.java +++ b/Kitodo/src/main/java/org/kitodo/production/ldap/LdapUser.java @@ -44,6 +44,7 @@ import javax.naming.directory.SearchResult; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bouncycastle.crypto.digests.MD4Digest; @@ -278,7 +279,7 @@ public static String toHexString(byte[] bytes) { @Override public Attributes getAttributes(String name) throws NamingException { - if (!name.isEmpty()) { + if (StringUtils.isBlank(name)) { throw new NameNotFoundException(); } return (Attributes) this.attributes.clone(); @@ -291,7 +292,7 @@ public Attributes getAttributes(Name name) throws NamingException { @Override public Attributes getAttributes(String name, String[] ids) throws NamingException { - if (!name.isEmpty()) { + if (StringUtils.isBlank(name)) { throw new NameNotFoundException(); } diff --git a/Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java b/Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java index 3b1fce697db..657f46d50dd 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java @@ -1183,14 +1183,14 @@ private List getCanonicalFileNamePartsAndSanitizeAbsoluteURIs(Workpiece physicalDivision.getMediaFiles().put(entry.getKey(), mediaFile); } String fileCanonical = subfolder.getCanonical(mediaFile); - if (unitCanonical.isEmpty()) { + if (StringUtils.isBlank(unitCanonical)) { unitCanonical = fileCanonical; } else if (!unitCanonical.equals(fileCanonical)) { throw new InvalidImagesException("Ambiguous canonical file name part in the same physical division: \"" + unitCanonical + "\" and \"" + fileCanonical + "\"!"); } } - if (!physicalDivision.getMediaFiles().isEmpty() && unitCanonical.isEmpty()) { + if (!physicalDivision.getMediaFiles().isEmpty() && StringUtils.isBlank(unitCanonical)) { throw new InvalidImagesException("Missing canonical file name part in physical division " + physicalDivision); } canonicals.add(unitCanonical); diff --git a/Kitodo/src/main/java/org/kitodo/production/services/index/IndexingService.java b/Kitodo/src/main/java/org/kitodo/production/services/index/IndexingService.java index ac3a02592e7..547614e0f51 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/index/IndexingService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/index/IndexingService.java @@ -33,6 +33,7 @@ import javax.json.JsonReader; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.kitodo.config.ConfigCore; @@ -426,7 +427,7 @@ public int getProgress(ObjectType currentType, PushContext pollingChannel) throw public String createMapping() throws IOException, CustomResponseException { for (String mappingType : KitodoRestClient.MAPPING_TYPES) { String mapping = readMapping(mappingType); - if (mapping.isEmpty()) { + if (StringUtils.isBlank(mapping)) { if (indexRestClient.createIndex(null, mappingType)) { currentState = IndexStates.CREATING_MAPPING_SUCCESSFUL; } else {