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(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
122 public static final AbbreviatedObjectId fromObjectId(AnyObjectId id) {
123 return new AbbreviatedObjectId(Constants.OBJECT_ID_STRING_LENGTH,
124 id.w1, id.w2, id.w3, id.w4, id.w5);
125 }
126
127
128
129
130
131
132
133
134 public static final AbbreviatedObjectId fromString(String str) {
135 if (str.length() > Constants.OBJECT_ID_STRING_LENGTH)
136 throw new IllegalArgumentException(MessageFormat.format(JGitText.get().invalidId, str));
137 final byte[] b = Constants.encodeASCII(str);
138 return fromHexString(b, 0, b.length);
139 }
140
141 private static final AbbreviatedObjectId fromHexString(final byte[] bs,
142 int ptr, final int end) {
143 try {
144 final int a = hexUInt32(bs, ptr, end);
145 final int b = hexUInt32(bs, ptr + 8, end);
146 final int c = hexUInt32(bs, ptr + 16, end);
147 final int d = hexUInt32(bs, ptr + 24, end);
148 final int e = hexUInt32(bs, ptr + 32, end);
149 return new AbbreviatedObjectId(end - ptr, a, b, c, d, e);
150 } catch (ArrayIndexOutOfBoundsException e1) {
151 throw new InvalidObjectIdException(bs, ptr, end - ptr);
152 }
153 }
154
155 private static final int hexUInt32(final byte[] bs, int p, final int end) {
156 if (8 <= end - p)
157 return RawParseUtils.parseHexInt32(bs, p);
158
159 int r = 0, n = 0;
160 while (n < 8 && p < end) {
161 r <<= 4;
162 r |= RawParseUtils.parseHexInt4(bs[p++]);
163 n++;
164 }
165 return r << ((8 - n) * 4);
166 }
167
168 static int mask(int nibbles, int word, int v) {
169 final int b = (word - 1) * 8;
170 if (b + 8 <= nibbles) {
171
172
173 return v;
174 }
175
176 if (nibbles <= b) {
177
178
179 return 0;
180 }
181
182 final int s = 32 - (nibbles - b) * 4;
183 return (v >>> s) << s;
184 }
185
186
187 final int nibbles;
188
189 final int w1;
190
191 final int w2;
192
193 final int w3;
194
195 final int w4;
196
197 final int w5;
198
199 AbbreviatedObjectId(final int n, final int new_1, final int new_2,
200 final int new_3, final int new_4, final int new_5) {
201 nibbles = n;
202 w1 = new_1;
203 w2 = new_2;
204 w3 = new_3;
205 w4 = new_4;
206 w5 = new_5;
207 }
208
209
210
211
212
213
214 public int length() {
215 return nibbles;
216 }
217
218
219
220
221
222
223 public boolean isComplete() {
224 return length() == Constants.OBJECT_ID_STRING_LENGTH;
225 }
226
227
228
229
230
231
232 public ObjectId toObjectId() {
233 return isComplete() ? new ObjectId(w1, w2, w3, w4, w5) : null;
234 }
235
236
237
238
239
240
241
242
243
244
245
246
247 public final int prefixCompare(AnyObjectId other) {
248 int cmp;
249
250 cmp = NB.compareUInt32(w1, mask(1, other.w1));
251 if (cmp != 0)
252 return cmp;
253
254 cmp = NB.compareUInt32(w2, mask(2, other.w2));
255 if (cmp != 0)
256 return cmp;
257
258 cmp = NB.compareUInt32(w3, mask(3, other.w3));
259 if (cmp != 0)
260 return cmp;
261
262 cmp = NB.compareUInt32(w4, mask(4, other.w4));
263 if (cmp != 0)
264 return cmp;
265
266 return NB.compareUInt32(w5, mask(5, other.w5));
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 public final int prefixCompare(byte[] bs, int p) {
284 int cmp;
285
286 cmp = NB.compareUInt32(w1, mask(1, NB.decodeInt32(bs, p)));
287 if (cmp != 0)
288 return cmp;
289
290 cmp = NB.compareUInt32(w2, mask(2, NB.decodeInt32(bs, p + 4)));
291 if (cmp != 0)
292 return cmp;
293
294 cmp = NB.compareUInt32(w3, mask(3, NB.decodeInt32(bs, p + 8)));
295 if (cmp != 0)
296 return cmp;
297
298 cmp = NB.compareUInt32(w4, mask(4, NB.decodeInt32(bs, p + 12)));
299 if (cmp != 0)
300 return cmp;
301
302 return NB.compareUInt32(w5, mask(5, NB.decodeInt32(bs, p + 16)));
303 }
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319 public final int prefixCompare(int[] bs, int p) {
320 int cmp;
321
322 cmp = NB.compareUInt32(w1, mask(1, bs[p]));
323 if (cmp != 0)
324 return cmp;
325
326 cmp = NB.compareUInt32(w2, mask(2, bs[p + 1]));
327 if (cmp != 0)
328 return cmp;
329
330 cmp = NB.compareUInt32(w3, mask(3, bs[p + 2]));
331 if (cmp != 0)
332 return cmp;
333
334 cmp = NB.compareUInt32(w4, mask(4, bs[p + 3]));
335 if (cmp != 0)
336 return cmp;
337
338 return NB.compareUInt32(w5, mask(5, bs[p + 4]));
339 }
340
341
342
343
344
345
346 public final int getFirstByte() {
347 return w1 >>> 24;
348 }
349
350 private int mask(int word, int v) {
351 return mask(nibbles, word, v);
352 }
353
354
355 @Override
356 public int hashCode() {
357 return w1;
358 }
359
360
361 @Override
362 public boolean equals(Object o) {
363 if (o instanceof AbbreviatedObjectId) {
364 final AbbreviatedObjectId/../org/eclipse/jgit/lib/AbbreviatedObjectId.html#AbbreviatedObjectId">AbbreviatedObjectId b = (AbbreviatedObjectId) o;
365 return nibbles == b.nibbles && w1 == b.w1 && w2 == b.w2
366 && w3 == b.w3 && w4 == b.w4 && w5 == b.w5;
367 }
368 return false;
369 }
370
371
372
373
374
375
376 public final String name() {
377 final char[] b = new char[Constants.OBJECT_ID_STRING_LENGTH];
378
379 AnyObjectId.formatHexChar(b, 0, w1);
380 if (nibbles <= 8)
381 return new String(b, 0, nibbles);
382
383 AnyObjectId.formatHexChar(b, 8, w2);
384 if (nibbles <= 16)
385 return new String(b, 0, nibbles);
386
387 AnyObjectId.formatHexChar(b, 16, w3);
388 if (nibbles <= 24)
389 return new String(b, 0, nibbles);
390
391 AnyObjectId.formatHexChar(b, 24, w4);
392 if (nibbles <= 32)
393 return new String(b, 0, nibbles);
394
395 AnyObjectId.formatHexChar(b, 32, w5);
396 return new String(b, 0, nibbles);
397 }
398
399
400 @SuppressWarnings("nls")
401 @Override
402 public String toString() {
403 return "AbbreviatedObjectId[" + name() + "]";
404 }
405 }