Mercurial > hg4j
comparison src/com/tmate/hgkit/ll/Nodeid.java @ 22:603806cd2dc6
Status of local working dir against non-tip base revision
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 06 Jan 2011 03:30:20 +0100 |
parents | 11cfabe692b3 |
children | d4fdd1845b3f |
comparison
equal
deleted
inserted
replaced
21:e929cecae4e1 | 22:603806cd2dc6 |
---|---|
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 | 5 |
6 import java.util.Arrays; | |
7 | 6 |
8 | 7 |
9 /** | 8 /** |
10 * Whether to store fixed size array (20 bytes) - ease of manipulation (e.g. hashcode/equals), or | 9 * 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? | 10 * memory effective - reuse supplied array, keep significant bits only? |
25 this.binaryData = binaryRepresentation; | 24 this.binaryData = binaryRepresentation; |
26 } | 25 } |
27 | 26 |
28 // instead of hashCode/equals | 27 // instead of hashCode/equals |
29 public int compareTo(Nodeid o) { | 28 public int compareTo(Nodeid o) { |
30 byte[] a1, a2; | 29 return equals(this.binaryData, o.binaryData) ? 0 : -1; |
31 if (this.binaryData.length != 20) { | 30 } |
32 a1 = new byte[20]; | 31 |
33 System.arraycopy(binaryData, 0, a1, 20 - binaryData.length, binaryData.length); | 32 public boolean equalsTo(byte[] buf) { |
34 } else { | 33 return equals(this.binaryData, buf); |
35 a1 = this.binaryData; | 34 } |
35 | |
36 private static boolean equals(byte[] a1, byte[] a2) { | |
37 if (a1 == null || a1.length < 20 || a2 == null || a2.length < 20) { | |
38 throw new IllegalArgumentException(); | |
36 } | 39 } |
37 | 40 // assume significant bits are at the end of the array |
38 if (o.binaryData.length != 20) { | 41 final int s1 = a1.length - 20, s2 = a2.length - 20; |
39 a2 = new byte[20]; | 42 for (int i = 0; i < 20; i++) { |
40 System.arraycopy(o.binaryData, 0, a2, 20 - o.binaryData.length, o.binaryData.length); | 43 if (a1[s1+i] != a2[s2+i]) { |
41 } else { | 44 return false; |
42 a2 = o.binaryData; | 45 } |
43 } | 46 } |
44 return Arrays.equals(a1, a2) ? 0 : -1; | 47 return true; |
45 } | 48 } |
46 | 49 |
47 @Override | 50 @Override |
48 public String toString() { | 51 public String toString() { |
49 return new DigestHelper().toHexString(binaryData, 0, binaryData.length); | 52 return new DigestHelper().toHexString(binaryData, 0, binaryData.length); |