diff options
author | Chris Aniszczyk <caniszczyk@gmail.com> | 2010-11-11 10:52:37 -0500 |
---|---|---|
committer | Code Review <codereview-daemon@eclipse.org> | 2010-11-11 10:52:37 -0500 |
commit | 6043d4638cbd865688e5cd0da4dde09ec9de4678 (patch) | |
tree | 9a24fd67f9336108ce001ff48d62344af7a3a4fd | |
parent | ea76489db1e57745de217e55879111a54a4f4671 (diff) | |
parent | e488f1cacd7813feaca9f7d7b09de7eb5ec69f84 (diff) | |
download | jgit-6043d4638cbd865688e5cd0da4dde09ec9de4678.tar.gz jgit-6043d4638cbd865688e5cd0da4dde09ec9de4678.zip |
Merge "Add MutableObjectId setByte to modify a mutable id"
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdTest.java | 29 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/lib/MutableObjectId.java | 54 |
2 files changed, 83 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdTest.java index 2eb1e6b9e1..e5faff15ed 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdTest.java @@ -122,4 +122,33 @@ public class ObjectIdTest extends TestCase { for (int i = 2; i < 20; i++) assertEquals("index " + i, raw[i] & 0xff, id.getByte(i)); } + + public void testSetByte() { + byte[] exp = new byte[20]; + for (int i = 0; i < 20; i++) + exp[i] = (byte) (0xa0 + i); + + MutableObjectId id = new MutableObjectId(); + id.fromRaw(exp); + assertEquals(ObjectId.fromRaw(exp).name(), id.name()); + + id.setByte(0, 0x10); + assertEquals(0x10, id.getByte(0)); + exp[0] = 0x10; + assertEquals(ObjectId.fromRaw(exp).name(), id.name()); + + for (int p = 1; p < 20; p++) { + id.setByte(p, 0x10 + p); + assertEquals(0x10 + p, id.getByte(p)); + exp[p] = (byte) (0x10 + p); + assertEquals(ObjectId.fromRaw(exp).name(), id.name()); + } + + for (int p = 0; p < 20; p++) { + id.setByte(p, 0x80 + p); + assertEquals(0x80 + p, id.getByte(p)); + exp[p] = (byte) (0x80 + p); + assertEquals(ObjectId.fromRaw(exp).name(), id.name()); + } + } } 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 26e83d423b..7d7dfc2f1a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/MutableObjectId.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/MutableObjectId.java @@ -74,6 +74,60 @@ public class MutableObjectId extends AnyObjectId { fromObjectId(src); } + /** + * Set any byte in the id. + * + * @param index + * index of the byte to set in the raw form of the ObjectId. Must + * be in range [0, {@link Constants#OBJECT_ID_LENGTH}). + * @param value + * the value of the specified byte at {@code index}. Values are + * unsigned and thus are in the range [0,255] rather than the + * signed byte range of [-128, 127]. + * @throws ArrayIndexOutOfBoundsException + * {@code index} is less than 0, equal to + * {@link Constants#OBJECT_ID_LENGTH}, or greater than + * {@link Constants#OBJECT_ID_LENGTH}. + */ + public void setByte(int index, int value) { + switch (index >> 2) { + case 0: + w1 = set(w1, index & 3, value); + break; + case 1: + w2 = set(w2, index & 3, value); + break; + case 2: + w3 = set(w3, index & 3, value); + break; + case 3: + w4 = set(w4, index & 3, value); + break; + case 4: + w5 = set(w5, index & 3, value); + break; + default: + throw new ArrayIndexOutOfBoundsException(index); + } + } + + private static int set(int w, int index, int value) { + value &= 0xff; + + switch (index) { + case 0: + return (w & 0x00ffffff) | (value << 24); + case 1: + return (w & 0xff00ffff) | (value << 16); + case 2: + return (w & 0xffff00ff) | (value << 8); + case 3: + return (w & 0xffffff00) | value; + default: + throw new ArrayIndexOutOfBoundsException(); + } + } + /** Make this id match {@link ObjectId#zeroId()}. */ public void clear() { w1 = 0; |