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