Ver código fonte

refactor: simplify collection.toArray()

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>
tags/v5.1.0.201808281540-m3
Michael Keppler 5 anos atrás
pai
commit
2fc00af44e
25 arquivos alterados com 37 adições e 37 exclusões
  1. 1
    1
      org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java
  2. 1
    1
      org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ServletBinderImpl.java
  3. 1
    1
      org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AppServer.java
  4. 1
    1
      org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/LfsPrePushHook.java
  5. 1
    1
      org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java
  6. 6
    6
      org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java
  7. 1
    1
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CommandCatalog.java
  8. 1
    1
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Describe.java
  9. 1
    1
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java
  10. 1
    1
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Remote.java
  11. 1
    1
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/opt/CmdLineParser.java
  12. 2
    2
      org.eclipse.jgit.test/src/org/eclipse/jgit/events/ChangeRecorder.java
  13. 3
    3
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ReflogCommandTest.java
  14. 2
    2
      org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathFilterGroupTest.java
  15. 1
    1
      org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java
  16. 2
    2
      org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchLeader.java
  17. 1
    1
      org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/LocalReplica.java
  18. 2
    2
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/LocalCachedPack.java
  19. 2
    2
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java
  20. 1
    1
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/BaseSearch.java
  21. 1
    1
      org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java
  22. 1
    1
      org.eclipse.jgit/src/org/eclipse/jgit/revplot/PlotWalk.java
  23. 1
    1
      org.eclipse.jgit/src/org/eclipse/jgit/transport/CredentialsProvider.java
  24. 1
    1
      org.eclipse.jgit/src/org/eclipse/jgit/transport/OpenSshConfig.java
  25. 1
    1
      org.eclipse.jgit/src/org/eclipse/jgit/util/FS_Win32.java

+ 1
- 1
org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java Ver arquivo



SmartServiceInfoRefs(String service, List<Filter> filters) { SmartServiceInfoRefs(String service, List<Filter> filters) {
this.svc = service; this.svc = service;
this.filters = filters.toArray(new Filter[filters.size()]);
this.filters = filters.toArray(new Filter[0]);
} }


/** {@inheritDoc} */ /** {@inheritDoc} */

+ 1
- 1
org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ServletBinderImpl.java Ver arquivo

* @return the configured filters; zero-length array if none. * @return the configured filters; zero-length array if none.
*/ */
protected Filter[] getFilters() { 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. */ /** @return the pipeline that matches and executes this chain. */

+ 1
- 1
org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AppServer.java Ver arquivo

sec.setAuthenticator(authType); sec.setAuthenticator(authType);
sec.setLoginService(users); sec.setLoginService(users);
sec.setConstraintMappings( sec.setConstraintMappings(
mappings.toArray(new ConstraintMapping[mappings.size()]));
mappings.toArray(new ConstraintMapping[0]));
sec.setHandler(ctx); sec.setHandler(ctx);


contexts.removeHandler(ctx); contexts.removeHandler(ctx);

+ 1
- 1
org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/LfsPrePushHook.java Ver arquivo



private Map<String, LfsPointer> requestBatchUpload(HttpConnection api, private Map<String, LfsPointer> requestBatchUpload(HttpConnection api,
Set<LfsPointer> toPush) throws IOException { 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<>(); Map<String, LfsPointer> oidStr2ptr = new HashMap<>();
for (LfsPointer p : res) { for (LfsPointer p : res) {
oidStr2ptr.put(p.getOid().name(), p); oidStr2ptr.put(p.getOid().name(), p);

+ 1
- 1
org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java Ver arquivo

} }
if (r.length() > 0) if (r.length() > 0)
list.add(r.toString()); list.add(r.toString());
return list.toArray(new String[list.size()]);
return list.toArray(new String[0]);
} }


public static class Result { public static class Result {

+ 6
- 6
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java Ver arquivo



byte[] result = CLIGitCommand.executeRaw( byte[] result = CLIGitCommand.executeRaw(
"git archive --format=zip HEAD", db).outBytes(); "git archive --format=zip HEAD", db).outBytes();
assertArrayEquals(l.toArray(new String[l.size()]),
assertArrayEquals(l.toArray(new String[0]),
listZipEntries(result)); listZipEntries(result));
} }




byte[] result = CLIGitCommand.executeRaw( byte[] result = CLIGitCommand.executeRaw(
"git archive --format=tar HEAD", db).outBytes(); "git archive --format=tar HEAD", db).outBytes();
assertArrayEquals(l.toArray(new String[l.size()]),
assertArrayEquals(l.toArray(new String[0]),
listTarEntries(result)); listTarEntries(result));
} }


