From 6e03645ad80adf71527ca18f937db4b8e34b7f13 Mon Sep 17 00:00:00 2001 From: Carsten Hammer Date: Sat, 6 Apr 2019 19:16:36 +0200 Subject: [PATCH] Use jdk 5 for loop Replace simple uses of Iterator with a corresponding for-loop. Also add missing braces on loops as necessary. Change-Id: I708d82acdf194787e3353699c07244c5ac3de189 Signed-off-by: Carsten Hammer Signed-off-by: Matthias Sohn --- .../junit/LocalDiskRepositoryTestCase.java | 17 ++++---- .../jgit/dircache/DirCacheBasicTest.java | 10 +++-- .../dircache/DirCacheBuilderIteratorTest.java | 5 ++- .../jgit/dircache/DirCacheBuilderTest.java | 10 +++-- .../jgit/dircache/DirCacheFindTest.java | 5 ++- .../jgit/dircache/DirCacheIteratorTest.java | 40 +++++++++++-------- .../jgit/dircache/DirCacheTreeTest.java | 15 ++++--- .../jgit/api/SubmoduleDeinitCommand.java | 4 +- .../internal/storage/dfs/DeltaBaseCache.java | 4 +- .../internal/storage/file/RefDirectory.java | 6 ++- .../jgit/patch/CombinedHunkHeader.java | 13 +++--- .../jgit/revplot/AbstractPlotRenderer.java | 20 +++++----- .../jgit/treewalk/NameConflictTreeWalk.java | 6 +-- .../org/eclipse/jgit/treewalk/TreeWalk.java | 12 ++---- 14 files changed, 91 insertions(+), 76 deletions(-) diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java index 5bac342b0..f93424ca1 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java @@ -235,17 +235,18 @@ public abstract class LocalDiskRepositoryTestCase { if (!dir.exists()) return silent; final File[] ls = dir.listFiles(); - if (ls != null) - for (int k = 0; k < ls.length; k++) { - final File e = ls[k]; - if (e.isDirectory()) - silent = recursiveDelete(e, silent, failOnError); - else if (!e.delete()) { - if (!silent) - reportDeleteFailure(failOnError, e); + if (ls != null) { + for (File f : ls) { + if (f.isDirectory()) { + silent = recursiveDelete(f, silent, failOnError); + } else if (!f.delete()) { + if (!silent) { + reportDeleteFailure(failOnError, f); + } silent = !failOnError; } } + } if (!dir.delete()) { if (!silent) reportDeleteFailure(failOnError, dir); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBasicTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBasicTest.java index 014406eb9..972dc6015 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBasicTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBasicTest.java @@ -205,8 +205,9 @@ public class DirCacheBasicTest extends RepositoryTestCase { } final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); assertFalse(dc.hasUnmergedPaths()); @@ -229,8 +230,9 @@ public class DirCacheBasicTest extends RepositoryTestCase { ents[2].setFileMode(FileMode.REGULAR_FILE); final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); assertTrue(dc.hasUnmergedPaths()); } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBuilderIteratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBuilderIteratorTest.java index 3598f3a4a..6020a748a 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBuilderIteratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBuilderIteratorTest.java @@ -71,8 +71,9 @@ public class DirCacheBuilderIteratorTest extends RepositoryTestCase { } { final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBuilderTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBuilderTest.java index b6291bfca..b284771e5 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBuilderTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBuilderTest.java @@ -301,8 +301,9 @@ public class DirCacheBuilderTest extends RepositoryTestCase { } final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); assertEquals(paths.length, dc.getEntryCount()); @@ -351,8 +352,9 @@ public class DirCacheBuilderTest extends RepositoryTestCase { } { final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); } assertEquals(paths.length, dc.getEntryCount()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheFindTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheFindTest.java index 3b8c6ee7b..7fc28018f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheFindTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheFindTest.java @@ -66,8 +66,9 @@ public class DirCacheFindTest extends RepositoryTestCase { final int aLast = 3; final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); assertEquals(paths.length, dc.getEntryCount()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java index 82565fcee..bc99aeef2 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java @@ -94,8 +94,9 @@ public class DirCacheIteratorTest extends RepositoryTestCase { } final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); final DirCacheIterator i = new DirCacheIterator(dc); @@ -121,8 +122,9 @@ public class DirCacheIteratorTest extends RepositoryTestCase { } final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); final DirCacheIterator i = new DirCacheIterator(dc); @@ -154,8 +156,9 @@ public class DirCacheIteratorTest extends RepositoryTestCase { } final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); final String[] expPaths = { "a-", "a", "a0b" }; @@ -200,8 +203,9 @@ public class DirCacheIteratorTest extends RepositoryTestCase { } final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); final DirCacheIterator i = new DirCacheIterator(dc); @@ -236,8 +240,9 @@ public class DirCacheIteratorTest extends RepositoryTestCase { } final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); try (TreeWalk tw = new TreeWalk(db)) { @@ -271,8 +276,9 @@ public class DirCacheIteratorTest extends RepositoryTestCase { } final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); DirCacheIterator dci = new DirCacheIterator(dc); @@ -365,8 +371,9 @@ public class DirCacheIteratorTest extends RepositoryTestCase { } final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); DirCacheIterator dci = new DirCacheIterator(dc); @@ -398,8 +405,9 @@ public class DirCacheIteratorTest extends RepositoryTestCase { } final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); try (TreeWalk tw = new TreeWalk(db)) { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheTreeTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheTreeTest.java index f662e2660..513db7532 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheTreeTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheTreeTest.java @@ -102,8 +102,9 @@ public class DirCacheTreeTest extends RepositoryTestCase { final int aLast = 3; final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); assertNull(dc.getCacheTree(false)); @@ -142,8 +143,9 @@ public class DirCacheTreeTest extends RepositoryTestCase { final int acLast = 3; final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.finish(); assertNull(dc.getCacheTree(false)); @@ -198,8 +200,9 @@ public class DirCacheTreeTest extends RepositoryTestCase { } final DirCacheBuilder b = dc.builder(); - for (int i = 0; i < ents.length; i++) - b.add(ents[i]); + for (DirCacheEntry ent : ents) { + b.add(ent); + } b.commit(); DirCache read = db.readDirCache(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleDeinitCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleDeinitCommand.java index 5a0528b0f..ebcea4b1d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleDeinitCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleDeinitCommand.java @@ -173,8 +173,8 @@ public class SubmoduleDeinitCommand } final File[] ls = dir.listFiles(); if (ls != null) { - for (int i = 0; i < ls.length; i++) { - FileUtils.delete(ls[i], RECURSIVE); + for (File f : ls) { + FileUtils.delete(f, RECURSIVE); } } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DeltaBaseCache.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DeltaBaseCache.java index bd4b4d23f..3b92deeae 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DeltaBaseCache.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DeltaBaseCache.java @@ -180,8 +180,8 @@ final class DeltaBaseCache { int getMemoryUsedByTableForTest() { int r = 0; - for (int i = 0; i < table.length; i++) { - for (Entry e = table[i]; e != null; e = e.tableNext) { + for (Entry t : table) { + for (Entry e = t; e != null; e = e.tableNext) { r += e.data.length; } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java index a4729bba4..35aca086b 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java @@ -1128,9 +1128,11 @@ public class RefDirectory extends RefDatabase { // check whether the found new ref is the an additional ref. These refs // should not go into looseRefs - for (int i = 0; i < additionalRefsNames.length; i++) - if (name.equals(additionalRefsNames[i])) + for (String additionalRefsName : additionalRefsNames) { + if (name.equals(additionalRefsName)) { return n; + } + } if (looseRefs.compareAndSet(curList, curList.add(idx, n))) modCnt.incrementAndGet(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/patch/CombinedHunkHeader.java b/org.eclipse.jgit/src/org/eclipse/jgit/patch/CombinedHunkHeader.java index d278132c6..74eec8120 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/patch/CombinedHunkHeader.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/patch/CombinedHunkHeader.java @@ -109,12 +109,13 @@ public class CombinedHunkHeader extends HunkHeader { final MutableInteger ptr = new MutableInteger(); ptr.value = nextLF(buf, startOffset, ' '); - for (int n = 0; n < old.length; n++) { - old[n].startLine = -parseBase10(buf, ptr.value, ptr); - if (buf[ptr.value] == ',') - old[n].lineCount = parseBase10(buf, ptr.value + 1, ptr); - else - old[n].lineCount = 1; + for (CombinedOldImage o : old) { + o.startLine = -parseBase10(buf, ptr.value, ptr); + if (buf[ptr.value] == ',') { + o.lineCount = parseBase10(buf, ptr.value + 1, ptr); + } else { + o.lineCount = 1; + } } newStartLine = parseBase10(buf, ptr.value + 1, ptr); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revplot/AbstractPlotRenderer.java b/org.eclipse.jgit/src/org/eclipse/jgit/revplot/AbstractPlotRenderer.java index 58e2106fe..b2f8d1192 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revplot/AbstractPlotRenderer.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revplot/AbstractPlotRenderer.java @@ -114,39 +114,39 @@ public abstract class AbstractPlotRenderer { drawLine(myColor, myLaneX, h, myLaneX, (h + dotSize) / 2, LINE_WIDTH); - for (int i = 0; i < commit.mergingLanes.length; i++) { - final TLane pLane = (TLane) commit.mergingLanes[i]; + for (PlotLane mergingLane : commit.mergingLanes) { + final TLane pLane = (TLane) mergingLane; final TColor pColor = laneColor(pLane); final int cx = laneC(pLane); - if (Math.abs(myLaneX - cx) > LANE_WIDTH) { final int ix; - if (myLaneX < cx) + if (myLaneX < cx) { ix = cx - LANE_WIDTH / 2; - else + } else { ix = cx + LANE_WIDTH / 2; + } drawLine(pColor, myLaneX, h / 2, ix, h / 2, LINE_WIDTH); drawLine(pColor, ix, h / 2, cx, h, LINE_WIDTH); } else drawLine(pColor, myLaneX, h / 2, cx, h, LINE_WIDTH); - maxCenter = Math.max(maxCenter, cx); } } if (commit.getChildCount() > 0) { - for (int i = 0; i < commit.forkingOffLanes.length; i++) { - final TLane childLane = (TLane) commit.forkingOffLanes[i]; + for (PlotLane forkingOffLane : commit.forkingOffLanes) { + final TLane childLane = (TLane) forkingOffLane; final TColor cColor = laneColor(childLane); final int cx = laneC(childLane); if (Math.abs(myLaneX - cx) > LANE_WIDTH) { final int ix; - if (myLaneX < cx) + if (myLaneX < cx) { ix = cx - LANE_WIDTH / 2; - else + } else { ix = cx + LANE_WIDTH / 2; + } drawLine(cColor, myLaneX, h / 2, ix, h / 2, LINE_WIDTH); drawLine(cColor, ix, h / 2, cx, 0, LINE_WIDTH); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/NameConflictTreeWalk.java b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/NameConflictTreeWalk.java index 61b130f93..2f8af78f8 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/NameConflictTreeWalk.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/NameConflictTreeWalk.java @@ -323,8 +323,7 @@ public class NameConflictTreeWalk extends TreeWalk { @Override void popEntriesEqual() throws CorruptObjectException { final AbstractTreeIterator ch = currentHead; - for (int i = 0; i < trees.length; i++) { - final AbstractTreeIterator t = trees[i]; + for (AbstractTreeIterator t : trees) { if (t.matches == ch) { if (t.matchShift == 0) t.next(1); @@ -343,8 +342,7 @@ public class NameConflictTreeWalk extends TreeWalk { @Override void skipEntriesEqual() throws CorruptObjectException { final AbstractTreeIterator ch = currentHead; - for (int i = 0; i < trees.length; i++) { - final AbstractTreeIterator t = trees[i]; + for (AbstractTreeIterator t : trees) { if (t.matches == ch) { if (t.matchShift == 0) t.skip(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java index 69303d6ee..65d851217 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java @@ -1338,8 +1338,7 @@ public class TreeWalk implements AutoCloseable, AttributesProvider { void popEntriesEqual() throws CorruptObjectException { final AbstractTreeIterator ch = currentHead; - for (int i = 0; i < trees.length; i++) { - final AbstractTreeIterator t = trees[i]; + for (AbstractTreeIterator t : trees) { if (t.matches == ch) { t.next(1); t.matches = null; @@ -1349,8 +1348,7 @@ public class TreeWalk implements AutoCloseable, AttributesProvider { void skipEntriesEqual() throws CorruptObjectException { final AbstractTreeIterator ch = currentHead; - for (int i = 0; i < trees.length; i++) { - final AbstractTreeIterator t = trees[i]; + for (AbstractTreeIterator t : trees) { if (t.matches == ch) { t.skip(); t.matches = null; @@ -1398,10 +1396,8 @@ public class TreeWalk implements AutoCloseable, AttributesProvider { * @param * a tree type. */ - public T getTree( - Class type) { - for (int i = 0; i < trees.length; i++) { - AbstractTreeIterator tree = trees[i]; + public T getTree(Class type) { + for (AbstractTreeIterator tree : trees) { if (type.isInstance(tree)) { return type.cast(tree); }