]> source.dussan.org Git - jgit.git/commitdiff
refactor: simplify collection.toArray() 94/127094/3
authorMichael Keppler <Michael.Keppler@gmx.de>
Mon, 6 Aug 2018 12:11:26 +0000 (14:11 +0200)
committerJonathan Nieder <jrn@google.com>
Thu, 23 Aug 2018 05:34:39 +0000 (01:34 -0400)
On recent VMs, collection.toArray(new T[0]) is faster than
collection.toArray(new T[collection.size()]). Since it is also more
readable, it should now be the preferred way of collection to array
conversion.

https://shipilev.net/blog/2016/arrays-wisdom-ancients/

Change-Id: I80388532fb4b2b0663ee1fe8baa94f5df55c8442
Signed-off-by: Michael Keppler <Michael.Keppler@gmx.de>
25 files changed:
org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java
org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ServletBinderImpl.java
org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AppServer.java
org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/LfsPrePushHook.java
org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CommandCatalog.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Describe.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Remote.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/opt/CmdLineParser.java
org.eclipse.jgit.test/src/org/eclipse/jgit/events/ChangeRecorder.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ReflogCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathFilterGroupTest.java
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchLeader.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/LocalReplica.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/LocalCachedPack.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/BaseSearch.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java
org.eclipse.jgit/src/org/eclipse/jgit/revplot/PlotWalk.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/CredentialsProvider.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/OpenSshConfig.java
org.eclipse.jgit/src/org/eclipse/jgit/util/FS_Win32.java

