Mercurial > jhg
comparison src/com/tmate/hgkit/ll/Nodeid.java @ 31:346b66add79d
Basic lookup for incoming changes
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Wed, 12 Jan 2011 00:30:55 +0100 |
parents | b2251b7a9823 |
children | 51bc56c0addd |
comparison
equal
deleted
inserted
replaced
30:de7217a0aa4d | 31:346b66add79d |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2010, 2011 Artem Tikhomirov | 2 * Copyright (c) 2010, 2011 Artem Tikhomirov |
3 */ | 3 */ |
4 package com.tmate.hgkit.ll; | 4 package com.tmate.hgkit.ll; |
5 | |
6 import static com.tmate.hgkit.ll.DigestHelper.toHexString; | |
5 | 7 |
6 import java.util.Arrays; | 8 import java.util.Arrays; |
7 | 9 |
8 | 10 |
9 | 11 |
15 * @author artem | 17 * @author artem |
16 * | 18 * |
17 */ | 19 */ |
18 public final class Nodeid { | 20 public final class Nodeid { |
19 | 21 |
20 public static int NULLREV = -1; | 22 public static final Nodeid NULL = new Nodeid(new byte[20], false); |
21 private final byte[] binaryData; | 23 private final byte[] binaryData; |
22 | 24 |
23 /** | 25 /** |
24 * @param binaryRepresentation - byte[20], kept by reference | 26 * @param binaryRepresentation - byte[20], kept by reference |
25 * @param shallClone - true if array is subject to future modification and shall be copied, not referenced | 27 * @param shallClone - true if array is subject to future modification and shall be copied, not referenced |
51 return Arrays.equals(this.binaryData, buf); | 53 return Arrays.equals(this.binaryData, buf); |
52 } | 54 } |
53 | 55 |
54 @Override | 56 @Override |
55 public String toString() { | 57 public String toString() { |
56 return new DigestHelper().toHexString(binaryData, 0, binaryData.length); | 58 return toHexString(binaryData, 0, binaryData.length); |
57 } | 59 } |
58 | 60 |
61 public String shortNotation() { | |
62 return toHexString(binaryData, 0, 6); | |
63 } | |
64 | |
59 // binascii.unhexlify() | 65 // binascii.unhexlify() |
60 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { | 66 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { |
61 if (length != 40) { | 67 if (length != 40) { |
62 throw new IllegalArgumentException(); | 68 throw new IllegalArgumentException(); |
63 } | 69 } |
64 byte[] data = new byte[20]; | 70 byte[] data = new byte[20]; |
71 boolean zeroBytes = true; | |
65 for (int i = 0, j = offset; i < data.length; i++) { | 72 for (int i = 0, j = offset; i < data.length; i++) { |
66 int hiNibble = Character.digit(asciiRepresentation[j++], 16); | 73 int hiNibble = Character.digit(asciiRepresentation[j++], 16); |
67 int lowNibble = Character.digit(asciiRepresentation[j++], 16); | 74 int lowNibble = Character.digit(asciiRepresentation[j++], 16); |
68 data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); | 75 byte b = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); |
76 data[i] = b; | |
77 zeroBytes = zeroBytes && b == 0; | |
78 } | |
79 if (zeroBytes) { | |
80 return NULL; | |
69 } | 81 } |
70 return new Nodeid(data, false); | 82 return new Nodeid(data, false); |
71 } | 83 } |
72 } | 84 } |