tikhomirov@20: /* tikhomirov@20: * Copyright (c) 2010, 2011 Artem Tikhomirov tikhomirov@0: */ tikhomirov@0: package com.tmate.hgkit.ll; tikhomirov@0: tikhomirov@31: import static com.tmate.hgkit.ll.DigestHelper.toHexString; tikhomirov@31: tikhomirov@24: import java.util.Arrays; tikhomirov@24: tikhomirov@20: tikhomirov@0: tikhomirov@0: /** tikhomirov@20: * Whether to store fixed size array (20 bytes) - ease of manipulation (e.g. hashcode/equals), or tikhomirov@20: * memory effective - reuse supplied array, keep significant bits only? tikhomirov@20: * Fixed size array looks most appealing to me now - I doubt one can save any significant amount of memory. tikhomirov@20: * There'd always 20 non-zero bytes, the difference is only for any extra bytes one may pass to constructor tikhomirov@0: * @author artem tikhomirov@0: * tikhomirov@0: */ tikhomirov@24: public final class Nodeid { tikhomirov@0: tikhomirov@31: public static final Nodeid NULL = new Nodeid(new byte[20], false); tikhomirov@0: private final byte[] binaryData; tikhomirov@0: tikhomirov@24: /** tikhomirov@28: * @param binaryRepresentation - byte[20], kept by reference tikhomirov@28: * @param shallClone - true if array is subject to future modification and shall be copied, not referenced tikhomirov@24: */ tikhomirov@28: public Nodeid(byte[] binaryRepresentation, boolean shallClone) { tikhomirov@0: // 5 int fields => 32 bytes tikhomirov@0: // byte[20] => 48 bytes tikhomirov@24: if (binaryRepresentation == null || binaryRepresentation.length != 20) { tikhomirov@24: throw new IllegalArgumentException(); tikhomirov@24: } tikhomirov@28: this.binaryData = shallClone ? binaryRepresentation.clone() : binaryRepresentation; tikhomirov@0: } tikhomirov@0: tikhomirov@24: @Override tikhomirov@24: public int hashCode() { tikhomirov@24: // TODO consider own impl, especially if byte[] get replaced with 5 ints tikhomirov@24: return Arrays.hashCode(binaryData); tikhomirov@24: } tikhomirov@24: tikhomirov@24: @Override tikhomirov@24: public boolean equals(Object o) { tikhomirov@24: if (o instanceof Nodeid) { tikhomirov@24: return Arrays.equals(this.binaryData, ((Nodeid) o).binaryData); tikhomirov@24: } tikhomirov@24: return false; tikhomirov@22: } tikhomirov@22: tikhomirov@22: public boolean equalsTo(byte[] buf) { tikhomirov@24: return Arrays.equals(this.binaryData, buf); tikhomirov@22: } tikhomirov@22: tikhomirov@0: @Override tikhomirov@0: public String toString() { tikhomirov@34: // XXX may want to output just single 0 for the NULL id? tikhomirov@31: return toHexString(binaryData, 0, binaryData.length); tikhomirov@0: } tikhomirov@31: tikhomirov@31: public String shortNotation() { tikhomirov@31: return toHexString(binaryData, 0, 6); tikhomirov@31: } tikhomirov@34: tikhomirov@34: public boolean isNull() { tikhomirov@34: if (this == NULL) { tikhomirov@34: return true; tikhomirov@34: } tikhomirov@34: for (int i = 0; i < 20; i++) { tikhomirov@34: if (this.binaryData[i] != 0) { tikhomirov@34: return false; tikhomirov@34: } tikhomirov@34: } tikhomirov@34: return true; tikhomirov@34: } tikhomirov@34: tikhomirov@41: // primary purpose is to give DigestHelper access to internal structure. Despite it's friends-only (package visibility), it's still makes sense to tikhomirov@41: // return a copy, to avoid any accidental modification (same reason field is not made visible, nor any callback, e.g. Output.write(byte[]) was introduced) tikhomirov@41: /*package-local*/byte[] cloneData() { tikhomirov@41: return binaryData.clone(); tikhomirov@41: } tikhomirov@41: tikhomirov@34: // primary difference with cons is handling of NULL id (this method returns constant) tikhomirov@34: // always makes a copy of an array passed tikhomirov@34: public static Nodeid fromBinary(byte[] binaryRepresentation, int offset) { tikhomirov@34: if (binaryRepresentation == null || binaryRepresentation.length - offset < 20) { tikhomirov@34: throw new IllegalArgumentException(); tikhomirov@34: } tikhomirov@34: int i = 0; tikhomirov@34: while (i < 20 && binaryRepresentation[offset+i] == 0) i++; tikhomirov@34: if (i == 20) { tikhomirov@34: return NULL; tikhomirov@34: } tikhomirov@34: if (offset == 0 && binaryRepresentation.length == 20) { tikhomirov@34: return new Nodeid(binaryRepresentation, true); tikhomirov@34: } tikhomirov@34: byte[] b = new byte[20]; // create new instance if no other reasonable guesses possible tikhomirov@34: System.arraycopy(binaryRepresentation, offset, b, 0, 20); tikhomirov@34: return new Nodeid(b, false); tikhomirov@34: } tikhomirov@31: tikhomirov@0: // binascii.unhexlify() tikhomirov@0: public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { tikhomirov@24: if (length != 40) { tikhomirov@24: throw new IllegalArgumentException(); tikhomirov@24: } tikhomirov@24: byte[] data = new byte[20]; tikhomirov@31: boolean zeroBytes = true; tikhomirov@0: for (int i = 0, j = offset; i < data.length; i++) { tikhomirov@0: int hiNibble = Character.digit(asciiRepresentation[j++], 16); tikhomirov@0: int lowNibble = Character.digit(asciiRepresentation[j++], 16); tikhomirov@31: byte b = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); tikhomirov@31: data[i] = b; tikhomirov@31: zeroBytes = zeroBytes && b == 0; tikhomirov@31: } tikhomirov@31: if (zeroBytes) { tikhomirov@31: return NULL; tikhomirov@0: } tikhomirov@28: return new Nodeid(data, false); tikhomirov@0: } tikhomirov@0: }