From 95c5f3e02244f8deebda1acc78b04b8630c5e9c8 Mon Sep 17 00:00:00 2001 From: Jonas Schaub <44881147+JonasSchaub@users.noreply.github.com> Date: Wed, 20 Dec 2023 11:14:24 +0100 Subject: [PATCH] Fixed bugs SonarCloud complained about; --- .../cdk/tools/ErtlFunctionalGroupsFinder.java | 2 +- ...ctionalGroupsFinderPerformanceSnapshotApp.java | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/openscience/cdk/tools/ErtlFunctionalGroupsFinder.java b/src/main/java/org/openscience/cdk/tools/ErtlFunctionalGroupsFinder.java index 6ee66d4..43c471b 100644 --- a/src/main/java/org/openscience/cdk/tools/ErtlFunctionalGroupsFinder.java +++ b/src/main/java/org/openscience/cdk/tools/ErtlFunctionalGroupsFinder.java @@ -605,7 +605,7 @@ private void expandFullEnvironments(List fGroups) { for(int i = 0; i < atomCount; i++) { IAtom atom = fGroup.getAtom(i); - if(isDbg()) log.debug(String.format(" - Atom #%d:% - Expanding environment...", i)); + if(isDbg()) log.debug(String.format(" - Atom #%d - Expanding environment...", i)); expandEnvironment(atom, fGroup); int hCount = atom.getImplicitHydrogenCount(); diff --git a/src/main/java/org/openscience/cdk/tools/efgf/app/ErtlFunctionalGroupsFinderPerformanceSnapshotApp.java b/src/main/java/org/openscience/cdk/tools/efgf/app/ErtlFunctionalGroupsFinderPerformanceSnapshotApp.java index cbdd185..721103b 100644 --- a/src/main/java/org/openscience/cdk/tools/efgf/app/ErtlFunctionalGroupsFinderPerformanceSnapshotApp.java +++ b/src/main/java/org/openscience/cdk/tools/efgf/app/ErtlFunctionalGroupsFinderPerformanceSnapshotApp.java @@ -52,6 +52,7 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import java.util.Random; import java.util.Set; import java.util.concurrent.ExecutorService; @@ -138,6 +139,7 @@ public ErtlFunctionalGroupsFinderPerformanceSnapshotApp(String[] anArgs) throws tmpExceptionsPrintWriter.println(); tmpExceptionsPrintWriter.flush(); tmpExceptionsPrintWriter.close(); + ExecutorService executor = null; try { if (anArgs.length != 2) { throw new IllegalArgumentException("Two arguments (a file name and the number of threads to use) are required."); @@ -211,13 +213,14 @@ public ErtlFunctionalGroupsFinderPerformanceSnapshotApp(String[] anArgs) throws IAtomContainer[] tmpMoleculesArrayForThread = Arrays.copyOfRange(this.moleculesArray, i, i + tmpNumberOfMoleculesPerThread); tmpListOfThreads.add(new ExtractFunctionalGroupsTask(tmpMoleculesArrayForThread)); } - ExecutorService executor = Executors.newFixedThreadPool(this.numberOfThreadsToUse); + executor = Executors.newFixedThreadPool(this.numberOfThreadsToUse); List> tmpFuturesList = new LinkedList<>(); long tmpStartTime = System.currentTimeMillis(); try { tmpFuturesList = executor.invokeAll(tmpListOfThreads); } catch (Exception ex) { this.appendToLogfile(ex); + throw ex; } long tmpEndTime = System.currentTimeMillis(); tmpResultsPrintWriter.println("Divided molecules onto " + this.numberOfThreadsToUse @@ -230,13 +233,17 @@ public ErtlFunctionalGroupsFinderPerformanceSnapshotApp(String[] anArgs) throws } tmpResultsPrintWriter.println(tmpExceptionsCounter + " molecules produced an exception."); tmpResultsPrintWriter.flush(); - executor.shutdown(); + executor.close(); tmpResultsPrintWriter.println(); tmpResultsPrintWriter.flush(); tmpResultsPrintWriter.close(); } catch (Exception anException) { this.appendToLogfile(anException); anException.printStackTrace(System.err); + if (!Objects.isNull(executor)) { + executor.close(); + } + Thread.currentThread().interrupt(); System.exit(1); } } @@ -265,7 +272,9 @@ private IAtomContainer applyFiltersAndPreprocessing(IAtomContainer aMolecule) th tmpBiggestFragment = tmpFragment; } } - aMolecule = tmpBiggestFragment; + if (!Objects.isNull(tmpBiggestFragment)) { + aMolecule = tmpBiggestFragment; + } } for (IAtom tmpAtom : aMolecule.atoms()) { if (!this.nonMetallicAtomicNumbersSet.contains(tmpAtom.getAtomicNumber())) {