Browse Source

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>
stable-0.7
Robin Rosenberg 15 years ago
parent
commit
db9f8126db
  1. 8
      org.eclipse.jgit/src/org/eclipse/jgit/lib/AbbreviatedObjectId.java
  2. 18
      org.eclipse.jgit/src/org/eclipse/jgit/lib/AnyObjectId.java
  3. 15
      org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java
  4. 8
      org.eclipse.jgit/src/org/eclipse/jgit/lib/MutableObjectId.java
  5. 2
      org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java
  6. 9
      org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java

8
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 final class AbbreviatedObjectId {
* @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 final class AbbreviatedObjectId {
/** @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 final class AbbreviatedObjectId {
* @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)

18
org.eclipse.jgit/src/org/eclipse/jgit/lib/AnyObjectId.java

@ -58,16 +58,6 @@ import org.eclipse.jgit.util.NB;
* 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 abstract class AnyObjectId implements Comparable {
}
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 abstract class AnyObjectId implements Comparable {
*/
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 abstract class AnyObjectId implements Comparable {
*/
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;
}

15
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";

8
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 class MutableObjectId extends AnyObjectId {
* 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 @@ public class MutableObjectId extends AnyObjectId {
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);
}
}

2
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java

@ -130,7 +130,7 @@ public class ObjectChecker {
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;
}

9
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java

@ -80,10 +80,10 @@ public class ObjectId extends AnyObjectId {
* @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 class ObjectId extends AnyObjectId {
* @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 @@ public class ObjectId extends AnyObjectId {
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);
}
}

Loading…
Cancel
Save