]> source.dussan.org Git - jgit.git/commitdiff
Replace chain of if statements with switch 64/140164/18
authorCarsten Hammer <carsten.hammer@t-online.de>
Tue, 21 May 2019 15:04:18 +0000 (17:04 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Sat, 14 Dec 2019 19:42:03 +0000 (20:42 +0100)
and switch over strings where possible. Sometimes if statements are
chained and form a series of comparisons against constants. Using switch
statements improves readability.

Bug: 545856
Change-Id: Iacb78956ee5c20db4d793e6b668508ec67466606
Signed-off-by: Carsten Hammer <carsten.hammer@t-online.de>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
20 files changed:
org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/AmazonS3Client.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Remote.java
org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/transport/sshd/JGitClientSession.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollectorTest.java
org.eclipse.jgit.ui/src/org/eclipse/jgit/awtui/AwtCredentialsProvider.java
org.eclipse.jgit/src/org/eclipse/jgit/blame/ReverseWalk.java
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/ManifestParser.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java
org.eclipse.jgit/src/org/eclipse/jgit/revplot/PlotCommit.java
org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/AdvertiseRefsHookChain.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/PostReceiveHookChain.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/PreReceiveHookChain.java
org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java
org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java
org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java
org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFOutputStream.java
org.eclipse.jgit/src/org/eclipse/jgit/util/sha1/SHA1.java

index befb7f62aca03c464410433445f8796e5c01d3b6..1d9dca73d093913b07b52b580ef73a5c30beff76 100644 (file)
@@ -190,11 +190,12 @@ public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase {
        }
 
        protected String cmdString(String... cmds) {
-               if (cmds.length == 0)
+               switch (cmds.length) {
+               case 0:
                        return "";
-               else if (cmds.length == 1)
+               case 1:
                        return "\"" + escapeJava(cmds[0]) + "\"";
-               else {
+               default:
                        StringBuilder sb = new StringBuilder(cmdString(cmds[0]));
                        for (int i=1; i<cmds.length; i++) {
                                sb.append(", ");
index f4f8d8f50fbbfd7e7fb80714cca9d18c836e54d6..9dd4eb5f1d91603cb46cf7761a549feb3dbe1031 100644 (file)
@@ -86,7 +86,11 @@ class AmazonS3Client extends TextBuiltin {
        protected void run() throws Exception {
                final AmazonS3 s3 = new AmazonS3(properties());
 
-               if ("get".equals(op)) { //$NON-NLS-1$
+               if (op == null) {
+                       throw die(MessageFormat.format(CLIText.get().unsupportedOperation, op));
+               }
+               switch (op) {
+               case "get": //$NON-NLS-1$
                        final URLConnection c = s3.get(bucket, key);
                        int len = c.getContentLength();
                        try (InputStream in = c.getInputStream()) {
@@ -103,22 +107,25 @@ class AmazonS3Client extends TextBuiltin {
                                }
                                outs.flush();
                        }
-
-               } else if ("ls".equals(op) || "list".equals(op)) { //$NON-NLS-1$//$NON-NLS-2$
+                       break;
+               case "ls": //$NON-NLS-1$
+               case "list": //$NON-NLS-1$
                        for (String k : s3.list(bucket, key))
                                outw.println(k);
-
-               } else if ("rm".equals(op) || "delete".equals(op)) { //$NON-NLS-1$ //$NON-NLS-2$
+                       break;
+               case "rm": //$NON-NLS-1$
+               case "delete": //$NON-NLS-1$
                        s3.delete(bucket, key);
-
-               } else if ("put".equals(op)) { //$NON-NLS-1$
+                       break;
+               case "put": //$NON-NLS-1$
                        try (OutputStream os = s3.beginPut(bucket, key, null, null)) {
                                final byte[] tmp = new byte[2048];
                                int n;
                                while ((n = ins.read(tmp)) > 0)
                                        os.write(tmp, 0, n);
                        }
-               } else {
+                       break;
+               default:
                        throw die(MessageFormat.format(CLIText.get().unsupportedOperation, op));
                }
        }
index 58138fa03b04c1f201b4a492ecc5362ef494803e..cdf95410126e379957c59db0c171c35d84bbe427 100644 (file)
@@ -93,33 +93,35 @@ class Remote extends TextBuiltin {
                                RemoteListCommand cmd = git.remoteList();
                                List<RemoteConfig> remotes = cmd.call();
                                print(remotes);
-                       } else if ("add".equals(command)) { //$NON-NLS-1$
-                               RemoteAddCommand cmd = git.remoteAdd();
-                               cmd.setName(name);
-                               cmd.setUri(new URIish(uri));
-                               cmd.call();
-                       } else if ("remove".equals(command) || "rm".equals(command)) { //$NON-NLS-1$ //$NON-NLS-2$
-                               RemoteRemoveCommand cmd = git.remoteRemove();
-                               cmd.setRemoteName(name);
-                               cmd.call();
-                       } else if ("set-url".equals(command)) { //$NON-NLS-1$
-                               RemoteSetUrlCommand cmd = git.remoteSetUrl();
-                               cmd.setRemoteName(name);
-                               cmd.setRemoteUri(new URIish(uri));
-                               cmd.setUriType(push ? UriType.PUSH : UriType.FETCH);
-                               cmd.call();
-                       } else if ("update".equals(command)) { //$NON-NLS-1$
-                               // reuse fetch command for basic implementation of remote update
+                               return;
+                       }
+                       switch (command) {
+                       case "add": //$NON-NLS-1$
+                               RemoteAddCommand add = git.remoteAdd();
+                               add.setName(name);
+                               add.setUri(new URIish(uri));
+                               add.call();
+                               break;
+                       case "remove": //$NON-NLS-1$
+                       case "rm": //$NON-NLS-1$
+                               RemoteRemoveCommand rm = git.remoteRemove();
+                               rm.setRemoteName(name);
+                               rm.call();
+                               break;
+                       case "set-url": //$NON-NLS-1$
+                               RemoteSetUrlCommand remoteSetUrl = git.remoteSetUrl();
+                               remoteSetUrl.setRemoteName(name);
+                               remoteSetUrl.setRemoteUri(new URIish(uri));
+                               remoteSetUrl.setUriType(push ? UriType.PUSH : UriType.FETCH);
+                               remoteSetUrl.call();
+                               break;
+                       case "update": //$NON-NLS-1$
                                Fetch fetch = new Fetch();
                                fetch.init(db, gitdir);
-
-                               // redirect the output stream
                                StringWriter osw = new StringWriter();
                                fetch.outw = new ThrowingPrintWriter(osw);
-                               // redirect the error stream
                                StringWriter esw = new StringWriter();
                                fetch.errw = new ThrowingPrintWriter(esw);
-
                                List<String> fetchArgs = new ArrayList<>();
                                if (verbose) {
                                        fetchArgs.add("--verbose"); //$NON-NLS-1$
@@ -130,15 +132,13 @@ class Remote extends TextBuiltin {
                                if (name != null) {
                                        fetchArgs.add(name);
                                }
-
                                fetch.execute(fetchArgs.toArray(new String[0]));
-
-                               // flush the streams
                                fetch.outw.flush();
                                fetch.errw.flush();
                                outw.println(osw.toString());
                                errw.println(esw.toString());
-                       } else {
+                               break;
+                       default:
                                throw new JGitInternalException(MessageFormat
                                                .format(CLIText.get().unknownSubcommand, command));
                        }
index 79bd3faa0178302af92af956eadeb2dd51445cc4..be5eaa495da003ddd6f5f6fd161ead4b1ad76563 100644 (file)
@@ -251,12 +251,13 @@ public class JGitClientSession extends ClientSessionImpl {
                                .getProperty(SshConstants.HOST_KEY_ALGORITHMS);
                if (hostKeyAlgorithms != null && !hostKeyAlgorithms.isEmpty()) {
                        char first = hostKeyAlgorithms.charAt(0);
-                       if (first == '+') {
+                       switch (first) {
+                       case '+':
                                // Additions make not much sense -- it's either in
                                // defaultSignatures already, or we have no implementation for
                                // it. No point in proposing it.
                                return String.join(",", defaultSignatures); //$NON-NLS-1$
-                       } else if (first == '-') {
+                       case '-':
                                // This takes wildcard patterns!
                                removeFromList(defaultSignatures,
                                                SshConstants.HOST_KEY_ALGORITHMS,
@@ -269,7 +270,7 @@ public class JGitClientSession extends ClientSessionImpl {
                                                        hostKeyAlgorithms));
                                }
                                return String.join(",", defaultSignatures); //$NON-NLS-1$
-                       } else {
+                       default:
                                // Default is overridden -- only accept the ones for which we do
                                // have an implementation.
                                List<String> newNames = filteredList(defaultSignatures,
@@ -282,6 +283,7 @@ public class JGitClientSession extends ClientSessionImpl {
                                } else {
                                        return String.join(",", newNames); //$NON-NLS-1$
                                }
+                               break;
                        }
                }
                // No HostKeyAlgorithms; using default -- change order to put existing
index cfc275a7dec0f245c1a8bf7217eb1c2936811f0f..6e0762fe3dfce33b0c1ef8471413383a31019d46 100644 (file)
@@ -169,12 +169,16 @@ public class DfsGarbageCollectorTest {
                DfsPackFile garbage = null;
                for (DfsPackFile pack : odb.getPacks()) {
                        DfsPackDescription d = pack.getPackDescription();
-                       if (d.getPackSource() == GC) {
+                       switch (d.getPackSource()) {
+                       case GC:
                                gc = pack;
-                       } else if (d.getPackSource() == UNREACHABLE_GARBAGE) {
+                               break;
+                       case UNREACHABLE_GARBAGE:
                                garbage = pack;
-                       } else {
+                               break;
+                       default:
                                fail("unexpected " + d.getPackSource());
+                               break;
                        }
                }
 
@@ -200,16 +204,20 @@ public class DfsGarbageCollectorTest {
                boolean garbagePackFound = false;
                for (DfsPackFile pack : odb.getPacks()) {
                        DfsPackDescription d = pack.getPackDescription();
-                       if (d.getPackSource() == GC) {
+                       switch (d.getPackSource()) {
+                       case GC:
                                gcPackFound = true;
                                assertTrue("has commit0", isObjectInPack(commit0, pack));
                                assertFalse("no commit1", isObjectInPack(commit1, pack));
-                       } else if (d.getPackSource() == UNREACHABLE_GARBAGE) {
+                               break;
+                       case UNREACHABLE_GARBAGE:
                                garbagePackFound = true;
                                assertFalse("no commit0", isObjectInPack(commit0, pack));
                                assertTrue("has commit1", isObjectInPack(commit1, pack));
-                       } else {
+                               break;
+                       default:
                                fail("unexpected " + d.getPackSource());
+                               break;
                        }
                }
                assertTrue("gc pack found", gcPackFound);
@@ -240,16 +248,20 @@ public class DfsGarbageCollectorTest {
                boolean garbagePackFound = false;
                for (DfsPackFile pack : odb.getPacks()) {
                        DfsPackDescription d = pack.getPackDescription();
-                       if (d.getPackSource() == GC) {
+                       switch (d.getPackSource()) {
+                       case GC:
                                gcPackFound = true;
                                assertTrue("has commit0", isObjectInPack(commit0, pack));
                                assertFalse("no commit1", isObjectInPack(commit1, pack));
-                       } else if (d.getPackSource() == UNREACHABLE_GARBAGE) {
+                               break;
+                       case UNREACHABLE_GARBAGE:
                                garbagePackFound = true;
                                assertFalse("no commit0", isObjectInPack(commit0, pack));
                                assertTrue("has commit1", isObjectInPack(commit1, pack));
-                       } else {
+                               break;
+                       default:
                                fail("unexpected " + d.getPackSource());
+                               break;
                        }
                }
                assertTrue("gc pack found", gcPackFound);
@@ -448,12 +460,16 @@ public class DfsGarbageCollectorTest {
                long inputPacksSize = 32;
                for (DfsPackFile pack : odb.getPacks()) {
                        DfsPackDescription d = pack.getPackDescription();
-                       if (d.getPackSource() == GC) {
+                       switch (d.getPackSource()) {
+                       case GC:
                                gcPackFound = true;
-                       } else if (d.getPackSource() == INSERT) {
+                               break;
+                       case INSERT:
                                insertPackFound = true;
-                       } else {
+                               break;
+                       default:
                                fail("unexpected " + d.getPackSource());
+                               break;
                        }
                        inputPacksSize += d.getFileSize(PACK) - 32;
                }
@@ -512,12 +528,16 @@ public class DfsGarbageCollectorTest {
                long inputPacksSize = 32;
                for (DfsPackFile pack : odb.getPacks()) {
                        DfsPackDescription d = pack.getPackDescription();
-                       if (d.getPackSource() == GC_REST) {
+                       switch (d.getPackSource()) {
+                       case GC_REST:
                                gcRestPackFound = true;
-                       } else if (d.getPackSource() == INSERT) {
+                               break;
+                       case INSERT:
                                insertPackFound = true;
-                       } else {
+                               break;
+                       default:
                                fail("unexpected " + d.getPackSource());
+                               break;
                        }
                        inputPacksSize += d.getFileSize(PACK) - 32;
                }
@@ -555,17 +575,22 @@ public class DfsGarbageCollectorTest {
                long insertPackSize = 0;
                for (DfsPackFile pack : odb.getPacks()) {
                        DfsPackDescription d = pack.getPackDescription();
-                       if (d.getPackSource() == GC) {
+                       switch (d.getPackSource()) {
+                       case GC:
                                gcPackFound = true;
                                gcPackSize = d.getFileSize(PACK);
-                       } else if (d.getPackSource() == GC_REST) {
+                               break;
+                       case GC_REST:
                                gcRestPackFound = true;
                                gcRestPackSize = d.getFileSize(PACK);
-                       } else if (d.getPackSource() == INSERT) {
+                               break;
+                       case INSERT:
                                insertPackFound = true;
                                insertPackSize = d.getFileSize(PACK);
-                       } else {
+                               break;
+                       default:
                                fail("unexpected " + d.getPackSource());
+                               break;
                        }
                }
                assertTrue(gcPackFound);
@@ -583,16 +608,20 @@ public class DfsGarbageCollectorTest {
                gcRestPackFound = false;
                for (DfsPackFile pack : odb.getPacks()) {
                        DfsPackDescription d = pack.getPackDescription();
-                       if (d.getPackSource() == GC) {
+                       switch (d.getPackSource()) {
+                       case GC:
                                gcPackFound = true;
                                assertEquals(gcPackSize + insertPackSize - 32,
                                                pack.getPackDescription().getEstimatedPackSize());
-                       } else if (d.getPackSource() == GC_REST) {
+                               break;
+                       case GC_REST:
                                gcRestPackFound = true;
                                assertEquals(gcRestPackSize + insertPackSize - 32,
                                                pack.getPackDescription().getEstimatedPackSize());
-                       } else {
+                               break;
+                       default:
                                fail("unexpected " + d.getPackSource());
+                               break;
                        }
                }
                assertTrue(gcPackFound);
@@ -629,18 +658,22 @@ public class DfsGarbageCollectorTest {
                assertEquals(2, odb.getPacks().length);
                for (DfsPackFile pack : odb.getPacks()) {
                        DfsPackDescription d = pack.getPackDescription();
-                       if (d.getPackSource() == GC) {
+                       switch (d.getPackSource()) {
+                       case GC:
                                // Even though just commit0 will end up in GC pack, because
                                // there is no good way to know that up front, both the pack
                                // sizes are considered while computing the estimated size of GC
                                // pack.
                                assertEquals(packSize0 + packSize1 - 32,
                                                d.getEstimatedPackSize());
-                       } else if (d.getPackSource() == UNREACHABLE_GARBAGE) {
+                               break;
+                       case UNREACHABLE_GARBAGE:
                                // commit1 is moved to UNREACHABLE_GARBAGE pack.
                                assertEquals(packSize1, d.getEstimatedPackSize());
-                       } else {
+                               break;
+                       default:
                                fail("unexpected " + d.getPackSource());
+                               break;
                        }
                }
        }
index edf94cff44b6ff69b119f2c4222ffe3e858ad24b..22409a3b6db55ed22a3cc46428c182ee6ae0bd1a 100644 (file)
@@ -108,10 +108,10 @@ public class AwtCredentialsProvider extends CredentialsProvider {
        @Override
        public boolean get(URIish uri, CredentialItem... items)
                        throws UnsupportedCredentialItem {
-               if (items.length == 0) {
+               switch (items.length) {
+               case 0:
                        return true;
-
-               } else if (items.length == 1) {
+               case 1:
                        final CredentialItem item = items[0];
 
                        if (item instanceof CredentialItem.InformationalMessage) {
@@ -141,8 +141,7 @@ public class AwtCredentialsProvider extends CredentialsProvider {
                        } else {
                                return interactive(uri, items);
                        }
-
-               } else {
+               default:
                        return interactive(uri, items);
                }
        }
index 106737805c86e28c0aec38c1c0b34c3734a13b3d..4e544d18e86627695443ca1d82f0e821d1e122fb 100644 (file)
@@ -92,15 +92,19 @@ final class ReverseWalk extends RevWalk {
                        // visited first by BlameGenerator when considering candidates.
 
                        int cnt = children.length;
-                       if (cnt == 0)
+                       switch (cnt) {
+                       case 0:
                                children = new ReverseCommit[] { c };
-                       else if (cnt == 1)
+                               break;
+                       case 1:
                                children = new ReverseCommit[] { c, children[0] };
-                       else {
+                               break;
+                       default:
                                ReverseCommit[] n = new ReverseCommit[1 + cnt];
                                n[0] = c;
                                System.arraycopy(children, 0, n, 1, cnt);
                                children = n;
+                               break;
                        }
                }
 
index febdb9209169d9a864700f23936d2227b02692e7..4c652e583deb12ee35cb82851b2119c72b94df2c 100644 (file)
@@ -183,54 +183,61 @@ public class ManifestParser extends DefaultHandler {
        }
 
        /** {@inheritDoc} */
+       @SuppressWarnings("nls")
        @Override
        public void startElement(
                        String uri,
                        String localName,
                        String qName,
                        Attributes attributes) throws SAXException {
-               if ("project".equals(qName)) { //$NON-NLS-1$
-                       if (attributes.getValue("name") == null) { //$NON-NLS-1$
+               if (qName == null) {
+                       return;
+               }
+               switch (qName) {
+               case "project":
+                       if (attributes.getValue("name") == null) {
                                throw new SAXException(RepoText.get().invalidManifest);
                        }
-                       currentProject = new RepoProject(
-                                       attributes.getValue("name"), //$NON-NLS-1$
-                                       attributes.getValue("path"), //$NON-NLS-1$
-                                       attributes.getValue("revision"), //$NON-NLS-1$
-                                       attributes.getValue("remote"), //$NON-NLS-1$
-                                       attributes.getValue("groups")); //$NON-NLS-1$
-                       currentProject.setRecommendShallow(
-                               attributes.getValue("clone-depth")); //$NON-NLS-1$
-               } else if ("remote".equals(qName)) { //$NON-NLS-1$
-                       String alias = attributes.getValue("alias"); //$NON-NLS-1$
-                       String fetch = attributes.getValue("fetch"); //$NON-NLS-1$
-                       String revision = attributes.getValue("revision"); //$NON-NLS-1$
+                       currentProject = new RepoProject(attributes.getValue("name"),
+                                       attributes.getValue("path"),
+                                       attributes.getValue("revision"),
+                                       attributes.getValue("remote"),
+                                       attributes.getValue("groups"));
+                       currentProject
+                                       .setRecommendShallow(attributes.getValue("clone-depth"));
+                       break;
+               case "remote":
+                       String alias = attributes.getValue("alias");
+                       String fetch = attributes.getValue("fetch");
+                       String revision = attributes.getValue("revision");
                        Remote remote = new Remote(fetch, revision);
-                       remotes.put(attributes.getValue("name"), remote); //$NON-NLS-1$
-                       if (alias != null)
+                       remotes.put(attributes.getValue("name"), remote);
+                       if (alias != null) {
                                remotes.put(alias, remote);
-               } else if ("default".equals(qName)) { //$NON-NLS-1$
-                       defaultRemote = attributes.getValue("remote"); //$NON-NLS-1$
-                       defaultRevision = attributes.getValue("revision"); //$NON-NLS-1$
-               } else if ("copyfile".equals(qName)) { //$NON-NLS-1$
-                       if (currentProject == null)
+                       }
+                       break;
+               case "default":
+                       defaultRemote = attributes.getValue("remote");
+                       defaultRevision = attributes.getValue("revision");
+                       break;
+               case "copyfile":
+                       if (currentProject == null) {
                                throw new SAXException(RepoText.get().invalidManifest);
-                       currentProject.addCopyFile(new CopyFile(
-                                               rootRepo,
-                                               currentProject.getPath(),
-                                               attributes.getValue("src"), //$NON-NLS-1$
-                                               attributes.getValue("dest"))); //$NON-NLS-1$
-               } else if ("linkfile".equals(qName)) { //$NON-NLS-1$
+                       }
+                       currentProject.addCopyFile(new CopyFile(rootRepo,
+                                       currentProject.getPath(), attributes.getValue("src"),
+                                       attributes.getValue("dest")));
+                       break;
+               case "linkfile":
                        if (currentProject == null) {
                                throw new SAXException(RepoText.get().invalidManifest);
                        }
-                       currentProject.addLinkFile(new LinkFile(
-                                               rootRepo,
-                                               currentProject.getPath(),
-                                               attributes.getValue("src"), //$NON-NLS-1$
-                                               attributes.getValue("dest"))); //$NON-NLS-1$
-               } else if ("include".equals(qName)) { //$NON-NLS-1$
-                       String name = attributes.getValue("name"); //$NON-NLS-1$
+                       currentProject.addLinkFile(new LinkFile(rootRepo,
+                                       currentProject.getPath(), attributes.getValue("src"),
+                                       attributes.getValue("dest")));
+                       break;
+               case "include":
+                       String name = attributes.getValue("name");
                        if (includedReader != null) {
                                try (InputStream is = includedReader.readIncludeFile(name)) {
                                        if (is == null) {
@@ -239,8 +246,8 @@ public class ManifestParser extends DefaultHandler {
                                        }
                                        read(is);
                                } catch (Exception e) {
-                                       throw new SAXException(MessageFormat.format(
-                                                       RepoText.get().errorIncludeFile, name), e);
+                                       throw new SAXException(MessageFormat
+                                                       .format(RepoText.get().errorIncludeFile, name), e);
                                }
                        } else if (filename != null) {
                                int index = filename.lastIndexOf('/');
@@ -248,13 +255,18 @@ public class ManifestParser extends DefaultHandler {
                                try (InputStream is = new FileInputStream(path)) {
                                        read(is);
                                } catch (IOException e) {
-                                       throw new SAXException(MessageFormat.format(
-                                                       RepoText.get().errorIncludeFile, path), e);
+                                       throw new SAXException(MessageFormat
+                                                       .format(RepoText.get().errorIncludeFile, path), e);
                                }
                        }
-               } else if ("remove-project".equals(qName)) { //$NON-NLS-1$
-                       String name = attributes.getValue("name"); //$NON-NLS-1$
-                       projects.removeIf((p) -> p.getName().equals(name));
+                       break;
+               case "remove-project": {
+                       String name2 = attributes.getValue("name");
+                       projects.removeIf((p) -> p.getName().equals(name2));
+                       break;
+               }
+               default:
+                       break;
                }
        }
 
index 13f71a7ff669f4b7b00eb17d9764e0e6f94b417e..d9059226a49759062069ce73bc54ebfe4a5da865 100644 (file)
@@ -410,7 +410,8 @@ public class CommitBuilder {
                        throws IOException, IllegalArgumentException {
                for (int i = 0; i < in.length(); ++i) {
                        char ch = in.charAt(i);
-                       if (ch == '\r') {
+                       switch (ch) {
+                       case '\r':
                                if (i + 1 < in.length() && in.charAt(i + 1) == '\n') {
                                        out.write('\n');
                                        out.write(' ');
@@ -419,15 +420,18 @@ public class CommitBuilder {
                                        out.write('\n');
                                        out.write(' ');
                                }
-                       } else if (ch == '\n') {
+                               break;
+                       case '\n':
                                out.write('\n');
                                out.write(' ');
-                       } else {
+                               break;
+                       default:
                                // sanity check
                                if (ch > 127)
                                        throw new IllegalArgumentException(MessageFormat
                                                        .format(JGitText.get().notASCIIString, in));
                                out.write(ch);
+                               break;
                        }
                }
        }
index 9914b0c991bf87943c076aad92f8cf32169b6e90..a575dee029bb59697c3c263c136dc0453f7a7389 100644 (file)
@@ -102,27 +102,34 @@ public class PlotCommit<L extends PlotLane> extends RevCommit {
 
        private static PlotLane[] addLane(PlotLane l, PlotLane[] lanes) {
                final int cnt = lanes.length;
-               if (cnt == 0)
+               switch (cnt) {
+               case 0:
                        lanes = new PlotLane[] { l };
-               else if (cnt == 1)
+                       break;
+               case 1:
                        lanes = new PlotLane[] { lanes[0], l };
-               else {
+                       break;
+               default:
                        final PlotLane[] n = new PlotLane[cnt + 1];
                        System.arraycopy(lanes, 0, n, 0, cnt);
                        n[cnt] = l;
                        lanes = n;
+                       break;
                }
                return lanes;
        }
 
        void addChild(PlotCommit c) {
                final int cnt = children.length;
-               if (cnt == 0)
+               switch (cnt) {
+               case 0:
                        children = new PlotCommit[] { c };
-               else if (cnt == 1) {
+                       break;
+               case 1:
                        if (!c.getId().equals(children[0].getId()))
                                children = new PlotCommit[] { children[0], c };
-               } else {
+                       break;
+               default:
                        for (PlotCommit pc : children)
                                if (c.getId().equals(pc.getId()))
                                        return;
@@ -130,6 +137,7 @@ public class PlotCommit<L extends PlotLane> extends RevCommit {
                        System.arraycopy(children, 0, n, 0, cnt);
                        n[cnt] = c;
                        children = n;
+                       break;
                }
        }
 
index af4ec1f00b9d3ce2fd9dfcd9aac003f2f97681df..98fb765699eeff3666b19dbbead4f5ba546897c8 100644 (file)
@@ -188,18 +188,22 @@ public class RevCommit extends RevObject {
                                }
                                idBuffer.fromString(raw, ptr + 7);
                                final RevCommit p = walk.lookupCommit(idBuffer);
-                               if (nParents == 0) {
+                               switch (nParents) {
+                               case 0:
                                        pList[nParents++] = p;
-                               } else if (nParents == 1) {
+                                       break;
+                               case 1:
                                        pList = new RevCommit[] { pList[0], p };
                                        nParents = 2;
-                               } else {
+                                       break;
+                               default:
                                        if (pList.length <= nParents) {
                                                RevCommit[] old = pList;
                                                pList = new RevCommit[pList.length + 32];
                                                System.arraycopy(old, 0, pList, 0, nParents);
                                        }
                                        pList[nParents++] = p;
+                                       break;
                                }
                                ptr += 48;
                        }
index 54c19783e34b96e3f70afeb5b74ccdfd49acad37..1c670b73de916dd1bdace2cac4d48d95ad6b7c3f 100644 (file)
@@ -72,12 +72,14 @@ public class AdvertiseRefsHookChain implements AdvertiseRefsHook {
                for (AdvertiseRefsHook hook : hooks)
                        if (hook != AdvertiseRefsHook.DEFAULT)
                                newHooks[i++] = hook;
-               if (i == 0)
+               switch (i) {
+               case 0:
                        return AdvertiseRefsHook.DEFAULT;
-               else if (i == 1)
+               case 1:
                        return newHooks[0];
-               else
+               default:
                        return new AdvertiseRefsHookChain(newHooks, i);
+               }
        }
 
        /** {@inheritDoc} */
index d73e1939a6d8666bf6f6ec43ad8a05ec50d61675..2af7f068dcd24e0b40383c78ff8b7368d1efcc21 100644 (file)
@@ -148,12 +148,16 @@ public class PacketLineIn {
                                return AckNackResult.ACK;
 
                        final String arg = line.substring(44);
-                       if (arg.equals(" continue")) //$NON-NLS-1$
+                       switch (arg) {
+                       case " continue": //$NON-NLS-1$
                                return AckNackResult.ACK_CONTINUE;
-                       else if (arg.equals(" common")) //$NON-NLS-1$
+                       case " common": //$NON-NLS-1$
                                return AckNackResult.ACK_COMMON;
-                       else if (arg.equals(" ready")) //$NON-NLS-1$
+                       case " ready": //$NON-NLS-1$
                                return AckNackResult.ACK_READY;
+                       default:
+                               break;
+                       }
                }
                if (line.startsWith("ERR ")) //$NON-NLS-1$
                        throw new PackProtocolException(line.substring(4));
index 0bdf7d4f6b395ee66cbff970306d0fa678e116ef..64598e1913e48c7b8b31ff1bedf06528e971cf4f 100644 (file)
@@ -70,12 +70,14 @@ public class PostReceiveHookChain implements PostReceiveHook {
                for (PostReceiveHook hook : hooks)
                        if (hook != PostReceiveHook.NULL)
                                newHooks[i++] = hook;
-               if (i == 0)
+               switch (i) {
+               case 0:
                        return PostReceiveHook.NULL;
-               else if (i == 1)
+               case 1:
                        return newHooks[0];
-               else
+               default:
                        return new PostReceiveHookChain(newHooks, i);
+               }
        }
 
        /** {@inheritDoc} */
index 47b94c6e49d4100afb2cd2fbf9497fc5b338af6e..30594bdb6c16e45cfb11dc9808d3880144eb1a10 100644 (file)
@@ -69,12 +69,14 @@ public class PreReceiveHookChain implements PreReceiveHook {
                for (PreReceiveHook hook : hooks)
                        if (hook != PreReceiveHook.NULL)
                                newHooks[i++] = hook;
-               if (i == 0)
+               switch (i) {
+               case 0:
                        return PreReceiveHook.NULL;
-               else if (i == 1)
+               case 1:
                        return newHooks[0];
-               else
+               default:
                        return new PreReceiveHookChain(newHooks, i);
+               }
        }
 
        /** {@inheritDoc} */
index c6a689994860bd91e8da1ce297234e03a5b8c6ca..1af11b241ade31aeecd7d708c24c7b742fcd6b80 100644 (file)
@@ -252,12 +252,13 @@ public class GitDateParser {
        }
 
        // tries to parse a string with a relative time specification
+       @SuppressWarnings("nls")
        private static Date parse_relative(String dateStr, Calendar now) {
                Calendar cal;
                SystemReader sysRead = SystemReader.getInstance();
 
                // check for the static words "yesterday" or "now"
-               if ("now".equals(dateStr)) { //$NON-NLS-1$
+               if ("now".equals(dateStr)) {
                        return ((now == null) ? new Date(sysRead.getCurrentTime()) : now
                                        .getTime());
                }
@@ -269,7 +270,7 @@ public class GitDateParser {
                } else
                        cal = (Calendar) now.clone();
 
-               if ("yesterday".equals(dateStr)) { //$NON-NLS-1$
+               if ("yesterday".equals(dateStr)) {
                        cal.add(Calendar.DATE, -1);
                        cal.set(Calendar.HOUR_OF_DAY, 0);
                        cal.set(Calendar.MINUTE, 0);
@@ -280,12 +281,12 @@ public class GitDateParser {
                }
 
                // parse constructs like "3 days ago", "5.week.2.day.ago"
-               String[] parts = dateStr.split("\\.| "); //$NON-NLS-1$
+               String[] parts = dateStr.split("\\.| ");
                int partsLength = parts.length;
                // check we have an odd number of parts (at least 3) and that the last
                // part is "ago"
                if (partsLength < 3 || (partsLength & 1) == 0
-                               || !"ago".equals(parts[parts.length - 1])) //$NON-NLS-1$
+                               || !"ago".equals(parts[parts.length - 1]))
                        return null;
                int number;
                for (int i = 0; i < parts.length - 2; i += 2) {
@@ -294,27 +295,41 @@ public class GitDateParser {
                        } catch (NumberFormatException e) {
                                return null;
                        }
-                       if ("year".equals(parts[i + 1]) || "years".equals(parts[i + 1])) //$NON-NLS-1$ //$NON-NLS-2$
+                       if (parts[i + 1] == null){
+                               return null;
+                       }
+                       switch (parts[i + 1]) {
+                       case "year":
+                       case "years":
                                cal.add(Calendar.YEAR, -number);
-                       else if ("month".equals(parts[i + 1]) //$NON-NLS-1$
-                                       || "months".equals(parts[i + 1])) //$NON-NLS-1$
+                               break;
+                       case "month":
+                       case "months":
                                cal.add(Calendar.MONTH, -number);
-                       else if ("week".equals(parts[i + 1]) //$NON-NLS-1$
-                                       || "weeks".equals(parts[i + 1])) //$NON-NLS-1$
+                               break;
+                       case "week":
+                       case "weeks":
                                cal.add(Calendar.WEEK_OF_YEAR, -number);
-                       else if ("day".equals(parts[i + 1]) || "days".equals(parts[i + 1])) //$NON-NLS-1$ //$NON-NLS-2$
+                               break;
+                       case "day":
+                       case "days":
                                cal.add(Calendar.DATE, -number);
-                       else if ("hour".equals(parts[i + 1]) //$NON-NLS-1$
-                                       || "hours".equals(parts[i + 1])) //$NON-NLS-1$
+                               break;
+                       case "hour":
+                       case "hours":
                                cal.add(Calendar.HOUR_OF_DAY, -number);
-                       else if ("minute".equals(parts[i + 1]) //$NON-NLS-1$
-                                       || "minutes".equals(parts[i + 1])) //$NON-NLS-1$
+                               break;
+                       case "minute":
+                       case "minutes":
                                cal.add(Calendar.MINUTE, -number);
-                       else if ("second".equals(parts[i + 1]) //$NON-NLS-1$
-                                       || "seconds".equals(parts[i + 1])) //$NON-NLS-1$
+                               break;
+                       case "second":
+                       case "seconds":
                                cal.add(Calendar.SECOND, -number);
-                       else
+                               break;
+                       default:
                                return null;
+                       }
                }
                return cal.getTime();
        }
index f4b6f9d0df04b809fad0e0ab4f32f7e833195699..7e8bbc80f8f39a4320f6e5b46c56f4eccb985265 100644 (file)
@@ -322,16 +322,21 @@ public final class StringUtils {
                int o = 0;
                for (int i = 0; i < buf.length; ++i) {
                        char ch = in.charAt(i);
-                       if (ch == '\r') {
+                       switch (ch) {
+                       case '\r':
                                if (i + 1 < buf.length && in.charAt(i + 1) == '\n') {
                                        buf[o++] = ' ';
                                        ++i;
                                } else
                                        buf[o++] = ' ';
-                       } else if (ch == '\n')
+                               break;
+                       case '\n':
                                buf[o++] = ' ';
-                       else
+                               break;
+                       default:
                                buf[o++] = ch;
+                               break;
+                       }
                }
                return new String(buf, 0, o);
        }
index d8cfee7c460d864d361c63d609eb61949bf00361..8eca6dce912ccd7de28e5b10e861cdf83841fb38 100644 (file)
@@ -129,9 +129,11 @@ public class AutoCRLFOutputStream extends OutputStream {
                }
                for (int i = off; i < off + len; ++i) {
                        final byte c = b[i];
-                       if (c == '\r') {
+                       switch (c) {
+                       case '\r':
                                buf = '\r';
-                       } else if (c == '\n') {
+                               break;
+                       case '\n':
                                if (buf != '\r') {
                                        if (lastw < i) {
                                                out.write(b, lastw, i - lastw);
@@ -140,8 +142,10 @@ public class AutoCRLFOutputStream extends OutputStream {
                                        lastw = i;
                                }
                                buf = -1;
-                       } else {
+                               break;
+                       default:
                                buf = -1;
+                               break;
                        }
                }
                if (lastw < off + len) {
index 908d0a0b44acea63694ae03281e997a6e9c7d167..e235aa0ed4c672a004fc5ce709d6f00572029908 100644 (file)
@@ -138,14 +138,16 @@ public class AutoLFOutputStream extends OutputStream {
                }
                for (int i = off; i < off + len; ++i) {
                        final byte c = b[i];
-                       if (c == '\r') {
+                       switch (c) {
+                       case '\r':
                                // skip write r but backlog r
                                if (lastw < i) {
                                        out.write(b, lastw, i - lastw);
                                }
                                lastw = i + 1;
                                buf = '\r';
-                       } else if (c == '\n') {
+                               break;
+                       case '\n':
                                if (buf == '\r') {
                                        out.write('\n');
                                        lastw = i + 1;
@@ -156,12 +158,14 @@ public class AutoLFOutputStream extends OutputStream {
                                        }
                                        lastw = i + 1;
                                }
-                       } else {
+                               break;
+                       default:
                                if (buf == '\r') {
                                        out.write('\r');
                                        lastw = i;
                                }
                                buf = -1;
+                               break;
                        }
                }
                if (lastw < off + len) {
index 1ad6602fce58798a5f8e8ee8b474dc33a8141197..661b194209cae811ebe98ed4ddad5a1ba03b591c 100644 (file)
@@ -325,11 +325,14 @@ public class SHA1 {
 
        private void recompress(int t) {
                State s;
-               if (t == 58) {
+               switch (t) {
+               case 58:
                        s = state58;
-               } else if (t == 65) {
+                       break;
+               case 65:
                        s = state65;
-               } else {
+                       break;
+               default:
                        throw new IllegalStateException();
                }
                int a = s.a, b = s.b, c = s.c, d = s.d, e = s.e;