comparison src/com/tmate/hgkit/ll/Nodeid.java @ 20:11cfabe692b3

Status operation for two repository revisions (no local dir involved)
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 04 Jan 2011 02:08:25 +0100
parents df8c67f3006a
children 603806cd2dc6
comparison
equal deleted inserted replaced
19:40532cdc92fc 20:11cfabe692b3
1 /** 1 /*
2 * Copyright (c) 2010 Artem Tikhomirov 2 * Copyright (c) 2010, 2011 Artem Tikhomirov
3 */ 3 */
4 package com.tmate.hgkit.ll; 4 package com.tmate.hgkit.ll;
5 5
6 import java.util.Arrays;
7
6 8
7 /** 9 /**
8 * @see mercurial/node.py 10 * Whether to store fixed size array (20 bytes) - ease of manipulation (e.g. hashcode/equals), or
11 * memory effective - reuse supplied array, keep significant bits only?
12 * Fixed size array looks most appealing to me now - I doubt one can save any significant amount of memory.
13 * There'd always 20 non-zero bytes, the difference is only for any extra bytes one may pass to constructor
9 * @author artem 14 * @author artem
10 * 15 *
11 */ 16 */
12 public class Nodeid { 17 public final class Nodeid implements Comparable<Nodeid> {
13 18
14 public static int NULLREV = -1; 19 public static int NULLREV = -1;
15 private final byte[] binaryData; 20 private final byte[] binaryData;
16 21
17 public Nodeid(byte[] binaryRepresentation) { 22 public Nodeid(byte[] binaryRepresentation) {
18 // 5 int fields => 32 bytes 23 // 5 int fields => 32 bytes
19 // byte[20] => 48 bytes 24 // byte[20] => 48 bytes
20 this.binaryData = binaryRepresentation; 25 this.binaryData = binaryRepresentation;
21 } 26 }
22 27
28 // instead of hashCode/equals
29 public int compareTo(Nodeid o) {
30 byte[] a1, a2;
31 if (this.binaryData.length != 20) {
32 a1 = new byte[20];
33 System.arraycopy(binaryData, 0, a1, 20 - binaryData.length, binaryData.length);
34 } else {
35 a1 = this.binaryData;
36 }
37
38 if (o.binaryData.length != 20) {
39 a2 = new byte[20];
40 System.arraycopy(o.binaryData, 0, a2, 20 - o.binaryData.length, o.binaryData.length);
41 } else {
42 a2 = o.binaryData;
43 }
44 return Arrays.equals(a1, a2) ? 0 : -1;
45 }
46
23 @Override 47 @Override
24 public String toString() { 48 public String toString() {
25 return new DigestHelper().toHexString(binaryData, 0, binaryData.length); 49 return new DigestHelper().toHexString(binaryData, 0, binaryData.length);
26 } 50 }
27 51
28 // binascii.unhexlify() 52 // binascii.unhexlify()
29 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { 53 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) {
30 assert length % 2 == 0; // Python's binascii.hexlify convert each byte into 2 digits 54 assert length % 2 == 0; // Python's binascii.hexlify convert each byte into 2 digits
31 byte[] data = new byte[length >>> 1]; // XXX use known size instead? nodeid is always 20 bytes 55 byte[] data = new byte[length >>> 1]; // XXX use known size instead? nodeid is always 20 bytes
32 for (int i = 0, j = offset; i < data.length; i++) { 56 for (int i = 0, j = offset; i < data.length; i++) {
34 int lowNibble = Character.digit(asciiRepresentation[j++], 16); 58 int lowNibble = Character.digit(asciiRepresentation[j++], 16);
35 data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); 59 data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF);
36 } 60 }
37 return new Nodeid(data); 61 return new Nodeid(data);
38 } 62 }
39
40
41 } 63 }