Mercurial > jhg
annotate src/com/tmate/hgkit/ll/Nodeid.java @ 36:205f9b59b400
Strip parsing logic out from console frontend
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 13 Jan 2011 23:31:39 +0100 |
parents | 51bc56c0addd |
children | 858d1b2458cb |
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 |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
6 import static com.tmate.hgkit.ll.DigestHelper.toHexString; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
7 |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
8 import java.util.Arrays; |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
9 |
20
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
10 |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
11 |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
12 /** |
20
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
13 * 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
|
14 * 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
|
15 * 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
|
16 * 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
|
17 * @author artem |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18 * |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
19 */ |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
20 public final class Nodeid { |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
21 |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
22 public static final Nodeid NULL = new Nodeid(new byte[20], false); |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
23 private final byte[] binaryData; |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
24 |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
25 /** |
28
b2251b7a9823
Explicit cons arg for clone is more error-prone
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
24
diff
changeset
|
26 * @param binaryRepresentation - byte[20], kept by reference |
b2251b7a9823
Explicit cons arg for clone is more error-prone
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
24
diff
changeset
|
27 * @param shallClone - true if array is subject to future modification and shall be copied, not referenced |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
28 */ |
28
b2251b7a9823
Explicit cons arg for clone is more error-prone
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
24
diff
changeset
|
29 public Nodeid(byte[] binaryRepresentation, boolean shallClone) { |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
30 // 5 int fields => 32 bytes |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
31 // byte[20] => 48 bytes |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
32 if (binaryRepresentation == null || binaryRepresentation.length != 20) { |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
33 throw new IllegalArgumentException(); |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
34 } |
28
b2251b7a9823
Explicit cons arg for clone is more error-prone
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
24
diff
changeset
|
35 this.binaryData = shallClone ? binaryRepresentation.clone() : binaryRepresentation; |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
36 } |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
37 |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
38 @Override |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
39 public int hashCode() { |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
40 // 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
|
41 return Arrays.hashCode(binaryData); |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
42 } |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
43 |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
44 @Override |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
45 public boolean equals(Object o) { |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
46 if (o instanceof Nodeid) { |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
47 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
|
48 } |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
49 return false; |
22
603806cd2dc6
Status of local working dir against non-tip base revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
20
diff
changeset
|
50 } |
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 public boolean equalsTo(byte[] buf) { |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
53 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
|
54 } |
603806cd2dc6
Status of local working dir against non-tip base revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
20
diff
changeset
|
55 |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 @Override |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 public String toString() { |
34
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
58 // XXX may want to output just single 0 for the NULL id? |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
59 return toHexString(binaryData, 0, binaryData.length); |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 } |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
61 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
62 public String shortNotation() { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
63 return toHexString(binaryData, 0, 6); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
64 } |
34
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
65 |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
66 public boolean isNull() { |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
67 if (this == NULL) { |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
68 return true; |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
69 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
70 for (int i = 0; i < 20; i++) { |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
71 if (this.binaryData[i] != 0) { |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
72 return false; |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
73 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
74 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
75 return true; |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
76 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
77 |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
78 // primary difference with cons is handling of NULL id (this method returns constant) |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
79 // always makes a copy of an array passed |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
80 public static Nodeid fromBinary(byte[] binaryRepresentation, int offset) { |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
81 if (binaryRepresentation == null || binaryRepresentation.length - offset < 20) { |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
82 throw new IllegalArgumentException(); |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
83 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
84 int i = 0; |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
85 while (i < 20 && binaryRepresentation[offset+i] == 0) i++; |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
86 if (i == 20) { |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
87 return NULL; |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
88 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
89 if (offset == 0 && binaryRepresentation.length == 20) { |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
90 return new Nodeid(binaryRepresentation, true); |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
91 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
92 byte[] b = new byte[20]; // create new instance if no other reasonable guesses possible |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
93 System.arraycopy(binaryRepresentation, offset, b, 0, 20); |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
94 return new Nodeid(b, false); |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
95 } |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
96 |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
97 // binascii.unhexlify() |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
98 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
|
99 if (length != 40) { |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
100 throw new IllegalArgumentException(); |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
101 } |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
102 byte[] data = new byte[20]; |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
103 boolean zeroBytes = true; |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
104 for (int i = 0, j = offset; i < data.length; i++) { |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
105 int hiNibble = Character.digit(asciiRepresentation[j++], 16); |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
106 int lowNibble = Character.digit(asciiRepresentation[j++], 16); |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
107 byte b = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
108 data[i] = b; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
109 zeroBytes = zeroBytes && b == 0; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
110 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
111 if (zeroBytes) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
112 return NULL; |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
113 } |
28
b2251b7a9823
Explicit cons arg for clone is more error-prone
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
24
diff
changeset
|
114 return new Nodeid(data, false); |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
115 } |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
116 } |