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