From 5997c9b25f6bf4d65abf3836b8774bac82a1f016 Mon Sep 17 00:00:00 2001 From: lbownik Date: Thu, 5 Dec 2024 19:50:13 +0100 Subject: [PATCH] compilation warnings fixes dead code elimination rewritten iterators to for-each loops rewritten loops into functional style some static imports added --- .../harvard/iq/dataverse/common/BitSet.java | 3 +- .../iq/dataverse/common/BundleUtil.java | 4 --- .../persistence/dataset/MetadataBlock.java | 27 +++++++++-------- .../dataset/MetadataBlockTest.java | 30 +++++++++++++++++++ .../edu/harvard/iq/dataverse/api/Files.java | 3 -- .../providers/shib/ShibServiceBean.java | 4 --- .../filesystem/FileRecordJobListener.java | 2 +- .../dataaccess/ImageThumbConverter.java | 4 +-- .../ingest/plugin/spi/ServiceRegistry.java | 14 ++++----- .../timer/DataverseTimerServiceBean.java | 10 ++----- .../command/impl/MoveDatasetCommandTest.java | 6 ++-- .../impl/MoveDataverseCommandTest.java | 2 +- 12 files changed, 60 insertions(+), 49 deletions(-) create mode 100644 dataverse-persistence/src/test/java/edu/harvard/iq/dataverse/persistence/dataset/MetadataBlockTest.java diff --git a/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/common/BitSet.java b/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/common/BitSet.java index bc1d0e7c69..728ff6cffc 100644 --- a/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/common/BitSet.java +++ b/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/common/BitSet.java @@ -8,6 +8,7 @@ * * @author michael */ +@SuppressWarnings("serial") public class BitSet implements Serializable { private long store = 0l; @@ -49,7 +50,7 @@ public static BitSet from(EnumSet es) { return emptySet(); } BitSet retVal = new BitSet(); - for (Enum e : es) { + for (Enum e : es) { retVal.set(e.ordinal()); } return retVal; diff --git a/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/common/BundleUtil.java b/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/common/BundleUtil.java index 7a419ce71d..64dd326416 100644 --- a/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/common/BundleUtil.java +++ b/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/common/BundleUtil.java @@ -85,10 +85,6 @@ public static Locale getCurrentLocale() { // -------------------- PRIVATE -------------------- - private static String getStringFromPropertyFile(String bundleKey, String bundleName) throws MissingResourceException { - return getStringFromPropertyFile(bundleKey, bundleName, getCurrentLocale()); - } - /** * Gets display name for specified bundle key. If it is external bundle, * method tries to access external directory (jvm property - dataverse.lang.directory) diff --git a/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/persistence/dataset/MetadataBlock.java b/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/persistence/dataset/MetadataBlock.java index 8a5f9baa78..1f8fbc7b44 100644 --- a/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/persistence/dataset/MetadataBlock.java +++ b/dataverse-persistence/src/main/java/edu/harvard/iq/dataverse/persistence/dataset/MetadataBlock.java @@ -1,13 +1,10 @@ package edu.harvard.iq.dataverse.persistence.dataset; -import edu.harvard.iq.dataverse.common.BundleUtil; import edu.harvard.iq.dataverse.persistence.dataverse.Dataverse; -import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Index; import javax.persistence.JoinColumn; @@ -18,6 +15,13 @@ import javax.persistence.OrderBy; import javax.persistence.Table; import javax.persistence.Transient; + +import static edu.harvard.iq.dataverse.common.BundleUtil.getStringFromNonDefaultBundle; +import static javax.persistence.CascadeType.MERGE; +import static javax.persistence.CascadeType.PERSIST; +import static javax.persistence.CascadeType.REMOVE; +import static javax.persistence.GenerationType.IDENTITY; + import java.io.Serializable; import java.util.List; import java.util.Objects; @@ -37,7 +41,7 @@ public class MetadataBlock implements Serializable { private static final long serialVersionUID = 1L; @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) + @GeneratedValue(strategy = IDENTITY) private Long id; @Column(nullable = false) @@ -72,7 +76,7 @@ public void setNamespaceUri(String namespaceUri) { this.namespaceUri = namespaceUri; } - @OneToMany(mappedBy = "metadataBlock", cascade = {CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}) + @OneToMany(mappedBy = "metadataBlock", cascade = {REMOVE, MERGE, PERSIST}) @OrderBy("displayOrder") private List datasetFieldTypes; @@ -85,12 +89,8 @@ public void setDatasetFieldTypes(List datasetFieldTypes) { } public boolean isDisplayOnCreate() { - for (DatasetFieldType dsfType : datasetFieldTypes) { - if (dsfType.isDisplayOnCreate()) { - return true; - } - } - return false; + return this.datasetFieldTypes.stream() + .anyMatch(DatasetFieldType::isDisplayOnCreate); } public String getDisplayName() { @@ -107,7 +107,8 @@ public boolean isCitationMetaBlock() { } @OneToOne - @JoinColumn(name = "owner_id", unique = false, nullable = true, insertable = true, updatable = true) + @JoinColumn(name = "owner_id", unique = false, nullable = true, + insertable = true, updatable = true) private Dataverse owner; public Dataverse getOwner() { @@ -188,7 +189,7 @@ public String toString() { } public String getLocaleDisplayName() { - String localeDisplayName = BundleUtil.getStringFromNonDefaultBundle("metadatablock.displayName", getName()); + String localeDisplayName = getStringFromNonDefaultBundle("metadatablock.displayName", getName()); return localeDisplayName.isEmpty() ? displayName : localeDisplayName; } } diff --git a/dataverse-persistence/src/test/java/edu/harvard/iq/dataverse/persistence/dataset/MetadataBlockTest.java b/dataverse-persistence/src/test/java/edu/harvard/iq/dataverse/persistence/dataset/MetadataBlockTest.java new file mode 100644 index 0000000000..40318e6fa9 --- /dev/null +++ b/dataverse-persistence/src/test/java/edu/harvard/iq/dataverse/persistence/dataset/MetadataBlockTest.java @@ -0,0 +1,30 @@ +package edu.harvard.iq.dataverse.persistence.dataset; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; + +public class MetadataBlockTest { + + @Test + void isDisplayOnCreate_works() { + MetadataBlock block = new MetadataBlock(); + block.setDatasetFieldTypes(new ArrayList<>()); + + assertThat(block.isDisplayOnCreate()).isFalse(); + + block.getDatasetFieldTypes().add(new DatasetFieldType()); + + assertThat(block.isDisplayOnCreate()).isFalse(); + + block.getDatasetFieldTypes().add(new DatasetFieldType()); + + assertThat(block.isDisplayOnCreate()).isFalse(); + + block.getDatasetFieldTypes().get(1).setDisplayOnCreate(true); + + assertThat(block.isDisplayOnCreate()).isTrue(); + } +} diff --git a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/api/Files.java b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/api/Files.java index 880703df82..bf43e82e2e 100644 --- a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/api/Files.java +++ b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/api/Files.java @@ -112,9 +112,6 @@ public Response replaceFileInDataset( // ------------------------------------- if ((jsonObj.has("forceReplace")) && (!jsonObj.get("forceReplace").isJsonNull())) { forceReplace = jsonObj.get("forceReplace").getAsBoolean(); - if (forceReplace == null) { - forceReplace = false; - } } try { // (2b) Load up optional params via JSON diff --git a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/authorization/providers/shib/ShibServiceBean.java b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/authorization/providers/shib/ShibServiceBean.java index a59a652e45..78ad02a4fa 100644 --- a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/authorization/providers/shib/ShibServiceBean.java +++ b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/authorization/providers/shib/ShibServiceBean.java @@ -171,10 +171,6 @@ public String getAffiliation(String shibIdp, DevShibAccountType devShibAccountTy logger.info(ex.toString()); return null; } - if (url == null) { - logger.info("url object was null after parsing " + discoFeedUrl); - return null; - } HttpURLConnection discoFeedRequest = null; try { discoFeedRequest = (HttpURLConnection) url.openConnection(); diff --git a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/batch/jobs/importer/filesystem/FileRecordJobListener.java b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/batch/jobs/importer/filesystem/FileRecordJobListener.java index 11b779b93f..8695e45eaf 100644 --- a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/batch/jobs/importer/filesystem/FileRecordJobListener.java +++ b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/batch/jobs/importer/filesystem/FileRecordJobListener.java @@ -220,7 +220,7 @@ public void afterJob() throws Exception { doReport(); // report any unused checksums - HashMap checksumHashMap = (HashMap) jobContext.getTransientUserData(); + HashMap checksumHashMap = (HashMap) jobContext.getTransientUserData(); for (Object key : checksumHashMap.keySet()) { getJobLogger().log(Level.SEVERE, "File listed in checksum manifest not found: " + key); } diff --git a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/dataaccess/ImageThumbConverter.java b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/dataaccess/ImageThumbConverter.java index 1503ab41fa..8d561109fb 100644 --- a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/dataaccess/ImageThumbConverter.java +++ b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/dataaccess/ImageThumbConverter.java @@ -642,9 +642,9 @@ private void rescaleImage(BufferedImage fullSizeImage, int width, int height, in thumbImage = makeColorTransparent(bufferedImageForTransparency, new Color(color)); */ ImageWriter writer = null; - Iterator iter = ImageIO.getImageWritersByFormatName("png"); + Iterator iter = ImageIO.getImageWritersByFormatName("png"); if (iter.hasNext()) { - writer = (ImageWriter) iter.next(); + writer = iter.next(); } else { throw new IOException("Failed to locatie ImageWriter plugin for image type PNG"); } diff --git a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/ingest/plugin/spi/ServiceRegistry.java b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/ingest/plugin/spi/ServiceRegistry.java index 35c48e23c3..f0a525a377 100644 --- a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/ingest/plugin/spi/ServiceRegistry.java +++ b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/ingest/plugin/spi/ServiceRegistry.java @@ -508,8 +508,8 @@ public Iterator getServiceProviders(Class category, if (reg == null) { throw new IllegalArgumentException("category unknown!"); } - Iterator iter = getServiceProviders(category, useOrdering); - return new FilterIterator(iter, filter); + Iterator iter = getServiceProviders(category, useOrdering); + return new FilterIterator(iter, filter); } /** @@ -531,7 +531,7 @@ public T getServiceProviderByClass(Class providerClass) { if (providerClass == null) { throw new IllegalArgumentException("providerClass == null!"); } - for (Class c : categoryMap.keySet()) { + for (Class c : categoryMap.keySet()) { if (c.isAssignableFrom(providerClass)) { SubRegistry reg = categoryMap.get(c); T provider = reg.getServiceProviderByClass(providerClass); @@ -652,11 +652,7 @@ public void deregisterAll(Class category) { * categories. */ public void deregisterAll() { - Iterator iter = categoryMap.values().iterator(); - while (iter.hasNext()) { - SubRegistry reg = (SubRegistry) iter.next(); - reg.clear(); - } + this.categoryMap.values().forEach(SubRegistry::clear); } /** @@ -762,7 +758,7 @@ public T getServiceProviderByClass(Class providerClass) { } public void clear() { - Iterator iter = map.values().iterator(); + Iterator iter = map.values().iterator(); while (iter.hasNext()) { Object provider = iter.next(); iter.remove(); diff --git a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/timer/DataverseTimerServiceBean.java b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/timer/DataverseTimerServiceBean.java index fac63a20ec..4169588971 100644 --- a/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/timer/DataverseTimerServiceBean.java +++ b/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/timer/DataverseTimerServiceBean.java @@ -247,13 +247,9 @@ public void removeAllTimers() { int i = 0; - for (Iterator it = timerService.getTimers().iterator(); it.hasNext(); ) { - - Timer timer = (Timer) it.next(); - + for(final Timer timer : timerService.getTimers()) { logger.info("Removing timer " + i + ";"); timer.cancel(); - i++; } logger.info("Done!"); @@ -269,9 +265,7 @@ public void removeHarvestTimers() { logger.log(Level.INFO, "Removing existing harvest timers.."); int i = 1; - for (Iterator it = timerService.getTimers().iterator(); it.hasNext(); ) { - - Timer timer = (Timer) it.next(); + for(final Timer timer : timerService.getTimers()) { logger.log(Level.INFO, "HarvesterService: checking timer " + i); if (timer.getInfo() instanceof HarvestTimerInfo) { diff --git a/dataverse-webapp/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/MoveDatasetCommandTest.java b/dataverse-webapp/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/MoveDatasetCommandTest.java index bfbab512ec..143d91ce24 100644 --- a/dataverse-webapp/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/MoveDatasetCommandTest.java +++ b/dataverse-webapp/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/MoveDatasetCommandTest.java @@ -141,19 +141,19 @@ public void setUp() { gbResp.setGuestbook(gbA); gbResp.setDataset(movedResponses); - List includeA = new ArrayList(); + List includeA = new ArrayList<>(); includeA.add(gbA); includeA.add(gbB); grandchildAA.setGuestbooks(includeA); - List notIncludeA = new ArrayList(); + List notIncludeA = new ArrayList<>(); notIncludeA.add(gbC); notIncludeA.add(gbB); childB.setGuestbooks(notIncludeA); - List none = new ArrayList(); + List none = new ArrayList<>(); root.setGuestbooks(none); grandchildBB.setGuestbooks(none); grandchildBB.setGuestbookRoot(false); diff --git a/dataverse-webapp/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/MoveDataverseCommandTest.java b/dataverse-webapp/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/MoveDataverseCommandTest.java index d53a36bd19..ffe76dfe97 100644 --- a/dataverse-webapp/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/MoveDataverseCommandTest.java +++ b/dataverse-webapp/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/MoveDataverseCommandTest.java @@ -122,7 +122,7 @@ public void setUp() { datasetC.setGuestbook(gbA); datasetCC.setGuestbook(gbA); - List noneGb = new ArrayList(); + List noneGb = new ArrayList<>(); root.setGuestbooks(noneGb); childA.setGuestbooks(noneGb); grandchildAA.setGuestbooks(noneGb);