Mercurial > jhg
comparison src/com/tmate/hgkit/ll/Nodeid.java @ 34:51bc56c0addd
Static Nodeid creation methods to reuse NULL id
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 13 Jan 2011 04:03:13 +0100 |
parents | 346b66add79d |
children | 858d1b2458cb |
comparison
equal
deleted
inserted
replaced
33:565ce0835674 | 34:51bc56c0addd |
---|---|
53 return Arrays.equals(this.binaryData, buf); | 53 return Arrays.equals(this.binaryData, buf); |
54 } | 54 } |
55 | 55 |
56 @Override | 56 @Override |
57 public String toString() { | 57 public String toString() { |
58 // XXX may want to output just single 0 for the NULL id? | |
58 return toHexString(binaryData, 0, binaryData.length); | 59 return toHexString(binaryData, 0, binaryData.length); |
59 } | 60 } |
60 | 61 |
61 public String shortNotation() { | 62 public String shortNotation() { |
62 return toHexString(binaryData, 0, 6); | 63 return toHexString(binaryData, 0, 6); |
64 } | |
65 | |
66 public boolean isNull() { | |
67 if (this == NULL) { | |
68 return true; | |
69 } | |
70 for (int i = 0; i < 20; i++) { | |
71 if (this.binaryData[i] != 0) { | |
72 return false; | |
73 } | |
74 } | |
75 return true; | |
76 } | |
77 | |
78 // primary difference with cons is handling of NULL id (this method returns constant) | |
79 // always makes a copy of an array passed | |
80 public static Nodeid fromBinary(byte[] binaryRepresentation, int offset) { | |
81 if (binaryRepresentation == null || binaryRepresentation.length - offset < 20) { | |
82 throw new IllegalArgumentException(); | |
83 } | |
84 int i = 0; | |
85 while (i < 20 && binaryRepresentation[offset+i] == 0) i++; | |
86 if (i == 20) { | |
87 return NULL; | |
88 } | |
89 if (offset == 0 && binaryRepresentation.length == 20) { | |
90 return new Nodeid(binaryRepresentation, true); | |
91 } | |
92 byte[] b = new byte[20]; // create new instance if no other reasonable guesses possible | |
93 System.arraycopy(binaryRepresentation, offset, b, 0, 20); | |
94 return new Nodeid(b, false); | |
63 } | 95 } |
64 | 96 |
65 // binascii.unhexlify() | 97 // binascii.unhexlify() |
66 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { | 98 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { |
67 if (length != 40) { | 99 if (length != 40) { |