diff options
author | Carsten Hammer <carsten.hammer@t-online.de> | 2019-05-21 17:04:18 +0200 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2019-12-14 20:42:03 +0100 |
commit | 74bc50125d3a2cb0a7eb8c32118b3f9a9530a361 (patch) | |
tree | 48e86457ef252be344ece68d9558f72fd23b911c /org.eclipse.jgit | |
parent | 1b3a555f667712ece7f17f009b5444862cbac54f (diff) | |
download | jgit-74bc50125d3a2cb0a7eb8c32118b3f9a9530a361.tar.gz jgit-74bc50125d3a2cb0a7eb8c32118b3f9a9530a361.zip |
Replace chain of if statements with switch
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>
Diffstat (limited to 'org.eclipse.jgit')
14 files changed, 170 insertions, 97 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/blame/ReverseWalk.java b/org.eclipse.jgit/src/org/eclipse/jgit/blame/ReverseWalk.java index 106737805c..4e544d18e8 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/blame/ReverseWalk.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/blame/ReverseWalk.java @@ -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; } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/ManifestParser.java b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/ManifestParser.java index febdb92091..4c652e583d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/ManifestParser.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/ManifestParser.java @@ -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; } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java index 13f71a7ff6..d9059226a4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java @@ -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; } } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revplot/PlotCommit.java b/org.eclipse.jgit/src/org/eclipse/jgit/revplot/PlotCommit.java index 9914b0c991..a575dee029 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revplot/PlotCommit.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revplot/PlotCommit.java @@ -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; } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java index af4ec1f00b..98fb765699 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java @@ -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; } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/AdvertiseRefsHookChain.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/AdvertiseRefsHookChain.java index 54c19783e3..1c670b73de 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/AdvertiseRefsHookChain.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/AdvertiseRefsHookChain.java @@ -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} */ diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java index d73e1939a6..2af7f068dc 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java @@ -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)); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PostReceiveHookChain.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PostReceiveHookChain.java index 0bdf7d4f6b..64598e1913 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PostReceiveHookChain.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PostReceiveHookChain.java @@ -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} */ diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PreReceiveHookChain.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PreReceiveHookChain.java index 47b94c6e49..30594bdb6c 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PreReceiveHookChain.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PreReceiveHookChain.java @@ -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} */ diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java index c6a6899948..1af11b241a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java @@ -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(); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java index f4b6f9d0df..7e8bbc80f8 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java @@ -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); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java index d8cfee7c46..8eca6dce91 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java @@ -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) { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFOutputStream.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFOutputStream.java index 908d0a0b44..e235aa0ed4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFOutputStream.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFOutputStream.java @@ -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) { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/sha1/SHA1.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/sha1/SHA1.java index 1ad6602fce..661b194209 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/sha1/SHA1.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/sha1/SHA1.java @@ -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; |