1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.lib;
12
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import java.io.Writer;
16 import java.nio.ByteBuffer;
17
18 import org.eclipse.jgit.util.NB;
19 import org.eclipse.jgit.util.References;
20
21
22
23
24
25
26
27
28 public abstract class AnyObjectId implements Comparable<AnyObjectId> {
29
30
31
32
33
34
35
36
37
38
39
40 @Deprecated
41 @SuppressWarnings("AmbiguousMethodReference")
42 public static boolean equals(final AnyObjectId firstObjectId,
43 final AnyObjectId secondObjectId) {
44 return isEqual(firstObjectId, secondObjectId);
45 }
46
47
48
49
50
51
52
53
54
55
56
57 public static boolean isEqual(final AnyObjectId firstObjectId,
58 final AnyObjectId secondObjectId) {
59 if (References.isSameObject(firstObjectId, secondObjectId)) {
60 return true;
61 }
62
63
64
65
66
67
68
69 return firstObjectId.w3 == secondObjectId.w3
70 && firstObjectId.w4 == secondObjectId.w4
71 && firstObjectId.w5 == secondObjectId.w5
72 && firstObjectId.w1 == secondObjectId.w1
73 && firstObjectId.w2 == secondObjectId.w2;
74 }
75
76 int w1;
77
78 int w2;
79
80 int w3;
81
82 int w4;
83
84 int w5;
85
86
87
88
89
90
91
92
93
94
95 public final int getFirstByte() {
96 return w1 >>> 24;
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 public final int getByte(int index) {
119 int w;
120 switch (index >> 2) {
121 case 0:
122 w = w1;
123 break;
124 case 1:
125 w = w2;
126 break;
127 case 2:
128 w = w3;
129 break;
130 case 3:
131 w = w4;
132 break;
133 case 4:
134 w = w5;
135 break;
136 default:
137 throw new ArrayIndexOutOfBoundsException(index);
138 }
139
140 return (w >>> (8 * (3 - (index & 3)))) & 0xff;
141 }
142
143
144
145
146
147
148 @Override
149 public final int compareTo(AnyObjectId other) {
150 if (this == other)
151 return 0;
152
153 int cmp;
154
155 cmp = NB.compareUInt32(w1, other.w1);
156 if (cmp != 0)
157 return cmp;
158
159 cmp = NB.compareUInt32(w2, other.w2);
160 if (cmp != 0)
161 return cmp;
162
163 cmp = NB.compareUInt32(w3, other.w3);
164 if (cmp != 0)
165 return cmp;
166
167 cmp = NB.compareUInt32(w4, other.w4);
168 if (cmp != 0)
169 return cmp;
170
171 return NB.compareUInt32(w5, other.w5);
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185 public final int compareTo(byte[] bs, int p) {
186 int cmp;
187
188 cmp = NB.compareUInt32(w1, NB.decodeInt32(bs, p));
189 if (cmp != 0)
190 return cmp;
191
192 cmp = NB.compareUInt32(w2, NB.decodeInt32(bs, p + 4));
193 if (cmp != 0)
194 return cmp;
195
196 cmp = NB.compareUInt32(w3, NB.decodeInt32(bs, p + 8));
197 if (cmp != 0)
198 return cmp;
199
200 cmp = NB.compareUInt32(w4, NB.decodeInt32(bs, p + 12));
201 if (cmp != 0)
202 return cmp;
203
204 return NB.compareUInt32(w5, NB.decodeInt32(bs, p + 16));
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218 public final int compareTo(int[] bs, int p) {
219 int cmp;
220
221 cmp = NB.compareUInt32(w1, bs[p]);
222 if (cmp != 0)
223 return cmp;
224
225 cmp = NB.compareUInt32(w2, bs[p + 1]);
226 if (cmp != 0)
227 return cmp;
228
229 cmp = NB.compareUInt32(w3, bs[p + 2]);
230 if (cmp != 0)
231 return cmp;
232
233 cmp = NB.compareUInt32(w4, bs[p + 3]);
234 if (cmp != 0)
235 return cmp;
236
237 return NB.compareUInt32(w5, bs[p + 4]);
238 }
239
240
241
242
243
244
245
246
247 public boolean startsWith(AbbreviatedObjectId abbr) {
248 return abbr.prefixCompare(this) == 0;
249 }
250
251
252 @Override
253 public final int hashCode() {
254 return w2;
255 }
256
257
258
259
260
261
262
263
264 @SuppressWarnings({ "NonOverridingEquals", "AmbiguousMethodReference" })
265 public final boolean equals(AnyObjectId other) {
266 return other != null ? isEqual(this, other) : false;
267 }
268
269
270 @Override
271 public final boolean equals(Object o) {
272 if (o instanceof AnyObjectId) {
273 return equals((AnyObjectId) o);
274 }
275 return false;
276 }
277
278
279
280
281
282
283
284 public void copyRawTo(ByteBuffer w) {
285 w.putInt(w1);
286 w.putInt(w2);
287 w.putInt(w3);
288 w.putInt(w4);
289 w.putInt(w5);
290 }
291
292
293
294
295
296
297
298
299
300 public void copyRawTo(byte[] b, int o) {
301 NB.encodeInt32(b, o, w1);
302 NB.encodeInt32(b, o + 4, w2);
303 NB.encodeInt32(b, o + 8, w3);
304 NB.encodeInt32(b, o + 12, w4);
305 NB.encodeInt32(b, o + 16, w5);
306 }
307
308
309
310
311
312
313
314
315
316 public void copyRawTo(int[] b, int o) {
317 b[o] = w1;
318 b[o + 1] = w2;
319 b[o + 2] = w3;
320 b[o + 3] = w4;
321 b[o + 4] = w5;
322 }
323
324
325
326
327
328
329
330
331
332 public void copyRawTo(OutputStream w) throws IOException {
333 writeRawInt(w, w1);
334 writeRawInt(w, w2);
335 writeRawInt(w, w3);
336 writeRawInt(w, w4);
337 writeRawInt(w, w5);
338 }
339
340 private static void writeRawInt(OutputStream w, int v)
341 throws IOException {
342 w.write(v >>> 24);
343 w.write(v >>> 16);
344 w.write(v >>> 8);
345 w.write(v);
346 }
347
348
349
350
351
352
353
354
355
356 public void copyTo(OutputStream w) throws IOException {
357 w.write(toHexByteArray());
358 }
359
360
361
362
363
364
365
366
367
368 public void copyTo(byte[] b, int o) {
369 formatHexByte(b, o + 0, w1);
370 formatHexByte(b, o + 8, w2);
371 formatHexByte(b, o + 16, w3);
372 formatHexByte(b, o + 24, w4);
373 formatHexByte(b, o + 32, w5);
374 }
375
376
377
378
379
380
381
382 public void copyTo(ByteBuffer b) {
383 b.put(toHexByteArray());
384 }
385
386 private byte[] toHexByteArray() {
387 final byte[] dst = new byte[Constants.OBJECT_ID_STRING_LENGTH];
388 formatHexByte(dst, 0, w1);
389 formatHexByte(dst, 8, w2);
390 formatHexByte(dst, 16, w3);
391 formatHexByte(dst, 24, w4);
392 formatHexByte(dst, 32, w5);
393 return dst;
394 }
395
396 private static final byte[] hexbyte = { '0', '1', '2', '3', '4', '5', '6',
397 '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
398
399 private static void formatHexByte(byte[] dst, int p, int w) {
400 int o = p + 7;
401 while (o >= p && w != 0) {
402 dst[o--] = hexbyte[w & 0xf];
403 w >>>= 4;
404 }
405 while (o >= p)
406 dst[o--] = '0';
407 }
408
409
410
411
412
413
414
415
416
417 public void copyTo(Writer w) throws IOException {
418 w.write(toHexCharArray());
419 }
420
421
422
423
424
425
426
427
428
429
430
431
432
433 public void copyTo(char[] tmp, Writer w) throws IOException {
434 toHexCharArray(tmp);
435 w.write(tmp, 0, Constants.OBJECT_ID_STRING_LENGTH);
436 }
437
438
439
440
441
442
443
444
445
446
447
448 public void copyTo(char[] tmp, StringBuilder w) {
449 toHexCharArray(tmp);
450 w.append(tmp, 0, Constants.OBJECT_ID_STRING_LENGTH);
451 }
452
453 private char[] toHexCharArray() {
454 final char[] dst = new char[Constants.OBJECT_ID_STRING_LENGTH];
455 toHexCharArray(dst);
456 return dst;
457 }
458
459 private void toHexCharArray(char[] dst) {
460 formatHexChar(dst, 0, w1);
461 formatHexChar(dst, 8, w2);
462 formatHexChar(dst, 16, w3);
463 formatHexChar(dst, 24, w4);
464 formatHexChar(dst, 32, w5);
465 }
466
467 private static final char[] hexchar = { '0', '1', '2', '3', '4', '5', '6',
468 '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
469
470 static void formatHexChar(char[] dst, int p, int w) {
471 int o = p + 7;
472 while (o >= p && w != 0) {
473 dst[o--] = hexchar[w & 0xf];
474 w >>>= 4;
475 }
476 while (o >= p)
477 dst[o--] = '0';
478 }
479
480
481 @SuppressWarnings("nls")
482 @Override
483 public String toString() {
484 return "AnyObjectId[" + name() + "]";
485 }
486
487
488
489
490
491
492 public final String name() {
493 return new String(toHexCharArray());
494 }
495
496
497
498
499
500
501 public final String getName() {
502 return name();
503 }
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518 public AbbreviatedObjectId abbreviate(int len) {
519 final int a = AbbreviatedObjectId.mask(len, 1, w1);
520 final int b = AbbreviatedObjectId.mask(len, 2, w2);
521 final int c = AbbreviatedObjectId.mask(len, 3, w3);
522 final int d = AbbreviatedObjectId.mask(len, 4, w4);
523 final int e = AbbreviatedObjectId.mask(len, 5, w5);
524 return new AbbreviatedObjectId(len, a, b, c, d, e);
525 }
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540 public final ObjectId copy() {
541 if (getClass() == ObjectId.class)
542 return (ObjectId) this;
543 return new ObjectId(this);
544 }
545
546
547
548
549
550
551
552
553
554
555
556 public abstract ObjectId toObjectId();
557 }