{"thread":{"id":"17478","subject":"[PATCH JGIT] use switch to avoid multiple ifs","startedAt":"2009-01-31T14:25:44Z","lastAt":"2009-01-31T14:25:44Z","messageCount":1,"participants":["Yann Simon"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"102699","messageId":"1233411944.8213.2.camel@localhost","threadId":"17478","inReplyTo":null,"subject":"[PATCH JGIT] use switch to avoid multiple ifs","fromName":"Yann Simon","fromEmail":"yann.simon.fr@gmail.com","sentAt":"2009-01-31T14:25:44Z","receivedAt":"2009-01-31T14:25:44Z","isPatch":true,"sender":{"key":"yann.simon.fr@gmail.com","avatar":"https://gravatar.com/avatar/2d926895d27ac988c5c8e591887e5a6a4c7036390403c74fce92519119b887a0?d=mp&s=160"},"body":"instead of using multiple ifs that will evaluate an expression x times,\nuse a switch with cases to evaluate the\nexpression only one time.\nSigned-off-by: Yann Simon <yann.simon.fr@gmail.com>\n---\n .../src/org/spearce/jgit/lib/Repository.java       |   29\n+++++++++++++------\n 1 files changed, 20 insertions(+), 9 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java\nb/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java\nindex b6efac1..f42bae5 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java\n@@ -389,16 +389,23 @@ public Object mapObject(final ObjectId id, final\nString refName) throws IOExcept\n \t\tif (or == null)\n \t\t\treturn null;\n \t\tfinal byte[] raw = or.getBytes();\n-\t\tif (or.getType() == Constants.OBJ_TREE)\n+\t\tswitch (or.getType()) {\n+\t\tcase Constants.OBJ_TREE:\n \t\t\treturn makeTree(id, raw);\n-\t\tif (or.getType() == Constants.OBJ_COMMIT)\n+\t\t\t\n+\t\tcase Constants.OBJ_COMMIT:\n \t\t\treturn makeCommit(id, raw);\n-\t\tif (or.getType() == Constants.OBJ_TAG)\n+\t\t\t\n+\t\tcase Constants.OBJ_TAG:\n \t\t\treturn makeTag(id, refName, raw);\n-\t\tif (or.getType() == Constants.OBJ_BLOB)\n+\t\t\t\n+\t\tcase Constants.OBJ_BLOB:\n \t\t\treturn raw;\n-\t\tthrow new IncorrectObjectTypeException(id,\n+\t\t\t\n+\t\tdefault:\n+\t\t\tthrow new IncorrectObjectTypeException(id,\n \t\t\t\t\"COMMIT nor TREE nor BLOB nor TAG\");\n+\t\t}\n \t}\n \n \t/**\n@@ -449,12 +456,16 @@ public Tree mapTree(final ObjectId id) throws\nIOException {\n \t\tif (or == null)\n \t\t\treturn null;\n \t\tfinal byte[] raw = or.getBytes();\n-\t\tif (Constants.OBJ_TREE == or.getType()) {\n+\t\tswitch (or.getType()) {\n+\t\tcase Constants.OBJ_TREE:\n \t\t\treturn new Tree(this, id, raw);\n-\t\t}\n-\t\tif (Constants.OBJ_COMMIT == or.getType())\n+\n+\t\tcase Constants.OBJ_COMMIT:\n \t\t\treturn mapTree(ObjectId.fromString(raw, 5));\n-\t\tthrow new IncorrectObjectTypeException(id, Constants.TYPE_TREE);\n+\t\t\t\n+\t\tdefault:\n+\t\t\tthrow new IncorrectObjectTypeException(id, Constants.TYPE_TREE);\n+\t\t}\n \t}\n \n \tprivate Tree makeTree(final ObjectId id, final byte[] raw) throws\nIOException {\n-- \n1.6.0.6\n"}]}