From db9f8126db23ba90be62777f26a692c7adbb10d9 Mon Sep 17 00:00:00 2001
From: Robin Rosenberg <robin.rosenberg@dewire.com>
Date: Mon, 28 Dec 2009 16:54:43 +0100
Subject: [PATCH] Get rid of a duplicate constant for SHA-1 length

Since Constants.OBJECT_ID_LENGTH is a compile time constant we
can be sure that it will always be inlined. The same goes for the
associated constant STR_LEN which is now refactored to the Constant
class and given a name better suited for wider use.

Change-Id: I03f52131e64edcd0aa74bbbf36e7d42faaf4a698
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../eclipse/jgit/lib/AbbreviatedObjectId.java  |  8 ++++----
 .../src/org/eclipse/jgit/lib/AnyObjectId.java  | 18 ++++--------------
 .../src/org/eclipse/jgit/lib/Constants.java    | 15 ++++++++++++++-
 .../org/eclipse/jgit/lib/MutableObjectId.java  |  8 ++++----
 .../org/eclipse/jgit/lib/ObjectChecker.java    |  2 +-
 .../src/org/eclipse/jgit/lib/ObjectId.java     |  9 +++++----
 6 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/AbbreviatedObjectId.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/AbbreviatedObjectId.java
index 13c54201c..9d9174111 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/AbbreviatedObjectId.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/AbbreviatedObjectId.java
@@ -73,7 +73,7 @@ public final class AbbreviatedObjectId {
 	 */
 	public static final AbbreviatedObjectId fromString(final byte[] buf,
 			final int offset, final int end) {
-		if (end - offset > AnyObjectId.STR_LEN)
+		if (end - offset > Constants.OBJECT_ID_STRING_LENGTH)
 			throw new IllegalArgumentException("Invalid id");
 		return fromHexString(buf, offset, end);
 	}
@@ -86,7 +86,7 @@ public static final AbbreviatedObjectId fromString(final byte[] buf,
 	 * @return the converted object id.
 	 */
 	public static final AbbreviatedObjectId fromString(final String str) {
-		if (str.length() > AnyObjectId.STR_LEN)
+		if (str.length() > Constants.OBJECT_ID_STRING_LENGTH)
 			throw new IllegalArgumentException("Invalid id: " + str);
 		final byte[] b = Constants.encodeASCII(str);
 		return fromHexString(b, 0, b.length);
@@ -167,7 +167,7 @@ public int length() {
 
 	/** @return true if this ObjectId is actually a complete id. */
 	public boolean isComplete() {
-		return length() == AnyObjectId.RAW_LEN * 2;
+		return length() == Constants.OBJECT_ID_STRING_LENGTH;
 	}
 
 	/** @return a complete ObjectId; null if {@link #isComplete()} is false */
@@ -231,7 +231,7 @@ public boolean equals(final Object o) {
 	 * @return string form of the abbreviation, in lower case hexadecimal.
 	 */
 	public final String name() {
-		final char[] b = new char[AnyObjectId.STR_LEN];
+		final char[] b = new char[Constants.OBJECT_ID_STRING_LENGTH];
 
 		AnyObjectId.formatHexChar(b, 0, w1);
 		if (nibbles <= 8)
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/AnyObjectId.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/AnyObjectId.java
index efea0ec22..d4d53574b 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/AnyObjectId.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/AnyObjectId.java
@@ -58,16 +58,6 @@
  * represent a different object name.
  */
 public abstract class AnyObjectId implements Comparable {
-	static final int RAW_LEN = Constants.OBJECT_ID_LENGTH;
-
-	static final int STR_LEN = RAW_LEN * 2;
-
-	static {
-		if (RAW_LEN != 20)
-			throw new LinkageError("ObjectId expects"
-					+ " Constants.OBJECT_ID_LENGTH = 20; it is " + RAW_LEN
-					+ ".");
-	}
 
 	/**
 	 * Compare to object identifier byte sequences for equality.
@@ -312,7 +302,7 @@ public void copyTo(final OutputStream w) throws IOException {
 	}
 
 	private byte[] toHexByteArray() {
-		final byte[] dst = new byte[STR_LEN];
+		final byte[] dst = new byte[Constants.OBJECT_ID_STRING_LENGTH];
 		formatHexByte(dst, 0, w1);
 		formatHexByte(dst, 8, w2);
 		formatHexByte(dst, 16, w3);
@@ -360,7 +350,7 @@ public void copyTo(final Writer w) throws IOException {
 	 */
 	public void copyTo(final char[] tmp, final Writer w) throws IOException {
 		toHexCharArray(tmp);
-		w.write(tmp, 0, STR_LEN);
+		w.write(tmp, 0, Constants.OBJECT_ID_STRING_LENGTH);
 	}
 
 	/**
@@ -375,11 +365,11 @@ public void copyTo(final char[] tmp, final Writer w) throws IOException {
 	 */
 	public void copyTo(final char[] tmp, final StringBuilder w) {
 		toHexCharArray(tmp);
-		w.append(tmp, 0, STR_LEN);
+		w.append(tmp, 0, Constants.OBJECT_ID_STRING_LENGTH);
 	}
 
 	private char[] toHexCharArray() {
-		final char[] dst = new char[STR_LEN];
+		final char[] dst = new char[Constants.OBJECT_ID_STRING_LENGTH];
 		toHexCharArray(dst);
 		return dst;
 	}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java
index de1315957..c1d78be5a 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java
@@ -58,9 +58,22 @@ public final class Constants {
 	/** Hash function used natively by Git for all objects. */
 	private static final String HASH_FUNCTION = "SHA-1";
 
-	/** Length of an object hash. */
+	/**
+	 * A Git object hash is 160 bits, i.e. 20 bytes.
+	 * <p>
+	 * Changing this assumption is not going to be as easy as changing this
+	 * declaration.
+	 */
 	public static final int OBJECT_ID_LENGTH = 20;
 
+	/**
+	 * A Git object can be expressed as a 40 character string of hexadecimal
+	 * digits.
+	 *
+	 * @see #OBJECT_ID_LENGTH
+	 */
+	public static final int OBJECT_ID_STRING_LENGTH = OBJECT_ID_LENGTH * 2;
+
 	/** Special name for the "HEAD" symbolic-ref. */
 	public static final String HEAD = "HEAD";
 
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/MutableObjectId.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/MutableObjectId.java
index c8df1e2ce..a6680d055 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/MutableObjectId.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/MutableObjectId.java
@@ -1,8 +1,7 @@
 /*
  * Copyright (C) 2008-2009, Google Inc.
- * Copyright (C) 2009, Jonas Fonseca <fonseca@diku.dk>
  * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
- * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
+ * Copyright (C) 2007-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  * and other copyright owners as documented in the project's IP log.
  *
@@ -162,7 +161,7 @@ public void fromString(final byte[] buf, final int offset) {
 	 *            the string to read from. Must be 40 characters long.
 	 */
 	public void fromString(final String str) {
-		if (str.length() != STR_LEN)
+		if (str.length() != Constants.OBJECT_ID_STRING_LENGTH)
 			throw new IllegalArgumentException("Invalid id: " + str);
 		fromHexString(Constants.encodeASCII(str), 0);
 	}
@@ -175,7 +174,8 @@ private void fromHexString(final byte[] bs, int p) {
 			w4 = RawParseUtils.parseHexInt32(bs, p + 24);
 			w5 = RawParseUtils.parseHexInt32(bs, p + 32);
 		} catch (ArrayIndexOutOfBoundsException e1) {
-			throw new InvalidObjectIdException(bs, p, STR_LEN);
+			throw new InvalidObjectIdException(bs, p,
+					Constants.OBJECT_ID_STRING_LENGTH);
 		}
 	}
 
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java
index 9cf1643db..82a8c3825 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java
@@ -130,7 +130,7 @@ public void check(final int objType, final byte[] raw)
 	private int id(final byte[] raw, final int ptr) {
 		try {
 			tempId.fromString(raw, ptr);
-			return ptr + AnyObjectId.STR_LEN;
+			return ptr + Constants.OBJECT_ID_STRING_LENGTH;
 		} catch (IllegalArgumentException e) {
 			return -1;
 		}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java
index 3a4e3a891..fc43d1953 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java
@@ -80,10 +80,10 @@ public static final ObjectId zeroId() {
 	 * @return true if the string can converted into an ObjectId.
 	 */
 	public static final boolean isId(final String id) {
-		if (id.length() != STR_LEN)
+		if (id.length() != Constants.OBJECT_ID_STRING_LENGTH)
 			return false;
 		try {
-			for (int i = 0; i < STR_LEN; i++) {
+			for (int i = 0; i < Constants.OBJECT_ID_STRING_LENGTH; i++) {
 				RawParseUtils.parseHexInt4((byte) id.charAt(i));
 			}
 			return true;
@@ -221,7 +221,7 @@ public static final ObjectId fromString(final byte[] buf, final int offset) {
 	 * @return the converted object id.
 	 */
 	public static final ObjectId fromString(final String str) {
-		if (str.length() != STR_LEN)
+		if (str.length() != Constants.OBJECT_ID_STRING_LENGTH)
 			throw new IllegalArgumentException("Invalid id: " + str);
 		return fromHexString(Constants.encodeASCII(str), 0);
 	}
@@ -235,7 +235,8 @@ private static final ObjectId fromHexString(final byte[] bs, int p) {
 			final int e = RawParseUtils.parseHexInt32(bs, p + 32);
 			return new ObjectId(a, b, c, d, e);
 		} catch (ArrayIndexOutOfBoundsException e1) {
-			throw new InvalidObjectIdException(bs, p, STR_LEN);
+			throw new InvalidObjectIdException(bs, p,
+					Constants.OBJECT_ID_STRING_LENGTH);
 		}
 	}
 
-- 
GitLab