1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.lib;
45
46 import java.io.Serializable;
47 import java.text.MessageFormat;
48
49 import org.eclipse.jgit.errors.InvalidObjectIdException;
50 import org.eclipse.jgit.internal.JGitText;
51 import org.eclipse.jgit.util.NB;
52 import org.eclipse.jgit.util.RawParseUtils;
53
54
55
56
57
58
59
60
61
62
63
64
65 public final class AbbreviatedObjectId implements Serializable {
66 private static final long serialVersionUID = 1L;
67
68
69
70
71
72
73
74
75
76
77 public static final boolean isId(final String id) {
78 if (id.length() < 2 || Constants.OBJECT_ID_STRING_LENGTH < id.length())
79 return false;
80 try {
81 for (int i = 0; i < id.length(); i++)
82 RawParseUtils.parseHexInt4((byte) id.charAt(i));
83 return true;
84 } catch (ArrayIndexOutOfBoundsException e) {
85 return false;
86 }
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public static final AbbreviatedObjectId fromString(final byte[] buf,
102 final int offset, final int end) {
103 if (end - offset > Constants.OBJECT_ID_STRING_LENGTH)
104 throw new IllegalArgumentException(MessageFormat.format(
105 JGitText.get().invalidIdLength,
106 Integer.valueOf(end - offset),
107 Integer.valueOf(Constants.OBJECT_ID_STRING_LENGTH)));
108 return fromHexString(buf, offset, end);
109 }
110
111
112
113
114
115
116
117
118
119
120
121 public static final AbbreviatedObjectId fromObjectId(AnyObjectId id) {
122 return new AbbreviatedObjectId(Constants.OBJECT_ID_STRING_LENGTH,
123 id.w1, id.w2, id.w3, id.w4, id.w5);
124 }
125
126
127
128
129
130
131
132
133 public static final AbbreviatedObjectId fromString(final String str) {
134 if (str.length() > Constants.OBJECT_ID_STRING_LENGTH)
135 throw new IllegalArgumentException(MessageFormat.format(JGitText.get().invalidId, str));
136 final byte[] b = Constants.encodeASCII(str);
137 return fromHexString(b, 0, b.length);
138 }
139
140 private static final AbbreviatedObjectId fromHexString(final byte[] bs,
141 int ptr, final int end) {
142 try {
143 final int a = hexUInt32(bs, ptr, end);
144 final int b = hexUInt32(bs, ptr + 8, end);
145 final int c = hexUInt32(bs, ptr + 16, end);
146 final int d = hexUInt32(bs, ptr + 24, end);
147 final int e = hexUInt32(bs, ptr + 32, end);
148 return new AbbreviatedObjectId(end - ptr, a, b, c, d, e);
149 } catch (ArrayIndexOutOfBoundsException e1) {
150 throw new InvalidObjectIdException(bs, ptr, end - ptr);
151 }
152 }
153
154 private static final int hexUInt32(final byte[] bs, int p, final int end) {
155 if (8 <= end - p)
156 return RawParseUtils.parseHexInt32(bs, p);
157
158 int r = 0, n = 0;
159 while (n < 8 && p < end) {
160 r <<= 4;
161 r |= RawParseUtils.parseHexInt4(bs[p++]);
162 n++;
163 }
164 return r << (8 - n) * 4;
165 }
166
167 static int mask(final int nibbles, final int word, final int v) {
168 final int b = (word - 1) * 8;
169 if (b + 8 <= nibbles) {
170
171
172 return v;
173 }
174
175 if (nibbles <= b) {
176
177
178 return 0;
179 }
180
181 final int s = 32 - (nibbles - b) * 4;
182 return (v >>> s) << s;
183 }
184
185
186 final int nibbles;
187
188 final int w1;
189
190 final int w2;
191
192 final int w3;
193
194 final int w4;
195
196 final int w5;
197
198 AbbreviatedObjectId(final int n, final int new_1, final int new_2,
199 final int new_3, final int new_4, final int new_5) {
200 nibbles = n;
201 w1 = new_1;
202 w2 = new_2;
203 w3 = new_3;
204 w4 = new_4;
205 w5 = new_5;
206 }
207
208
209 public int length() {
210 return nibbles;
211 }
212
213
214 public boolean isComplete() {
215 return length() == Constants.OBJECT_ID_STRING_LENGTH;
216 }
217
218
219 public ObjectId toObjectId() {
220 return isComplete() ? new ObjectId(w1, w2, w3, w4, w5) : null;
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234 public final int prefixCompare(final AnyObjectId other) {
235 int cmp;
236
237 cmp = NB.compareUInt32(w1, mask(1, other.w1));
238 if (cmp != 0)
239 return cmp;
240
241 cmp = NB.compareUInt32(w2, mask(2, other.w2));
242 if (cmp != 0)
243 return cmp;
244
245 cmp = NB.compareUInt32(w3, mask(3, other.w3));
246 if (cmp != 0)
247 return cmp;
248
249 cmp = NB.compareUInt32(w4, mask(4, other.w4));
250 if (cmp != 0)
251 return cmp;
252
253 return NB.compareUInt32(w5, mask(5, other.w5));
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270 public final int prefixCompare(final byte[] bs, final int p) {
271 int cmp;
272
273 cmp = NB.compareUInt32(w1, mask(1, NB.decodeInt32(bs, p)));
274 if (cmp != 0)
275 return cmp;
276
277 cmp = NB.compareUInt32(w2, mask(2, NB.decodeInt32(bs, p + 4)));
278 if (cmp != 0)
279 return cmp;
280
281 cmp = NB.compareUInt32(w3, mask(3, NB.decodeInt32(bs, p + 8)));
282 if (cmp != 0)
283 return cmp;
284
285 cmp = NB.compareUInt32(w4, mask(4, NB.decodeInt32(bs, p + 12)));
286 if (cmp != 0)
287 return cmp;
288
289 return NB.compareUInt32(w5, mask(5, NB.decodeInt32(bs, p + 16)));
290 }
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306 public final int prefixCompare(final int[] bs, final int p) {
307 int cmp;
308
309 cmp = NB.compareUInt32(w1, mask(1, bs[p]));
310 if (cmp != 0)
311 return cmp;
312
313 cmp = NB.compareUInt32(w2, mask(2, bs[p + 1]));
314 if (cmp != 0)
315 return cmp;
316
317 cmp = NB.compareUInt32(w3, mask(3, bs[p + 2]));
318 if (cmp != 0)
319 return cmp;
320
321 cmp = NB.compareUInt32(w4, mask(4, bs[p + 3]));
322 if (cmp != 0)
323 return cmp;
324
325 return NB.compareUInt32(w5, mask(5, bs[p + 4]));
326 }
327
328
329 public final int getFirstByte() {
330 return w1 >>> 24;
331 }
332
333 private int mask(final int word, final int v) {
334 return mask(nibbles, word, v);
335 }
336
337 @Override
338 public int hashCode() {
339 return w1;
340 }
341
342 @Override
343 public boolean equals(final Object o) {
344 if (o instanceof AbbreviatedObjectId) {
345 final AbbreviatedObjectId b = (AbbreviatedObjectId) o;
346 return nibbles == b.nibbles && w1 == b.w1 && w2 == b.w2
347 && w3 == b.w3 && w4 == b.w4 && w5 == b.w5;
348 }
349 return false;
350 }
351
352
353
354
355 public final String name() {
356 final char[] b = new char[Constants.OBJECT_ID_STRING_LENGTH];
357
358 AnyObjectId.formatHexChar(b, 0, w1);
359 if (nibbles <= 8)
360 return new String(b, 0, nibbles);
361
362 AnyObjectId.formatHexChar(b, 8, w2);
363 if (nibbles <= 16)
364 return new String(b, 0, nibbles);
365
366 AnyObjectId.formatHexChar(b, 16, w3);
367 if (nibbles <= 24)
368 return new String(b, 0, nibbles);
369
370 AnyObjectId.formatHexChar(b, 24, w4);
371 if (nibbles <= 32)
372 return new String(b, 0, nibbles);
373
374 AnyObjectId.formatHexChar(b, 32, w5);
375 return new String(b, 0, nibbles);
376 }
377
378 @SuppressWarnings("nls")
379 @Override
380 public String toString() {
381 return "AbbreviatedObjectId[" + name() + "]";
382 }
383 }