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>
This commit is contained in:
parent
2086fdaedd
commit
db9f8126db
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue