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