Mercurial > hg4j
comparison src/com/tmate/hgkit/ll/Nodeid.java @ 28:b2251b7a9823
Explicit cons arg for clone is more error-prone
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 11 Jan 2011 04:34:34 +0100 |
parents | d4fdd1845b3f |
children | 346b66add79d |
comparison
equal
deleted
inserted
replaced
27:b0a15cefdfd6 | 28:b2251b7a9823 |
---|---|
19 | 19 |
20 public static int NULLREV = -1; | 20 public static int NULLREV = -1; |
21 private final byte[] binaryData; | 21 private final byte[] binaryData; |
22 | 22 |
23 /** | 23 /** |
24 * @param binaryRepresentation - byte[20], kept by reference. Use {@link #clone()} if original array may get changed. | 24 * @param binaryRepresentation - byte[20], kept by reference |
25 * @param shallClone - true if array is subject to future modification and shall be copied, not referenced | |
25 */ | 26 */ |
26 public Nodeid(byte[] binaryRepresentation) { | 27 public Nodeid(byte[] binaryRepresentation, boolean shallClone) { |
27 // 5 int fields => 32 bytes | 28 // 5 int fields => 32 bytes |
28 // byte[20] => 48 bytes | 29 // byte[20] => 48 bytes |
29 if (binaryRepresentation == null || binaryRepresentation.length != 20) { | 30 if (binaryRepresentation == null || binaryRepresentation.length != 20) { |
30 throw new IllegalArgumentException(); | 31 throw new IllegalArgumentException(); |
31 } | 32 } |
32 this.binaryData = binaryRepresentation; | 33 this.binaryData = shallClone ? binaryRepresentation.clone() : binaryRepresentation; |
33 } | 34 } |
34 | 35 |
35 @Override | 36 @Override |
36 public int hashCode() { | 37 public int hashCode() { |
37 // TODO consider own impl, especially if byte[] get replaced with 5 ints | 38 // TODO consider own impl, especially if byte[] get replaced with 5 ints |
64 for (int i = 0, j = offset; i < data.length; i++) { | 65 for (int i = 0, j = offset; i < data.length; i++) { |
65 int hiNibble = Character.digit(asciiRepresentation[j++], 16); | 66 int hiNibble = Character.digit(asciiRepresentation[j++], 16); |
66 int lowNibble = Character.digit(asciiRepresentation[j++], 16); | 67 int lowNibble = Character.digit(asciiRepresentation[j++], 16); |
67 data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); | 68 data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); |
68 } | 69 } |
69 return new Nodeid(data); | 70 return new Nodeid(data, false); |
70 } | 71 } |
71 } | 72 } |