while ((e = in.getNextEntry()) != null) while ((e = in.getNextEntry()) != null)
l.add(e.getName()); 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) { private static Future<Object> writeAsync(OutputStream stream, byte[] data) {
while ((line = reader.readLine()) != null) while ((line = reader.readLine()) != null)
l.add(line); l.add(line);


return l.toArray(new String[l.size()]);
return l.toArray(new String[0]);
} finally { } finally {
writing.get(); writing.get();
proc.destroy(); proc.destroy();
String line; String line;
while ((line = reader.readLine()) != null) while ((line = reader.readLine()) != null)
l.add(line); l.add(line);
return l.toArray(new String[l.size()]);
return l.toArray(new String[0]);
} }


// not found // not found
while ((line = reader.readLine()) != null) while ((line = reader.readLine()) != null)
l.add(line); l.add(line);


return l.toArray(new String[l.size()]);
return l.toArray(new String[0]);
} finally { } finally {
writing.get(); writing.get();
proc.destroy(); proc.destroy();

+ 1
- 1
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CommandCatalog.java Ver arquivo

} }


private static CommandRef[] toSortedArray(Collection<CommandRef> c) { 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>() { Arrays.sort(r, new Comparator<CommandRef>() {
@Override @Override
public int compare(CommandRef o1, CommandRef o2) { public int compare(CommandRef o1, CommandRef o2) {

+ 1
- 1
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Describe.java Ver arquivo

if (tree != null) if (tree != null)
cmd.setTarget(tree); cmd.setTarget(tree);
cmd.setLong(longDesc); cmd.setLong(longDesc);
cmd.setMatch(patterns.toArray(new String[patterns.size()]));
cmd.setMatch(patterns.toArray(new String[0]));
String result = null; String result = null;
try { try {
result = cmd.call(); result = cmd.call();

+ 1
- 1
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java Ver arquivo

final TextBuiltin cmd = subcommand; final TextBuiltin cmd = subcommand;
init(cmd); init(cmd);
try { try {
cmd.execute(arguments.toArray(new String[arguments.size()]));
cmd.execute(arguments.toArray(new String[0]));
} finally { } finally {
if (cmd.outw != null) { if (cmd.outw != null) {
cmd.outw.flush(); cmd.outw.flush();

+ 1
- 1
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Remote.java Ver arquivo

fetchArgs.add(name); fetchArgs.add(name);
} }


fetch.execute(fetchArgs.toArray(new String[fetchArgs.size()]));
fetch.execute(fetchArgs.toArray(new String[0]));


// flush the streams // flush the streams
fetch.outw.flush(); fetch.outw.flush();

+ 1
- 1
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/opt/CmdLineParser.java Ver arquivo

} }


try { try {
super.parseArgument(tmp.toArray(new String[tmp.size()]));
super.parseArgument(tmp.toArray(new String[0]));
} catch (Die e) { } catch (Die e) {
if (!seenHelp) { if (!seenHelp) {
throw e; throw e;

+ 2
- 2
org.eclipse.jgit.test/src/org/eclipse/jgit/events/ChangeRecorder.java Ver arquivo

} }


private String[] getModified() { private String[] getModified() {
return modified.toArray(new String[modified.size()]);
return modified.toArray(new String[0]);
} }


private String[] getDeleted() { private String[] getDeleted() {
return deleted.toArray(new String[deleted.size()]);
return deleted.toArray(new String[0]);
} }


private void reset() { private void reset() {

+ 3
- 3
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ReflogCommandTest.java Ver arquivo

Collection<ReflogEntry> reflog = git.reflog().call(); Collection<ReflogEntry> reflog = git.reflog().call();
assertNotNull(reflog); assertNotNull(reflog);
assertEquals(3, reflog.size()); assertEquals(3, reflog.size());
ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[reflog.size()]);
ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
assertEquals(reflogs[2].getComment(), assertEquals(reflogs[2].getComment(),
"commit (initial): Initial commit"); "commit (initial): Initial commit");
assertEquals(reflogs[2].getNewId(), commit1.getId()); assertEquals(reflogs[2].getNewId(), commit1.getId());
.setRef(Constants.R_HEADS + "b1").call(); .setRef(Constants.R_HEADS + "b1").call();
assertNotNull(reflog); assertNotNull(reflog);
assertEquals(2, reflog.size()); 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].getComment(), "commit: Removed file");
assertEquals(reflogs[0].getNewId(), commit2.getId()); assertEquals(reflogs[0].getNewId(), commit2.getId());
assertEquals(reflogs[0].getOldId(), commit1.getId()); assertEquals(reflogs[0].getOldId(), commit1.getId());
Collection<ReflogEntry> reflog = git.reflog().call(); Collection<ReflogEntry> reflog = git.reflog().call();
assertNotNull(reflog); assertNotNull(reflog);
assertEquals(4, reflog.size()); assertEquals(4, reflog.size());
ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[reflog.size()]);
ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
assertEquals(reflogs[3].getComment(), assertEquals(reflogs[3].getComment(),
"commit (initial): Initial commit"); "commit (initial): Initial commit");
assertEquals(reflogs[3].getNewId(), commit1.getId()); assertEquals(reflogs[3].getNewId(), commit1.getId());

+ 2
- 2
org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathFilterGroupTest.java Ver arquivo

} }
} }


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(e);
Arrays.sort(a); Arrays.sort(a);
assertArrayEquals(e, a); assertArrayEquals(e, a);

+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java Ver arquivo



if (!conflicts.isEmpty()) { if (!conflicts.isEmpty()) {
if (failOnConflict) if (failOnConflict)
throw new CheckoutConflictException(conflicts.toArray(new String[conflicts.size()]));
throw new CheckoutConflictException(conflicts.toArray(new String[0]));
else else
cleanUpConflicts(); cleanUpConflicts();
} }

+ 2
- 2
org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/KetchLeader.java Ver arquivo



lock.lock(); lock.lock();
try { 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; self = me;
} finally { } finally {
lock.unlock(); lock.unlock();

+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/internal/ketch/LocalReplica.java Ver arquivo

checkFailed(failed, accepted); checkFailed(failed, accepted);
checkFailed(failed, committed); checkFailed(failed, committed);
if (!failed.isEmpty()) { if (!failed.isEmpty()) {
String[] arr = failed.toArray(new String[failed.size()]);
String[] arr = failed.toArray(new String[0]);
req.setRefs(refdb.exactRef(arr)); req.setRefs(refdb.exactRef(arr));
} }
} }

+ 2
- 2
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/LocalCachedPack.java Ver arquivo



LocalCachedPack(ObjectDirectory odb, List<String> packNames) { LocalCachedPack(ObjectDirectory odb, List<String> packNames) {
this.odb = odb; this.odb = odb;
this.packNames = packNames.toArray(new String[packNames.size()]);
this.packNames = packNames.toArray(new String[0]);
} }


LocalCachedPack(List<PackFile> packs) { LocalCachedPack(List<PackFile> packs) {
odb = null; odb = null;
packNames = null; packNames = null;
this.packs = packs.toArray(new PackFile[packs.size()]);
this.packs = packs.toArray(new PackFile[0]);
} }


/** {@inheritDoc} */ /** {@inheritDoc} */

+ 2
- 2
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java Ver arquivo

if (list.isEmpty()) if (list.isEmpty())
return new PackList(snapshot, NO_PACKS.packs); 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); Arrays.sort(r, PackFile.SORT);
return new PackList(snapshot, r); return new PackList(snapshot, r);
} }
l.add(openAlternate(line)); l.add(openAlternate(line));
} }
} }
return l.toArray(new AlternateHandle[l.size()]);
return l.toArray(new AlternateHandle[0]);
} }


