Mercurial > jhg
annotate src/org/tmatesoft/hg/core/Nodeid.java @ 141:8248aae33f7d
Adopt FileIterator moving towards WCStatusCollector parameterizing. Improved path handling, move 'em around
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 17 Feb 2011 04:08:34 +0100 |
parents | 26ad21b250e4 |
children | 1a7a9a20e1f9 |
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 /* |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
2 * Copyright (c) 2010-2011 TMate Software Ltd |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
3 * |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
6 * the Free Software Foundation; version 2 of the License. |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
7 * |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
11 * GNU General Public License for more details. |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
12 * |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
13 * For information on how to redistribute this software under |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
14 * the terms of a license other than GNU General Public License |
102
a3a2e5deb320
Updated contact address to support@hg4j.com
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
78
diff
changeset
|
15 * contact TMate Software at support@hg4j.com |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
16 */ |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
17 package org.tmatesoft.hg.core; |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18 |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
19 import static org.tmatesoft.hg.internal.DigestHelper.toHexString; |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
20 |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
21 import java.util.Arrays; |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
22 |
20
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
23 |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
24 |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
25 /** |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
26 * A 20-bytes (40 characters) long hash value to identify a revision. |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
27 * @see http://mercurial.selenic.com/wiki/Nodeid |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
28 * |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
29 * @author Artem Tikhomirov |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
30 * @author TMate Software Ltd. |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
31 * |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
32 */ |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
33 public final class Nodeid { |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
34 |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
35 /** |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
36 * <b>nullid</b>, empty root revision. |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
37 */ |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
38 public static final Nodeid NULL = new Nodeid(new byte[20], false); |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
39 |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
40 private final byte[] binaryData; |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
41 |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
42 /** |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
43 * @param binaryRepresentation - array of exactly 20 bytes |
28
b2251b7a9823
Explicit cons arg for clone is more error-prone
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
24
diff
changeset
|
44 * @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
|
45 */ |
28
b2251b7a9823
Explicit cons arg for clone is more error-prone
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
24
diff
changeset
|
46 public Nodeid(byte[] binaryRepresentation, boolean shallClone) { |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
47 // 5 int fields => 32 bytes |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
48 // byte[20] => 48 bytes |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
49 if (binaryRepresentation == null || binaryRepresentation.length != 20) { |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
50 throw new IllegalArgumentException(); |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
51 } |
28
b2251b7a9823
Explicit cons arg for clone is more error-prone
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
24
diff
changeset
|
52 this.binaryData = shallClone ? binaryRepresentation.clone() : binaryRepresentation; |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
53 } |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
54 |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
55 @Override |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
56 public int hashCode() { |
46
4022c34a4804
Better hashCode impl
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
41
diff
changeset
|
57 // digest (part thereof) seems to be nice candidate for the hashCode |
4022c34a4804
Better hashCode impl
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
41
diff
changeset
|
58 byte[] b = binaryData; |
4022c34a4804
Better hashCode impl
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
41
diff
changeset
|
59 return b[0] << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | (b[3] & 0xFF); |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
60 } |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
61 |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
62 @Override |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
63 public boolean equals(Object o) { |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
64 if (o instanceof Nodeid) { |
125
26ad21b250e4
Explicit op to tell merge revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
65 return this == o || equalsTo(((Nodeid) o).binaryData); |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
66 } |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
67 return false; |
22
603806cd2dc6
Status of local working dir against non-tip base revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
20
diff
changeset
|
68 } |
603806cd2dc6
Status of local working dir against non-tip base revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
20
diff
changeset
|
69 |
603806cd2dc6
Status of local working dir against non-tip base revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
20
diff
changeset
|
70 public boolean equalsTo(byte[] buf) { |
24
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
71 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
|
72 } |
603806cd2dc6
Status of local working dir against non-tip base revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
20
diff
changeset
|
73 |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 @Override |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
75 public String toString() { |
34
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
76 // 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
|
77 return toHexString(binaryData, 0, binaryData.length); |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 } |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
79 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
80 public String shortNotation() { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
81 return toHexString(binaryData, 0, 6); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
82 } |
34
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 public boolean isNull() { |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
85 if (this == NULL) { |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
86 return true; |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
87 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
88 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
|
89 if (this.binaryData[i] != 0) { |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
90 return false; |
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 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
93 return true; |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
94 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
95 |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
96 // copy |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
46
diff
changeset
|
97 public byte[] toByteArray() { |
41
858d1b2458cb
Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
34
diff
changeset
|
98 return binaryData.clone(); |
858d1b2458cb
Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
34
diff
changeset
|
99 } |
858d1b2458cb
Check integrity for bundle changelog. Sort nodeids when calculating hash
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
34
diff
changeset
|
100 |
34
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
101 // 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
|
102 // 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
|
103 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
|
104 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
|
105 throw new IllegalArgumentException(); |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
106 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
107 int i = 0; |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
108 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
|
109 if (i == 20) { |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
110 return NULL; |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
111 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
112 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
|
113 return new Nodeid(binaryRepresentation, true); |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
114 } |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
115 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
|
116 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
|
117 return new Nodeid(b, false); |
51bc56c0addd
Static Nodeid creation methods to reuse NULL id
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
118 } |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
119 |
78
c25c5c348d1b
Skip metadata in the beginning of a file content. Parse metadata, recognize copies/renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
120 public static Nodeid fromAscii(String asciiRepresentation) { |
c25c5c348d1b
Skip metadata in the beginning of a file content. Parse metadata, recognize copies/renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
121 if (asciiRepresentation.length() != 40) { |
c25c5c348d1b
Skip metadata in the beginning of a file content. Parse metadata, recognize copies/renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
122 throw new IllegalArgumentException(); |
c25c5c348d1b
Skip metadata in the beginning of a file content. Parse metadata, recognize copies/renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
123 } |
c25c5c348d1b
Skip metadata in the beginning of a file content. Parse metadata, recognize copies/renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
124 // XXX is better impl for String possible? |
c25c5c348d1b
Skip metadata in the beginning of a file content. Parse metadata, recognize copies/renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
125 return fromAscii(asciiRepresentation.getBytes(), 0, 40); |
c25c5c348d1b
Skip metadata in the beginning of a file content. Parse metadata, recognize copies/renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
126 } |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
127 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
|
128 if (length != 40) { |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
129 throw new IllegalArgumentException(); |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
130 } |
d4fdd1845b3f
Nodeid with array of exactly 20 bytes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
22
diff
changeset
|
131 byte[] data = new byte[20]; |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
132 boolean zeroBytes = true; |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
133 for (int i = 0, j = offset; i < data.length; i++) { |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
134 int hiNibble = Character.digit(asciiRepresentation[j++], 16); |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
135 int lowNibble = Character.digit(asciiRepresentation[j++], 16); |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
136 byte b = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
137 data[i] = b; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
138 zeroBytes = zeroBytes && b == 0; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
139 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
140 if (zeroBytes) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
28
diff
changeset
|
141 return NULL; |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
142 } |
28
b2251b7a9823
Explicit cons arg for clone is more error-prone
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
24
diff
changeset
|
143 return new Nodeid(data, false); |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
144 } |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
145 } |