index 195dff96135b04744737312957f161abbf638a03..75611eec95a5fe799ded699dcba42d8f40cc12e3 100644 (file)
@@ -77,7 +77,7 @@ abstract class SmartServiceInfoRefs implements Filter {
 
        SmartServiceInfoRefs(String service, List<Filter> filters) {
                this.svc = service;
-               this.filters = filters.toArray(new Filter[filters.size()]);
+               this.filters = filters.toArray(new Filter[0]);
        }
 
        /** {@inheritDoc} */
index 18650eb95bbecf6a33ce5ea714ec941300857fde..f16749775f98e239672d1a185c3b59a5160b7c73 100644 (file)
@@ -98,7 +98,7 @@ abstract class ServletBinderImpl implements ServletBinder {
         * @return the configured filters; zero-length array if none.
         */
        protected Filter[] getFilters() {
-               return filters.toArray(new Filter[filters.size()]);
+               return filters.toArray(new Filter[0]);
        }
 
        /** @return the pipeline that matches and executes this chain. */
index 9309fe7071fc7456e29ed11955ef4715076c75d7..9aef086b78a2b74044c3b57e963b92a537516739 100644 (file)
@@ -349,7 +349,7 @@ public class AppServer {
                sec.setAuthenticator(authType);
                sec.setLoginService(users);
                sec.setConstraintMappings(
-                               mappings.toArray(new ConstraintMapping[mappings.size()]));
+                               mappings.toArray(new ConstraintMapping[0]));
                sec.setHandler(ctx);
 
                contexts.removeHandler(ctx);
index de4449f5e3a573eaa1eb8ebd8de35eb328b2ed5a..a20c1d96fe445466286b143369dd0dc364edc6a4 100644 (file)
@@ -201,7 +201,7 @@ public class LfsPrePushHook extends PrePushHook {
 
        private Map<String, LfsPointer> requestBatchUpload(HttpConnection api,
                        Set<LfsPointer> toPush) throws IOException {
-               LfsPointer[] res = toPush.toArray(new LfsPointer[toPush.size()]);
+               LfsPointer[] res = toPush.toArray(new LfsPointer[0]);
                Map<String, LfsPointer> oidStr2ptr = new HashMap<>();
                for (LfsPointer p : res) {
                        oidStr2ptr.put(p.getOid().name(), p);
index 44ad79defc17afa6f0b8598d12de05ba7bd24488..81875f11bc6bdf7c66a4749757ac3cab00563620 100644 (file)
@@ -230,7 +230,7 @@ public class CLIGitCommand extends Main {
                }
                if (r.length() > 0)
                        list.add(r.toString());
-               return list.toArray(new String[list.size()]);
+               return list.toArray(new String[0]);
        }
 
        public static class Result {
index afeb5ef6e41ea6ffa271c9fb15a6bd94d034e0a2..ecc00d37f5fa66f3e917ba4e20ecdc0454b16948 100644 (file)
@@ -544,7 +544,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
 
                byte[] result = CLIGitCommand.executeRaw(
                                "git archive --format=zip HEAD", db).outBytes();
-               assertArrayEquals(l.toArray(new String[l.size()]),
+               assertArrayEquals(l.toArray(new String[0]),
                                listZipEntries(result));
        }
 
@@ -564,7 +564,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
 
                byte[] result = CLIGitCommand.executeRaw(
                                "git archive --format=tar HEAD", db).outBytes();
-               assertArrayEquals(l.toArray(new String[l.size()]),
+               assertArrayEquals(l.toArray(new String[0]),
                                listTarEntries(result));
        }
 
@@ -697,7 +697,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
                        while ((e = in.getNextEntry()) != null)
                                l.add(e.getName());
                }
-               return l.toArray(new String[l.size()]);
+               return l.toArray(new String[0]);
        }
 
        private static Future<Object> writeAsync(OutputStream stream, byte[] data) {
@@ -730,7 +730,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
                                while ((line = reader.readLine()) != null)
                                        l.add(line);
 
-                               return l.toArray(new String[l.size()]);
+                               return l.toArray(new String[0]);
                        } finally {
                                writing.get();
                                proc.destroy();
@@ -754,7 +754,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
                        String line;
                        while ((line = reader.readLine()) != null)
                                l.add(line);
-                       return l.toArray(new String[l.size()]);
+                       return l.toArray(new String[0]);
                }
 
                // not found
@@ -774,7 +774,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
                                while ((line = reader.readLine()) != null)
                                        l.add(line);
 
-                               return l.toArray(new String[l.size()]);
+                               return l.toArray(new String[0]);
                        } finally {
                                writing.get();
                                proc.destroy();
index 5754d7c44f3347efee88699ee748ad57e58f143e..2b0e5a717d7be5e9146b0bd3ba1135395a559592 100644 (file)
@@ -114,7 +114,7 @@ public class CommandCatalog {
        }
 
        private static CommandRef[] toSortedArray(Collection<CommandRef> c) {
-               final CommandRef[] r = c.toArray(new CommandRef[c.size()]);
+               final CommandRef[] r = c.toArray(new CommandRef[0]);
                Arrays.sort(r, new Comparator<CommandRef>() {
                        @Override
                        public int compare(CommandRef o1, CommandRef o2) {
index f9c1cf5d388ff36a35191bd0592673b8a3d398d3..f91474859d9dc81286abed3dc0b87dbccc4431f2 100644 (file)
@@ -73,7 +73,7 @@ class Describe extends TextBuiltin {
                        if (tree != null)
                                cmd.setTarget(tree);
                        cmd.setLong(longDesc);
-                       cmd.setMatch(patterns.toArray(new String[patterns.size()]));
+                       cmd.setMatch(patterns.toArray(new String[0]));
                        String result = null;
                        try {
                                result = cmd.call();
index ac53de976798164d4f071a816ee7556a483724ed..6fa92d3b0be883bcf463229988e880a9ecdf55da 100644 (file)
@@ -285,7 +285,7 @@ public class Main {
                final TextBuiltin cmd = subcommand;
                init(cmd);
                try {
-                       cmd.execute(arguments.toArray(new String[arguments.size()]));
+                       cmd.execute(arguments.toArray(new String[0]));
                } finally {
                        if (cmd.outw != null) {
                                cmd.outw.flush();
index 3308e18f245227a476c6ecf91d8f7e21d2508cec..63eba15abcbc11a2149dee209455cde3646e8147 100644 (file)
@@ -130,7 +130,7 @@ class Remote extends TextBuiltin {
                                        fetchArgs.add(name);
                                }
 
-                               fetch.execute(fetchArgs.toArray(new String[fetchArgs.size()]));
+                               fetch.execute(fetchArgs.toArray(new String[0]));
 
                                // flush the streams
                                fetch.outw.flush();
index 5cc98ca8ac29b150f8753f830c9ae8444dd05937..b97aa5b7af825d82000bedc3cd4a061e4abab9d6 100644 (file)
@@ -177,7 +177,7 @@ public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
                }
 
                try {
-                       super.parseArgument(tmp.toArray(new String[tmp.size()]));
+                       super.parseArgument(tmp.toArray(new String[0]));
                } catch (Die e) {
                        if (!seenHelp) {
                                throw e;
index c5582a860119d8d90eae1e131d29248f92f66d59..228df35c764e993ab81c6552e1b330ef5158408e 100644 (file)
@@ -74,11 +74,11 @@ public class ChangeRecorder implements WorkingTreeModifiedListener {
        }
 
        private String[] getModified() {
-               return modified.toArray(new String[modified.size()]);
+               return modified.toArray(new String[0]);
        }
 
        private String[] getDeleted() {
-               return deleted.toArray(new String[deleted.size()]);
+               return deleted.toArray(new String[0]);
        }
 
        private void reset() {
index b07c7033f5299d66e7f5ba6fd6b6075b54fdf134..8922837139fef4b4106b1d01fdcfb37d797b75a8 100644 (file)
@@ -89,7 +89,7 @@ public class ReflogCommandTest extends RepositoryTestCase {
                Collection<ReflogEntry> reflog = git.reflog().call();
                assertNotNull(reflog);
                assertEquals(3, reflog.size());
-               ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[reflog.size()]);
+               ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
                assertEquals(reflogs[2].getComment(),
                                "commit (initial): Initial commit");
                assertEquals(reflogs[2].getNewId(), commit1.getId());
@@ -114,7 +114,7 @@ public class ReflogCommandTest extends RepositoryTestCase {
                                .setRef(Constants.R_HEADS + "b1").call();
                assertNotNull(reflog);
                assertEquals(2, reflog.size());
-               ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[reflog.size()]);
+               ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
                assertEquals(reflogs[0].getComment(), "commit: Removed file");
                assertEquals(reflogs[0].getNewId(), commit2.getId());
                assertEquals(reflogs[0].getOldId(), commit1.getId());
@@ -136,7 +136,7 @@ public class ReflogCommandTest extends RepositoryTestCase {
                Collection<ReflogEntry> reflog = git.reflog().call();
                assertNotNull(reflog);
                assertEquals(4, reflog.size());
-               ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[reflog.size()]);
+               ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
                assertEquals(reflogs[3].getComment(),
                                "commit (initial): Initial commit");
                assertEquals(reflogs[3].getNewId(), commit1.getId());
index 71a2f327c8414bfa13c8e51ede739cc794ffe83d..15d03d38e78850ac94f0a6644cec933b45dc54e8 100644 (file)
@@ -231,8 +231,8 @@ public class PathFilterGroupTest {
                        }
                }
 
-               String[] e = expect.toArray(new String[expect.size()]);
-               String[] a = actual.toArray(new String[actual.size()]);
+               String[] e = expect.toArray(new String[0]);
+               String[] a = actual.toArray(new String[0]);
                Arrays.sort(e);
                Arrays.sort(a);
                assertArrayEquals(e, a);
index bad71de8afa57a32aff60b4b48add79008b23f6e..ca1e3ab27517e287ffed10c1f35dc81ecdaa57fb 100644 (file)
@@ -513,7 +513,7 @@ public class DirCacheCheckout {
 
                        if (!conflicts.isEmpty()) {
                                if (failOnConflict)
-                                       throw new CheckoutConflictException(conflicts.toArray(new String[conflicts.size()]));
+                                       throw new CheckoutConflictException(conflicts.toArray(new String[0]));
                                else
                                        cleanUpConflicts();
                        }
index bc5ba3926bbd83bda3cad490efe278e08543073a..fa32b267df28c91922d9f23ab6ad2d7b3cfb0fd2 100644 (file)
@@ -274,8 +274,8 @@ public abstract class KetchLeader {
 
                lock.lock();
                try {
-                       voters = v.toArray(new KetchReplica[v.size()]);
-                       followers = f.toArray(new KetchReplica[f.size()]);
+                       voters = v.toArray(new KetchReplica[0]);
+                       followers = f.toArray(new KetchReplica[0]);
                        self = me;
                } finally {
                        lock.unlock();
index 7ddde6337f211408ab1a652eee83c22049c71732..6f1f5c5c2cd96009bd9b8cb7d88c35943e1a16c5 100644 (file)
@@ -209,7 +209,7 @@ public class LocalReplica extends KetchReplica {
                checkFailed(failed, accepted);
                checkFailed(failed, committed);
                if (!failed.isEmpty()) {
-                       String[] arr = failed.toArray(new String[failed.size()]);
+                       String[] arr = failed.toArray(new String[0]);
                        req.setRefs(refdb.exactRef(arr));
                }
        }
index 289d89d698aa1c57105c023d1c70f3d2c57ff053..e931c1f9e7118367743b1beb3c2ef8515a06bb9e 100644 (file)
@@ -62,13 +62,13 @@ class LocalCachedPack extends CachedPack {
 
        LocalCachedPack(ObjectDirectory odb, List<String> packNames) {
                this.odb = odb;
-               this.packNames = packNames.toArray(new String[packNames.size()]);
+               this.packNames = packNames.toArray(new String[0]);
        }
 
        LocalCachedPack(List<PackFile> packs) {
                odb = null;
                packNames = null;
-               this.packs = packs.toArray(new PackFile[packs.size()]);
+               this.packs = packs.toArray(new PackFile[0]);
        }
 
        /** {@inheritDoc} */
index 51c5702fc26e974f8f7235d5d2963bb2d4d0a73a..24723d850c837b43b0048c14b2f1fdea50c3dd02 100644 (file)
@@ -938,7 +938,7 @@ public class ObjectDirectory extends FileObjectDatabase {
                if (list.isEmpty())
                        return new PackList(snapshot, NO_PACKS.packs);
 
-               final PackFile[] r = list.toArray(new PackFile[list.size()]);
+               final PackFile[] r = list.toArray(new PackFile[0]);
                Arrays.sort(r, PackFile.SORT);
                return new PackList(snapshot, r);
        }
@@ -1030,7 +1030,7 @@ public class ObjectDirectory extends FileObjectDatabase {
                                l.add(openAlternate(line));
                        }
                }
-               return l.toArray(new AlternateHandle[l.size()]);
+               return l.toArray(new AlternateHandle[0]);
        }
 
        private static BufferedReader open(File f)
index d231ccb997d07f26ec142ed83c1faea6a23fa28f..f42b3a5463eceea336f646e689c5e1e7841037f3 100644 (file)
@@ -91,7 +91,7 @@ class BaseSearch {
                        List<ObjectToPack> edges, ObjectReader or) {
                progress = countingMonitor;
                reader = or;
-               baseTrees = bases.toArray(new ObjectId[bases.size()]);
+               baseTrees = bases.toArray(new ObjectId[0]);
                objectsMap = objects;
                edgeObjects = edges;
 
index 2c7c6cb060f875a617a6fd1a1671a19ab7082cb9..d1cf1cd9aeca3516322f6b881c3e03a328768a5e 100644 (file)
@@ -283,7 +283,7 @@ public class BaseRepositoryBuilder<B extends BaseRepositoryBuilder, R extends Re
                final List<File> alts = alternateObjectDirectories;
                if (alts == null)
                        return null;
-               return alts.toArray(new File[alts.size()]);
+               return alts.toArray(new File[0]);
        }
 
        /**
index aff2a430d30fd2864baa98b8da070c474c2752f1..ee18fe7c2f21472c972c56071bcf0d3c0c9ed774 100644 (file)
@@ -177,7 +177,7 @@ public class PlotWalk extends RevWalk {
                if (list == null) {
                        return PlotCommit.NO_REFS;
                } else {
-                       Ref[] tags = list.toArray(new Ref[list.size()]);
+                       Ref[] tags = list.toArray(new Ref[0]);
                        Arrays.sort(tags, new PlotRefComparator());
                        return tags;
                }
index 811594620480477124588693983b3faf35d392db..f7a295d63d4ca25662a467547356bd2609d18fff 100644 (file)
@@ -155,7 +155,7 @@ public abstract class CredentialsProvider {
         */
        public boolean get(URIish uri, List<CredentialItem> items)
                        throws UnsupportedCredentialItem {
-               return get(uri, items.toArray(new CredentialItem[items.size()]));
+               return get(uri, items.toArray(new CredentialItem[0]));
        }
 
        /**
index f5ccdc8f1373a53cd3d64145f25b3af5322ca6cd..480055c2e41947127d3d8ca59de120d706a1db4d 100644 (file)
@@ -524,7 +524,7 @@ public class OpenSshConfig implements ConfigRepository {
                        if (values == null || values.isEmpty()) {
                                return new String[0];
                        }
-                       return values.toArray(new String[values.size()]);
+                       return values.toArray(new String[0]);
                }
 
                public void setValue(String key, String value) {
index 9f99e28557fd3ca8c43cc7eb53aa99463d85aa1e..98797dc64fff67ebf8d69888d358b356293c0d44 100644 (file)
@@ -169,7 +169,7 @@ public class FS_Win32 extends FS {
                if (result.isEmpty()) {
                        return NO_ENTRIES;
                }
-               return result.toArray(new Entry[result.size()]);
+               return result.toArray(new Entry[0]);
        }
 
        /** {@inheritDoc} */