This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git
The following commit(s) were added to refs/heads/master by this push: new 81ebe0d Use empty arrays instead of pre-sized ones (#188) 81ebe0d is described below commit 81ebe0dd0d94f0d5ec9185ddb6c453c1d72a52bc Author: Elliotte Rusty Harold <elh...@users.noreply.github.com> AuthorDate: Thu Mar 6 20:58:24 2025 +0000 Use empty arrays instead of pre-sized ones (#188) * Remove moot code --- .../maven/shared/utils/io/DirectoryScanner.java | 65 +++++++++++----------- .../apache/maven/shared/utils/io/FileUtils.java | 4 +- .../org/apache/maven/shared/utils/CaseTest.java | 5 +- .../ReflectionValueExtractorTest.java | 3 +- 4 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java b/src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java index 4eb9f31..aad7a01 100644 --- a/src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java +++ b/src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java @@ -400,12 +400,12 @@ public class DirectoryScanner { setupDefaultFilters(); setupMatchPatterns(); - filesIncluded = new ArrayList<String>(); - filesNotIncluded = new ArrayList<String>(); - filesExcluded = new ArrayList<String>(); - dirsIncluded = new ArrayList<String>(); - dirsNotIncluded = new ArrayList<String>(); - dirsExcluded = new ArrayList<String>(); + filesIncluded = new ArrayList<>(); + filesNotIncluded = new ArrayList<>(); + filesExcluded = new ArrayList<>(); + dirsIncluded = new ArrayList<>(); + dirsNotIncluded = new ArrayList<>(); + dirsExcluded = new ArrayList<>(); scanAction = ScanConductor.ScanAction.CONTINUE; if (isIncluded("")) { @@ -450,11 +450,11 @@ public class DirectoryScanner { */ public DirectoryScanResult diffIncludedFiles(String... oldFiles) { if (filesIncluded == null) { - // perform a scan if the directory didn't got scanned yet + // perform a scan if the directory didn't get scanned yet scan(); } - return diffFiles(oldFiles, filesIncluded.toArray(new String[filesIncluded.size()])); + return diffFiles(oldFiles, filesIncluded.toArray(new String[0])); } /** @@ -466,8 +466,8 @@ public class DirectoryScanner { Set<String> oldFileSet = arrayAsHashSet(oldFiles); Set<String> newFileSet = arrayAsHashSet(newFiles); - List<String> added = new ArrayList<String>(); - List<String> removed = new ArrayList<String>(); + List<String> added = new ArrayList<>(); + List<String> removed = new ArrayList<>(); for (String oldFile : oldFileSet) { if (!newFileSet.contains(oldFile)) { @@ -481,8 +481,8 @@ public class DirectoryScanner { } } - String[] filesAdded = added.toArray(new String[added.size()]); - String[] filesRemoved = removed.toArray(new String[removed.size()]); + String[] filesAdded = added.toArray(new String[0]); + String[] filesRemoved = removed.toArray(new String[0]); return new DirectoryScanResult(filesAdded, filesRemoved); } @@ -499,7 +499,7 @@ public class DirectoryScanner { return Collections.emptySet(); } - Set<T> set = new HashSet<T>(array.length); + Set<T> set = new HashSet<>(array.length); Collections.addAll(set, array); return set; @@ -512,14 +512,14 @@ public class DirectoryScanner { * <p/> * Returns immediately if a slow scan has already been completed. */ - void slowScan() { + private void slowScan() { if (haveSlowResults) { return; } - final String[] excl = dirsExcluded.toArray(new String[dirsExcluded.size()]); + final String[] excl = dirsExcluded.toArray(new String[0]); - final String[] notIncl = dirsNotIncluded.toArray(new String[dirsNotIncluded.size()]); + final String[] notIncl = dirsNotIncluded.toArray(new String[0]); for (String anExcl : excl) { if (!couldHoldIncluded(anExcl)) { @@ -541,10 +541,10 @@ public class DirectoryScanner { * collections, based on the matching of includes, excludes, and the selectors. When a directory is found, it is * scanned recursively. * - * @param dir The directory to scan. Must not be <code>null</code>. - * @param vpath The path relative to the base directory (needed to prevent problems with an absolute path when using + * @param dir the directory to scan. Must not be <code>null</code>. + * @param vpath the path relative to the base directory (needed to prevent problems with an absolute path when using * dir). Must not be <code>null</code>. - * @param fast Whether or not this call is part of a fast scan. + * @param fast whether this call is part of a fast scan * @see #filesIncluded * @see #filesNotIncluded * @see #filesExcluded @@ -553,7 +553,7 @@ public class DirectoryScanner { * @see #dirsExcluded * @see #slowScan */ - void scandir(@Nonnull final File dir, @Nonnull final String vpath, final boolean fast) { + private void scandir(@Nonnull final File dir, @Nonnull final String vpath, final boolean fast) { String[] newfiles = dir.list(); if (newfiles == null) { @@ -663,7 +663,7 @@ public class DirectoryScanner { } private String[] doNotFollowSymbolicLinks(final File dir, final String vpath, final String[] newfiles) { - final List<String> noLinks = new ArrayList<String>(); + final List<String> noLinks = new ArrayList<>(); for (final String newfile : newfiles) { if (Files.isSymbolicLink(dir.toPath())) { final String name = vpath + newfile; @@ -677,7 +677,7 @@ public class DirectoryScanner { noLinks.add(newfile); } } - return noLinks.toArray(new String[noLinks.size()]); + return noLinks.toArray(new String[0]); } /** @@ -687,7 +687,7 @@ public class DirectoryScanner { * @return <code>true</code> when the name matches at least one include pattern, or <code>false</code> * otherwise. */ - boolean isIncluded(final String name) { + private boolean isIncluded(final String name) { return includesPatterns.matches(name, isCaseSensitive); } @@ -698,7 +698,7 @@ public class DirectoryScanner { * @return <code>true</code> when the name matches against the start of at least one include pattern, or * <code>false</code> otherwise. */ - boolean couldHoldIncluded(@Nonnull final String name) { + private boolean couldHoldIncluded(@Nonnull final String name) { return includesPatterns.matchesPatternStart(name, isCaseSensitive); } @@ -709,7 +709,7 @@ public class DirectoryScanner { * @return <code>true</code> when the name matches against at least one exclude pattern, or <code>false</code> * otherwise. */ - boolean isExcluded(@Nonnull final String name) { + private boolean isExcluded(@Nonnull final String name) { return excludesPatterns.matches(name, isCaseSensitive); } @@ -726,7 +726,7 @@ public class DirectoryScanner { if (filesIncluded == null) { return new String[0]; } - return filesIncluded.toArray(new String[filesIncluded.size()]); + return filesIncluded.toArray(new String[0]); } /** @@ -738,7 +738,7 @@ public class DirectoryScanner { */ public String[] getNotIncludedFiles() { slowScan(); - return filesNotIncluded.toArray(new String[filesNotIncluded.size()]); + return filesNotIncluded.toArray(new String[0]); } /** @@ -752,7 +752,7 @@ public class DirectoryScanner { */ public String[] getExcludedFiles() { slowScan(); - return filesExcluded.toArray(new String[filesExcluded.size()]); + return filesExcluded.toArray(new String[0]); } /** @@ -765,7 +765,7 @@ public class DirectoryScanner { */ @Deprecated public String[] getIncludedDirectories() { - return dirsIncluded.toArray(new String[dirsIncluded.size()]); + return dirsIncluded.toArray(new String[0]); } /** @@ -777,7 +777,7 @@ public class DirectoryScanner { */ public String[] getNotIncludedDirectories() { slowScan(); - return dirsNotIncluded.toArray(new String[dirsNotIncluded.size()]); + return dirsNotIncluded.toArray(new String[0]); } /** @@ -791,7 +791,7 @@ public class DirectoryScanner { */ public String[] getExcludedDirectories() { slowScan(); - return dirsExcluded.toArray(new String[dirsExcluded.size()]); + return dirsExcluded.toArray(new String[0]); } /** @@ -799,8 +799,7 @@ public class DirectoryScanner { */ public void addDefaultExcludes() { final int excludesLength = excludes == null ? 0 : excludes.length; - String[] newExcludes; - newExcludes = new String[excludesLength + DEFAULTEXCLUDES.length]; + String[] newExcludes = new String[excludesLength + DEFAULTEXCLUDES.length]; if (excludesLength > 0) { System.arraycopy(excludes, 0, newExcludes, 0, excludesLength); } diff --git a/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java b/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java index b26f6ac..007b855 100644 --- a/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java +++ b/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java @@ -429,9 +429,7 @@ public class FileUtils { try (Writer writer = Files.newBufferedWriter(file.toPath(), charset)) { for (int i = 0; data != null && i < data.length; i++) { writer.write(data[i]); - if (i < data.length) { - writer.write("\n"); - } + writer.write("\n"); } } } diff --git a/src/test/java/org/apache/maven/shared/utils/CaseTest.java b/src/test/java/org/apache/maven/shared/utils/CaseTest.java index ff10886..9b2e5c0 100644 --- a/src/test/java/org/apache/maven/shared/utils/CaseTest.java +++ b/src/test/java/org/apache/maven/shared/utils/CaseTest.java @@ -139,11 +139,10 @@ public class CaseTest extends Assert { upper.toLowerCase(locale)); // check that toLowerCase on lower and toUpperCase on upper don't cause harm - assertEquals("'" + lower + "'.toLowerCase('" + locale.toString() + "')", lower, lower.toLowerCase(locale)); - assertEquals("'" + upper + "'.toUpperCase('" + locale.toString() + "')", upper, upper.toUpperCase(locale)); + assertEquals("'" + lower + "'.toLowerCase('" + locale + "')", lower, lower.toLowerCase(locale)); + assertEquals("'" + upper + "'.toUpperCase('" + locale + "')", upper, upper.toUpperCase(locale)); // check equalsIgnoreCase - assertTrue("'" + upper + "'.equalsIgnoreCase('" + lower + "')", upper.equalsIgnoreCase(lower)); assertTrue( "'" + upper + "'.equalsIgnoreCase('" + expectedToLowerCase + "')", upper.equalsIgnoreCase(expectedToLowerCase)); diff --git a/src/test/java/org/apache/maven/shared/utils/introspection/ReflectionValueExtractorTest.java b/src/test/java/org/apache/maven/shared/utils/introspection/ReflectionValueExtractorTest.java index 0e3a467..423f57f 100644 --- a/src/test/java/org/apache/maven/shared/utils/introspection/ReflectionValueExtractorTest.java +++ b/src/test/java/org/apache/maven/shared/utils/introspection/ReflectionValueExtractorTest.java @@ -389,8 +389,7 @@ public class ReflectionValueExtractorTest extends TestCase { public Map<String, Dependency> getDependenciesAsMap() { Map<String, Dependency> ret = new HashMap<>(); - for (Object o : getDependencies()) { - Dependency dep = (Dependency) o; + for (Dependency dep : getDependencies()) { ret.put(dep.getArtifactId(), dep); } return ret;