diff options
author | Robin Rosenberg <robin.rosenberg@dewire.com> | 2009-12-28 16:54:43 +0100 |
---|---|---|
committer | Robin Rosenberg <robin.rosenberg@dewire.com> | 2009-12-28 16:54:43 +0100 |
commit | db9f8126db23ba90be62777f26a692c7adbb10d9 (patch) | |
tree | 4b642c02b1327e8a5367cf8af7935673c3aefa17 | |
parent | 2086fdaedd5e71621470865c34ad075d2668af99 (diff) | |
download | jgit-db9f8126db23ba90be62777f26a692c7adbb10d9.tar.gz jgit-db9f8126db23ba90be62777f26a692c7adbb10d9.zip |
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>
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 13c54201c6..9d9174111b 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 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) 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 efea0ec222..d4d53574b3 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 @@ 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; } 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 de1315957b..c1d78be5af 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 c8df1e2ce6..a6680d0551 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 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); } } 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 9cf1643db5..82a8c38257 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 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; } 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 3a4e3a891b..fc43d19537 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 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); } } |