private static BufferedReader open(File f) private static BufferedReader open(File f)

+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/BaseSearch.java Ver arquivo

List<ObjectToPack> edges, ObjectReader or) { List<ObjectToPack> edges, ObjectReader or) {
progress = countingMonitor; progress = countingMonitor;
reader = or; reader = or;
baseTrees = bases.toArray(new ObjectId[bases.size()]);
baseTrees = bases.toArray(new ObjectId[0]);
objectsMap = objects; objectsMap = objects;
edgeObjects = edges; edgeObjects = edges;



+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java Ver arquivo

final List<File> alts = alternateObjectDirectories; final List<File> alts = alternateObjectDirectories;
if (alts == null) if (alts == null)
return null; return null;
return alts.toArray(new File[alts.size()]);
return alts.toArray(new File[0]);
} }


/** /**

+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/revplot/PlotWalk.java Ver arquivo

if (list == null) { if (list == null) {
return PlotCommit.NO_REFS; return PlotCommit.NO_REFS;
} else { } else {
Ref[] tags = list.toArray(new Ref[list.size()]);
Ref[] tags = list.toArray(new Ref[0]);
Arrays.sort(tags, new PlotRefComparator()); Arrays.sort(tags, new PlotRefComparator());
return tags; return tags;
} }

+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/transport/CredentialsProvider.java Ver arquivo

*/ */
public boolean get(URIish uri, List<CredentialItem> items) public boolean get(URIish uri, List<CredentialItem> items)
throws UnsupportedCredentialItem { throws UnsupportedCredentialItem {
return get(uri, items.toArray(new CredentialItem[items.size()]));
return get(uri, items.toArray(new CredentialItem[0]));
} }


/** /**

+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/transport/OpenSshConfig.java Ver arquivo

if (values == null || values.isEmpty()) { if (values == null || values.isEmpty()) {
return new String[0]; return new String[0];
} }
return values.toArray(new String[values.size()]);
return values.toArray(new String[0]);
} }


public void setValue(String key, String value) { public void setValue(String key, String value) {

+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/util/FS_Win32.java Ver arquivo

if (result.isEmpty()) { if (result.isEmpty()) {
return NO_ENTRIES; return NO_ENTRIES;
} }
return result.toArray(new Entry[result.size()]);
return result.toArray(new Entry[0]);
} }


/** {@inheritDoc} */ /** {@inheritDoc} */

Carregando…
Cancelar
Salvar