annotate src/com/tmate/hgkit/ll/Nodeid.java @ 24:d4fdd1845b3f

Nodeid with array of exactly 20 bytes
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 06 Jan 2011 04:42:15 +0100
parents 603806cd2dc6
children b2251b7a9823
rev   line source
20
11cfabe692b3 Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 13
diff changeset
1 /*
11cfabe692b3 Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 13
diff changeset
2 * Copyright (c) 2010, 2011 Artem Tikhomirov
0
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 */
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 package com.tmate.hgkit.ll;
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5
24
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
6 import java.util.Arrays;
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
7
20
11cfabe692b3 Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 13
diff changeset
8
0
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 /**
20
11cfabe692b3 Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 13
diff changeset
11 * Whether to store fixed size array (20 bytes) - ease of manipulation (e.g. hashcode/equals), or
11cfabe692b3 Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 13
diff changeset
12 * memory effective - reuse supplied array, keep significant bits only?
11cfabe692b3 Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 13
diff changeset
13 * Fixed size array looks most appealing to me now - I doubt one can save any significant amount of memory.
11cfabe692b3 Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 13
diff changeset
14 * There'd always 20 non-zero bytes, the difference is only for any extra bytes one may pass to constructor
0
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * @author artem
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 *
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 */
24
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
18 public final class Nodeid {
0
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 public static int NULLREV = -1;
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 private final byte[] binaryData;
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22
24
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
23 /**
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
24 * @param binaryRepresentation - byte[20], kept by reference. Use {@link #clone()} if original array may get changed.
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
25 */
0
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 public Nodeid(byte[] binaryRepresentation) {
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 // 5 int fields => 32 bytes
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 // byte[20] => 48 bytes
24
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
29 if (binaryRepresentation == null || binaryRepresentation.length != 20) {
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
30 throw new IllegalArgumentException();
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
31 }
0
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 this.binaryData = binaryRepresentation;
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 }
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34
24
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
35 @Override
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
36 public int hashCode() {
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
37 // TODO consider own impl, especially if byte[] get replaced with 5 ints
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
38 return Arrays.hashCode(binaryData);
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
39 }
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
40
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
41 @Override
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
42 public boolean equals(Object o) {
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
43 if (o instanceof Nodeid) {
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
44 return Arrays.equals(this.binaryData, ((Nodeid) o).binaryData);
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
45 }
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
46 return false;
22
603806cd2dc6 Status of local working dir against non-tip base revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 20
diff changeset
47 }
603806cd2dc6 Status of local working dir against non-tip base revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 20
diff changeset
48
603806cd2dc6 Status of local working dir against non-tip base revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 20
diff changeset
49 public boolean equalsTo(byte[] buf) {
24
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
50 return Arrays.equals(this.binaryData, buf);
22
603806cd2dc6 Status of local working dir against non-tip base revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 20
diff changeset
51 }
603806cd2dc6 Status of local working dir against non-tip base revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 20
diff changeset
52
0
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 @Override
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 public String toString() {
13
df8c67f3006a Basic manifest parsing to analyze what's in there
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 9
diff changeset
55 return new DigestHelper().toHexString(binaryData, 0, binaryData.length);
0
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 }
20
11cfabe692b3 Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 13
diff changeset
57
0
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 // binascii.unhexlify()
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) {
24
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
60 if (length != 40) {
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
61 throw new IllegalArgumentException();
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
62 }
d4fdd1845b3f Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 22
diff changeset
63 byte[] data = new byte[20];
0
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 for (int i = 0, j = offset; i < data.length; i++) {
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 int hiNibble = Character.digit(asciiRepresentation[j++], 16);
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 int lowNibble = Character.digit(asciiRepresentation[j++], 16);
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF);
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 }
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 return new Nodeid(data);
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 }
dbd663faec1f Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 }