{"thread":{"id":"20554","subject":"[PATCH JGIT 3/5] Introduce InvalidObjectIdException to reduce code duplicated","startedAt":"2009-08-12T15:06:30Z","lastAt":"2009-08-12T15:06:30Z","messageCount":1,"participants":["Jonas Fonseca"],"isPatch":true,"patchVersion":1,"patchTotal":5},"messages":[{"id":"120413","messageId":"1250089590-22401-1-git-send-email-fonseca@diku.dk","threadId":"20554","inReplyTo":null,"subject":"[PATCH JGIT 3/5] Introduce InvalidObjectIdException to reduce code duplicated","fromName":"Jonas Fonseca","fromEmail":"fonseca@diku.dk","sentAt":"2009-08-12T15:06:30Z","receivedAt":"2009-08-12T15:06:30Z","isPatch":true,"sender":{"key":"fonseca@diku.dk","avatar":"https://gravatar.com/avatar/f82f3ad698717c51873b020c750a92438c820a24056dc39fe4d07baa10a92264?d=mp&s=160"},"body":"Collects error handling code related to parsing of object id's in one\nplace.\n\nSigned-off-by: Jonas Fonseca <fonseca@diku.dk>\n---\n .../jgit/errors/InvalidObjectIdException.java      |   69 ++++++++++++++++++++\n .../org/spearce/jgit/lib/AbbreviatedObjectId.java  |   10 +--\n .../src/org/spearce/jgit/lib/MutableObjectId.java  |   12 +---\n .../src/org/spearce/jgit/lib/ObjectId.java         |   10 +--\n 4 files changed, 75 insertions(+), 26 deletions(-)\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/errors/InvalidObjectIdException.java\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/errors/InvalidObjectIdException.java b/org.spearce.jgit/src/org/spearce/jgit/errors/InvalidObjectIdException.java\nnew file mode 100644\nindex 0000000..5d50884\n--- /dev/null\n+++ b/org.spearce.jgit/src/org/spearce/jgit/errors/InvalidObjectIdException.java\n@@ -0,0 +1,69 @@\n+/*\n+ * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>\n+ * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>\n+ * Copyright (C) 2009, Jonas Fonseca <fonseca@diku.dk>\n+ *\n+ * All rights reserved.\n+ *\n+ * Redistribution and use in source and binary forms, with or\n+ * without modification, are permitted provided that the following\n+ * conditions are met:\n+ *\n+ * - Redistributions of source code must retain the above copyright\n+ *   notice, this list of conditions and the following disclaimer.\n+ *\n+ * - Redistributions in binary form must reproduce the above\n+ *   copyright notice, this list of conditions and the following\n+ *   disclaimer in the documentation and/or other materials provided\n+ *   with the distribution.\n+ *\n+ * - Neither the name of the Git Development Community nor the\n+ *   names of its contributors may be used to endorse or promote\n+ *   products derived from this software without specific prior\n+ *   written permission.\n+ *\n+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND\n+ * CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES,\n+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\n+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+\n+package org.spearce.jgit.errors;\n+\n+import java.io.UnsupportedEncodingException;\n+\n+/**\n+ * Thrown when an invalid object id is passed in as an argument.\n+ */\n+public class InvalidObjectIdException extends IllegalArgumentException {\n+\n+\t/**\n+\t * Create exception with bytes of the invalid object id.\n+\t *\n+\t * @param bytes containing the invalid id.\n+\t * @param offset in the byte array where the error occured.\n+\t * @param length of the sequence of invalid bytes.\n+\t */\n+\tpublic InvalidObjectIdException(byte[] bytes, int offset, int length) {\n+\t\tsuper(\"Invalid id\" + asAscii(bytes, offset, length));\n+\t}\n+\n+\tprivate static String asAscii(byte[] bytes, int offset, int length) {\n+\t\ttry {\n+\t\t\treturn \": \" + new String(bytes, offset, length, \"US-ASCII\");\n+\t\t} catch (UnsupportedEncodingException e2) {\n+\t\t\treturn \"\";\n+\t\t} catch (StringIndexOutOfBoundsException e2) {\n+\t\t\treturn \"\";\n+\t\t}\n+\t}\n+}\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/AbbreviatedObjectId.java b/org.spearce.jgit/src/org/spearce/jgit/lib/AbbreviatedObjectId.java\nindex 1706e88..6ad2ea1 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/AbbreviatedObjectId.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/AbbreviatedObjectId.java\n@@ -37,8 +37,7 @@\n \n package org.spearce.jgit.lib;\n \n-import java.io.UnsupportedEncodingException;\n-\n+import org.spearce.jgit.errors.InvalidObjectIdException;\n import org.spearce.jgit.util.NB;\n import org.spearce.jgit.util.RawParseUtils;\n \n@@ -97,12 +96,7 @@ private static final AbbreviatedObjectId fromHexString(final byte[] bs,\n \t\t\tfinal int e = hexUInt32(bs, ptr + 32, end);\n \t\t\treturn new AbbreviatedObjectId(end - ptr, a, b, c, d, e);\n \t\t} catch (ArrayIndexOutOfBoundsException e1) {\n-\t\t\ttry {\n-\t\t\t\tfinal String str = new String(bs, ptr, end - ptr, \"US-ASCII\");\n-\t\t\t\tthrow new IllegalArgumentException(\"Invalid id: \" + str);\n-\t\t\t} catch (UnsupportedEncodingException e2) {\n-\t\t\t\tthrow new IllegalArgumentException(\"Invalid id\");\n-\t\t\t}\n+\t\t\tthrow new InvalidObjectIdException(bs, ptr, end - ptr);\n \t\t}\n \t}\n \ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/MutableObjectId.java b/org.spearce.jgit/src/org/spearce/jgit/lib/MutableObjectId.java\nindex 805f328..9e25908 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/MutableObjectId.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/MutableObjectId.java\n@@ -37,8 +37,7 @@\n \n package org.spearce.jgit.lib;\n \n-import java.io.UnsupportedEncodingException;\n-\n+import org.spearce.jgit.errors.InvalidObjectIdException;\n import org.spearce.jgit.util.NB;\n import org.spearce.jgit.util.RawParseUtils;\n \n@@ -166,14 +165,7 @@ private void fromHexString(final byte[] bs, int p) {\n \t\t\tw4 = RawParseUtils.parseHexInt32(bs, p + 24);\n \t\t\tw5 = RawParseUtils.parseHexInt32(bs, p + 32);\n \t\t} catch (ArrayIndexOutOfBoundsException e1) {\n-\t\t\ttry {\n-\t\t\t\tfinal String str = new String(bs, p, STR_LEN, \"US-ASCII\");\n-\t\t\t\tthrow new IllegalArgumentException(\"Invalid id: \" + str);\n-\t\t\t} catch (UnsupportedEncodingException e2) {\n-\t\t\t\tthrow new IllegalArgumentException(\"Invalid id\");\n-\t\t\t} catch (StringIndexOutOfBoundsException e2) {\n-\t\t\t\tthrow new IllegalArgumentException(\"Invalid id\");\n-\t\t\t}\n+\t\t\tthrow new InvalidObjectIdException(bs, p, STR_LEN);\n \t\t}\n \t}\n \ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectId.java b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectId.java\nindex cdd523f..310d6e0 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectId.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectId.java\n@@ -38,8 +38,7 @@\n \n package org.spearce.jgit.lib;\n \n-import java.io.UnsupportedEncodingException;\n-\n+import org.spearce.jgit.errors.InvalidObjectIdException;\n import org.spearce.jgit.util.NB;\n import org.spearce.jgit.util.RawParseUtils;\n \n@@ -230,12 +229,7 @@ private static final ObjectId fromHexString(final byte[] bs, int p) {\n \t\t\tfinal int e = RawParseUtils.parseHexInt32(bs, p + 32);\n \t\t\treturn new ObjectId(a, b, c, d, e);\n \t\t} catch (ArrayIndexOutOfBoundsException e1) {\n-\t\t\ttry {\n-\t\t\t\tfinal String str = new String(bs, p, STR_LEN, \"US-ASCII\");\n-\t\t\t\tthrow new IllegalArgumentException(\"Invalid id: \" + str);\n-\t\t\t} catch (UnsupportedEncodingException e2) {\n-\t\t\t\tthrow new IllegalArgumentException(\"Invalid id\");\n-\t\t\t}\n+\t\t\tthrow new InvalidObjectIdException(bs, p, STR_LEN);\n \t\t}\n \t}\n \n-- \n1.6.4.rc3.195.g2b05f\n"}]}