1 /* 2 * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org> 3 * and other copyright owners as documented in the project's IP log. 4 * 5 * This program and the accompanying materials are made available 6 * under the terms of the Eclipse Distribution License v1.0 which 7 * accompanies this distribution, is reproduced below, and is 8 * available at http://www.eclipse.org/org/documents/edl-v10.php 9 * 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials provided 22 * with the distribution. 23 * 24 * - Neither the name of the Eclipse Foundation, Inc. nor the 25 * names of its contributors may be used to endorse or promote 26 * products derived from this software without specific prior 27 * written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 */ 43 44 package org.eclipse.jgit.lib; 45 46 import java.io.IOException; 47 import java.io.OutputStream; 48 import java.io.Writer; 49 import java.nio.ByteBuffer; 50 51 import org.eclipse.jgit.util.NB; 52 53 /** 54 * A (possibly mutable) SHA-1 abstraction. 55 * <p> 56 * If this is an instance of {@link MutableObjectId} the concept of equality 57 * with this instance can alter at any time, if this instance is modified to 58 * represent a different object name. 59 */ 60 public abstract class AnyObjectId implements Comparable<AnyObjectId> { 61 62 /** 63 * Compare to object identifier byte sequences for equality. 64 * 65 * @param firstObjectId 66 * the first identifier to compare. Must not be null. 67 * @param secondObjectId 68 * the second identifier to compare. Must not be null. 69 * @return true if the two identifiers are the same. 70 */ 71 public static boolean equals(final AnyObjectId firstObjectId, 72 final AnyObjectId secondObjectId) { 73 if (firstObjectId == secondObjectId) 74 return true; 75 76 // We test word 2 first as odds are someone already used our 77 // word 1 as a hash code, and applying that came up with these 78 // two instances we are comparing for equality. Therefore the 79 // first two words are very likely to be identical. We want to 80 // break away from collisions as quickly as possible. 81 // 82 return firstObjectId.w2 == secondObjectId.w2 83 && firstObjectId.w3 == secondObjectId.w3 84 && firstObjectId.w4 == secondObjectId.w4 85 && firstObjectId.w5 == secondObjectId.w5 86 && firstObjectId.w1 == secondObjectId.w1; 87 } 88 89 int w1; 90 91 int w2; 92 93 int w3; 94 95 int w4; 96 97 int w5; 98 99 /** 100 * Get the first 8 bits of the ObjectId. 101 * 102 * This is a faster version of {@code getByte(0)}. 103 * 104 * @return a discriminator usable for a fan-out style map. Returned values 105 * are unsigned and thus are in the range [0,255] rather than the 106 * signed byte range of [-128, 127]. 107 */ 108 public final int getFirstByte() { 109 return w1 >>> 24; 110 } 111 112 /** 113 * Get any byte from the ObjectId. 114 * 115 * Callers hard-coding {@code getByte(0)} should instead use the much faster 116 * special case variant {@link #getFirstByte()}. 117 * 118 * @param index 119 * index of the byte to obtain from the raw form of the ObjectId. 120 * Must be in range [0, {@link Constants#OBJECT_ID_LENGTH}). 121 * @return the value of the requested byte at {@code index}. Returned values 122 * are unsigned and thus are in the range [0,255] rather than the 123 * signed byte range of [-128, 127]. 124 * @throws ArrayIndexOutOfBoundsException 125 * {@code index} is less than 0, equal to 126 * {@link Constants#OBJECT_ID_LENGTH}, or greater than 127 * {@link Constants#OBJECT_ID_LENGTH}. 128 */ 129 public final int getByte(int index) { 130 int w; 131 switch (index >> 2) { 132 case 0: 133 w = w1; 134 break; 135 case 1: 136 w = w2; 137 break; 138 case 2: 139 w = w3; 140 break; 141 case 3: 142 w = w4; 143 break; 144 case 4: 145 w = w5; 146 break; 147 default: 148 throw new ArrayIndexOutOfBoundsException(index); 149 } 150 151 return (w >>> (8 * (3 - (index & 3)))) & 0xff; 152 } 153 154 /** 155 * Compare this ObjectId to another and obtain a sort ordering. 156 * 157 * @param other 158 * the other id to compare to. Must not be null. 159 * @return < 0 if this id comes before other; 0 if this id is equal to 160 * other; > 0 if this id comes after other. 161 */ 162 @Override 163 public final int compareTo(final AnyObjectId other) { 164 if (this == other) 165 return 0; 166 167 int cmp; 168 169 cmp = NB.compareUInt32(w1, other.w1); 170 if (cmp != 0) 171 return cmp; 172 173 cmp = NB.compareUInt32(w2, other.w2); 174 if (cmp != 0) 175 return cmp; 176 177 cmp = NB.compareUInt32(w3, other.w3); 178 if (cmp != 0) 179 return cmp; 180 181 cmp = NB.compareUInt32(w4, other.w4); 182 if (cmp != 0) 183 return cmp; 184 185 return NB.compareUInt32(w5, other.w5); 186 } 187 188 /** 189 * Compare this ObjectId to a network-byte-order ObjectId. 190 * 191 * @param bs 192 * array containing the other ObjectId in network byte order. 193 * @param p 194 * position within {@code bs} to start the compare at. At least 195 * 20 bytes, starting at this position are required. 196 * @return a negative integer, zero, or a positive integer as this object is 197 * less than, equal to, or greater than the specified object. 198 */ 199 public final int compareTo(final byte[] bs, final int p) { 200 int cmp; 201 202 cmp = NB.compareUInt32(w1, NB.decodeInt32(bs, p)); 203 if (cmp != 0) 204 return cmp; 205 206 cmp = NB.compareUInt32(w2, NB.decodeInt32(bs, p + 4)); 207 if (cmp != 0) 208 return cmp; 209 210 cmp = NB.compareUInt32(w3, NB.decodeInt32(bs, p + 8)); 211 if (cmp != 0) 212 return cmp; 213 214 cmp = NB.compareUInt32(w4, NB.decodeInt32(bs, p + 12)); 215 if (cmp != 0) 216 return cmp; 217 218 return NB.compareUInt32(w5, NB.decodeInt32(bs, p + 16)); 219 } 220 221 /** 222 * Compare this ObjectId to a network-byte-order ObjectId. 223 * 224 * @param bs 225 * array containing the other ObjectId in network byte order. 226 * @param p 227 * position within {@code bs} to start the compare at. At least 5 228 * integers, starting at this position are required. 229 * @return a negative integer, zero, or a positive integer as this object is 230 * less than, equal to, or greater than the specified object. 231 */ 232 public final int compareTo(final int[] bs, final int p) { 233 int cmp; 234 235 cmp = NB.compareUInt32(w1, bs[p]); 236 if (cmp != 0) 237 return cmp; 238 239 cmp = NB.compareUInt32(w2, bs[p + 1]); 240 if (cmp != 0) 241 return cmp; 242 243 cmp = NB.compareUInt32(w3, bs[p + 2]); 244 if (cmp != 0) 245 return cmp; 246 247 cmp = NB.compareUInt32(w4, bs[p + 3]); 248 if (cmp != 0) 249 return cmp; 250 251 return NB.compareUInt32(w5, bs[p + 4]); 252 } 253 254 /** 255 * Tests if this ObjectId starts with the given abbreviation. 256 * 257 * @param abbr 258 * the abbreviation. 259 * @return true if this ObjectId begins with the abbreviation; else false. 260 */ 261 public boolean startsWith(final AbbreviatedObjectId abbr) { 262 return abbr.prefixCompare(this) == 0; 263 } 264 265 @Override 266 public final int hashCode() { 267 return w2; 268 } 269 270 /** 271 * Determine if this ObjectId has exactly the same value as another. 272 * 273 * @param other 274 * the other id to compare to. May be null. 275 * @return true only if both ObjectIds have identical bits. 276 */ 277 public final boolean equals(final AnyObjectId other) { 278 return other != null ? equals(this, other) : false; 279 } 280 281 @Override 282 public final boolean equals(final Object o) { 283 if (o instanceof AnyObjectId) 284 return equals((AnyObjectId) o); 285 else 286 return false; 287 } 288 289 /** 290 * Copy this ObjectId to an output writer in raw binary. 291 * 292 * @param w 293 * the buffer to copy to. Must be in big endian order. 294 */ 295 public void copyRawTo(final ByteBuffer w) { 296 w.putInt(w1); 297 w.putInt(w2); 298 w.putInt(w3); 299 w.putInt(w4); 300 w.putInt(w5); 301 } 302 303 /** 304 * Copy this ObjectId to a byte array. 305 * 306 * @param b 307 * the buffer to copy to. 308 * @param o 309 * the offset within b to write at. 310 */ 311 public void copyRawTo(final byte[] b, final int o) { 312 NB.encodeInt32(b, o, w1); 313 NB.encodeInt32(b, o + 4, w2); 314 NB.encodeInt32(b, o + 8, w3); 315 NB.encodeInt32(b, o + 12, w4); 316 NB.encodeInt32(b, o + 16, w5); 317 } 318 319 /** 320 * Copy this ObjectId to an int array. 321 * 322 * @param b 323 * the buffer to copy to. 324 * @param o 325 * the offset within b to write at. 326 */ 327 public void copyRawTo(final int[] b, final int o) { 328 b[o] = w1; 329 b[o + 1] = w2; 330 b[o + 2] = w3; 331 b[o + 3] = w4; 332 b[o + 4] = w5; 333 } 334 335 /** 336 * Copy this ObjectId to an output writer in raw binary. 337 * 338 * @param w 339 * the stream to write to. 340 * @throws IOException 341 * the stream writing failed. 342 */ 343 public void copyRawTo(final OutputStream w) throws IOException { 344 writeRawInt(w, w1); 345 writeRawInt(w, w2); 346 writeRawInt(w, w3); 347 writeRawInt(w, w4); 348 writeRawInt(w, w5); 349 } 350 351 private static void writeRawInt(final OutputStream w, int v) 352 throws IOException { 353 w.write(v >>> 24); 354 w.write(v >>> 16); 355 w.write(v >>> 8); 356 w.write(v); 357 } 358 359 /** 360 * Copy this ObjectId to an output writer in hex format. 361 * 362 * @param w 363 * the stream to copy to. 364 * @throws IOException 365 * the stream writing failed. 366 */ 367 public void copyTo(final OutputStream w) throws IOException { 368 w.write(toHexByteArray()); 369 } 370 371 /** 372 * Copy this ObjectId to a byte array in hex format. 373 * 374 * @param b 375 * the buffer to copy to. 376 * @param o 377 * the offset within b to write at. 378 */ 379 public void copyTo(byte[] b, int o) { 380 formatHexByte(b, o + 0, w1); 381 formatHexByte(b, o + 8, w2); 382 formatHexByte(b, o + 16, w3); 383 formatHexByte(b, o + 24, w4); 384 formatHexByte(b, o + 32, w5); 385 } 386 387 /** 388 * Copy this ObjectId to a ByteBuffer in hex format. 389 * 390 * @param b 391 * the buffer to copy to. 392 */ 393 public void copyTo(ByteBuffer b) { 394 b.put(toHexByteArray()); 395 } 396 397 private byte[] toHexByteArray() { 398 final byte[] dst = new byte[Constants.OBJECT_ID_STRING_LENGTH]; 399 formatHexByte(dst, 0, w1); 400 formatHexByte(dst, 8, w2); 401 formatHexByte(dst, 16, w3); 402 formatHexByte(dst, 24, w4); 403 formatHexByte(dst, 32, w5); 404 return dst; 405 } 406 407 private static final byte[] hexbyte = { '0', '1', '2', '3', '4', '5', '6', 408 '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; 409 410 private static void formatHexByte(final byte[] dst, final int p, int w) { 411 int o = p + 7; 412 while (o >= p && w != 0) { 413 dst[o--] = hexbyte[w & 0xf]; 414 w >>>= 4; 415 } 416 while (o >= p) 417 dst[o--] = '0'; 418 } 419 420 /** 421 * Copy this ObjectId to an output writer in hex format. 422 * 423 * @param w 424 * the stream to copy to. 425 * @throws IOException 426 * the stream writing failed. 427 */ 428 public void copyTo(final Writer w) throws IOException { 429 w.write(toHexCharArray()); 430 } 431 432 /** 433 * Copy this ObjectId to an output writer in hex format. 434 * 435 * @param tmp 436 * temporary char array to buffer construct into before writing. 437 * Must be at least large enough to hold 2 digits for each byte 438 * of object id (40 characters or larger). 439 * @param w 440 * the stream to copy to. 441 * @throws IOException 442 * the stream writing failed. 443 */ 444 public void copyTo(final char[] tmp, final Writer w) throws IOException { 445 toHexCharArray(tmp); 446 w.write(tmp, 0, Constants.OBJECT_ID_STRING_LENGTH); 447 } 448 449 /** 450 * Copy this ObjectId to a StringBuilder in hex format. 451 * 452 * @param tmp 453 * temporary char array to buffer construct into before writing. 454 * Must be at least large enough to hold 2 digits for each byte 455 * of object id (40 characters or larger). 456 * @param w 457 * the string to append onto. 458 */ 459 public void copyTo(final char[] tmp, final StringBuilder w) { 460 toHexCharArray(tmp); 461 w.append(tmp, 0, Constants.OBJECT_ID_STRING_LENGTH); 462 } 463 464 private char[] toHexCharArray() { 465 final char[] dst = new char[Constants.OBJECT_ID_STRING_LENGTH]; 466 toHexCharArray(dst); 467 return dst; 468 } 469 470 private void toHexCharArray(final char[] dst) { 471 formatHexChar(dst, 0, w1); 472 formatHexChar(dst, 8, w2); 473 formatHexChar(dst, 16, w3); 474 formatHexChar(dst, 24, w4); 475 formatHexChar(dst, 32, w5); 476 } 477 478 private static final char[] hexchar = { '0', '1', '2', '3', '4', '5', '6', 479 '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; 480 481 static void formatHexChar(final char[] dst, final int p, int w) { 482 int o = p + 7; 483 while (o >= p && w != 0) { 484 dst[o--] = hexchar[w & 0xf]; 485 w >>>= 4; 486 } 487 while (o >= p) 488 dst[o--] = '0'; 489 } 490 491 @SuppressWarnings("nls") 492 @Override 493 public String toString() { 494 return "AnyObjectId[" + name() + "]"; 495 } 496 497 /** 498 * @return string form of the SHA-1, in lower case hexadecimal. 499 */ 500 public final String name() { 501 return new String(toHexCharArray()); 502 } 503 504 /** 505 * @return string form of the SHA-1, in lower case hexadecimal. 506 */ 507 public final String getName() { 508 return name(); 509 } 510 511 /** 512 * Return an abbreviation (prefix) of this object SHA-1. 513 * <p> 514 * This implementation does not guarantee uniqueness. Callers should 515 * instead use {@link ObjectReader#abbreviate(AnyObjectId, int)} to obtain a 516 * unique abbreviation within the scope of a particular object database. 517 * 518 * @param len 519 * length of the abbreviated string. 520 * @return SHA-1 abbreviation. 521 */ 522 public AbbreviatedObjectId abbreviate(final int len) { 523 final int a = AbbreviatedObjectId.mask(len, 1, w1); 524 final int b = AbbreviatedObjectId.mask(len, 2, w2); 525 final int c = AbbreviatedObjectId.mask(len, 3, w3); 526 final int d = AbbreviatedObjectId.mask(len, 4, w4); 527 final int e = AbbreviatedObjectId.mask(len, 5, w5); 528 return new AbbreviatedObjectId(len, a, b, c, d, e); 529 } 530 531 /** 532 * Obtain an immutable copy of this current object name value. 533 * <p> 534 * Only returns <code>this</code> if this instance is an unsubclassed 535 * instance of {@link ObjectId}; otherwise a new instance is returned 536 * holding the same value. 537 * <p> 538 * This method is useful to shed any additional memory that may be tied to 539 * the subclass, yet retain the unique identity of the object id for future 540 * lookups within maps and repositories. 541 * 542 * @return an immutable copy, using the smallest memory footprint possible. 543 */ 544 public final ObjectId copy() { 545 if (getClass() == ObjectId.class) 546 return (ObjectId) this; 547 return new ObjectId(this); 548 } 549 550 /** 551 * Obtain an immutable copy of this current object name value. 552 * <p> 553 * See {@link #copy()} if <code>this</code> is a possibly subclassed (but 554 * immutable) identity and the application needs a lightweight identity 555 * <i>only</i> reference. 556 * 557 * @return an immutable copy. May be <code>this</code> if this is already 558 * an immutable instance. 559 */ 560 public abstract ObjectId toObjectId(); 561 }