You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnyObjectId.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. package org.eclipse.jgit.lib;
  44. import java.io.IOException;
  45. import java.io.OutputStream;
  46. import java.io.Writer;
  47. import java.nio.ByteBuffer;
  48. import org.eclipse.jgit.util.NB;
  49. /**
  50. * A (possibly mutable) SHA-1 abstraction.
  51. * <p>
  52. * If this is an instance of {@link MutableObjectId} the concept of equality
  53. * with this instance can alter at any time, if this instance is modified to
  54. * represent a different object name.
  55. */
  56. public abstract class AnyObjectId implements Comparable {
  57. /**
  58. * Compare to object identifier byte sequences for equality.
  59. *
  60. * @param firstObjectId
  61. * the first identifier to compare. Must not be null.
  62. * @param secondObjectId
  63. * the second identifier to compare. Must not be null.
  64. * @return true if the two identifiers are the same.
  65. */
  66. public static boolean equals(final AnyObjectId firstObjectId,
  67. final AnyObjectId secondObjectId) {
  68. if (firstObjectId == secondObjectId)
  69. return true;
  70. // We test word 2 first as odds are someone already used our
  71. // word 1 as a hash code, and applying that came up with these
  72. // two instances we are comparing for equality. Therefore the
  73. // first two words are very likely to be identical. We want to
  74. // break away from collisions as quickly as possible.
  75. //
  76. return firstObjectId.w2 == secondObjectId.w2
  77. && firstObjectId.w3 == secondObjectId.w3
  78. && firstObjectId.w4 == secondObjectId.w4
  79. && firstObjectId.w5 == secondObjectId.w5
  80. && firstObjectId.w1 == secondObjectId.w1;
  81. }
  82. int w1;
  83. int w2;
  84. int w3;
  85. int w4;
  86. int w5;
  87. /**
  88. * Get the first 8 bits of the ObjectId.
  89. *
  90. * This is a faster version of {@code getByte(0)}.
  91. *
  92. * @return a discriminator usable for a fan-out style map. Returned values
  93. * are unsigned and thus are in the range [0,255] rather than the
  94. * signed byte range of [-128, 127].
  95. */
  96. public final int getFirstByte() {
  97. return w1 >>> 24;
  98. }
  99. /**
  100. * Get any byte from the ObjectId.
  101. *
  102. * Callers hard-coding {@code getByte(0)} should instead use the much faster
  103. * special case variant {@link #getFirstByte()}.
  104. *
  105. * @param index
  106. * index of the byte to obtain from the raw form of the ObjectId.
  107. * Must be in range [0, {@link Constants#OBJECT_ID_LENGTH}).
  108. * @return the value of the requested byte at {@code index}. Returned values
  109. * are unsigned and thus are in the range [0,255] rather than the
  110. * signed byte range of [-128, 127].
  111. * @throws ArrayIndexOutOfBoundsException
  112. * {@code index} is less than 0, equal to
  113. * {@link Constants#OBJECT_ID_LENGTH}, or greater than
  114. * {@link Constants#OBJECT_ID_LENGTH}.
  115. */
  116. public final int getByte(int index) {
  117. int w;
  118. switch (index >> 2) {
  119. case 0:
  120. w = w1;
  121. break;
  122. case 1:
  123. w = w2;
  124. break;
  125. case 2:
  126. w = w3;
  127. break;
  128. case 3:
  129. w = w4;
  130. break;
  131. case 4:
  132. w = w5;
  133. break;
  134. default:
  135. throw new ArrayIndexOutOfBoundsException(index);
  136. }
  137. return (w >>> (8 * (3 - (index & 3)))) & 0xff;
  138. }
  139. /**
  140. * Compare this ObjectId to another and obtain a sort ordering.
  141. *
  142. * @param other
  143. * the other id to compare to. Must not be null.
  144. * @return < 0 if this id comes before other; 0 if this id is equal to
  145. * other; > 0 if this id comes after other.
  146. */
  147. public final int compareTo(final AnyObjectId other) {
  148. if (this == other)
  149. return 0;
  150. int cmp;
  151. cmp = NB.compareUInt32(w1, other.w1);
  152. if (cmp != 0)
  153. return cmp;
  154. cmp = NB.compareUInt32(w2, other.w2);
  155. if (cmp != 0)
  156. return cmp;
  157. cmp = NB.compareUInt32(w3, other.w3);
  158. if (cmp != 0)
  159. return cmp;
  160. cmp = NB.compareUInt32(w4, other.w4);
  161. if (cmp != 0)
  162. return cmp;
  163. return NB.compareUInt32(w5, other.w5);
  164. }
  165. public final int compareTo(final Object other) {
  166. return compareTo(((AnyObjectId) other));
  167. }
  168. /**
  169. * Compare this ObjectId to a network-byte-order ObjectId.
  170. *
  171. * @param bs
  172. * array containing the other ObjectId in network byte order.
  173. * @param p
  174. * position within {@code bs} to start the compare at. At least
  175. * 20 bytes, starting at this position are required.
  176. * @return a negative integer, zero, or a positive integer as this object is
  177. * less than, equal to, or greater than the specified object.
  178. */
  179. public final int compareTo(final byte[] bs, final int p) {
  180. int cmp;
  181. cmp = NB.compareUInt32(w1, NB.decodeInt32(bs, p));
  182. if (cmp != 0)
  183. return cmp;
  184. cmp = NB.compareUInt32(w2, NB.decodeInt32(bs, p + 4));
  185. if (cmp != 0)
  186. return cmp;
  187. cmp = NB.compareUInt32(w3, NB.decodeInt32(bs, p + 8));
  188. if (cmp != 0)
  189. return cmp;
  190. cmp = NB.compareUInt32(w4, NB.decodeInt32(bs, p + 12));
  191. if (cmp != 0)
  192. return cmp;
  193. return NB.compareUInt32(w5, NB.decodeInt32(bs, p + 16));
  194. }
  195. /**
  196. * Compare this ObjectId to a network-byte-order ObjectId.
  197. *
  198. * @param bs
  199. * array containing the other ObjectId in network byte order.
  200. * @param p
  201. * position within {@code bs} to start the compare at. At least 5
  202. * integers, starting at this position are required.
  203. * @return a negative integer, zero, or a positive integer as this object is
  204. * less than, equal to, or greater than the specified object.
  205. */
  206. public final int compareTo(final int[] bs, final int p) {
  207. int cmp;
  208. cmp = NB.compareUInt32(w1, bs[p]);
  209. if (cmp != 0)
  210. return cmp;
  211. cmp = NB.compareUInt32(w2, bs[p + 1]);
  212. if (cmp != 0)
  213. return cmp;
  214. cmp = NB.compareUInt32(w3, bs[p + 2]);
  215. if (cmp != 0)
  216. return cmp;
  217. cmp = NB.compareUInt32(w4, bs[p + 3]);
  218. if (cmp != 0)
  219. return cmp;
  220. return NB.compareUInt32(w5, bs[p + 4]);
  221. }
  222. /**
  223. * Tests if this ObjectId starts with the given abbreviation.
  224. *
  225. * @param abbr
  226. * the abbreviation.
  227. * @return true if this ObjectId begins with the abbreviation; else false.
  228. */
  229. public boolean startsWith(final AbbreviatedObjectId abbr) {
  230. return abbr.prefixCompare(this) == 0;
  231. }
  232. public final int hashCode() {
  233. return w2;
  234. }
  235. /**
  236. * Determine if this ObjectId has exactly the same value as another.
  237. *
  238. * @param other
  239. * the other id to compare to. May be null.
  240. * @return true only if both ObjectIds have identical bits.
  241. */
  242. public final boolean equals(final AnyObjectId other) {
  243. return other != null ? equals(this, other) : false;
  244. }
  245. public final boolean equals(final Object o) {
  246. if (o instanceof AnyObjectId)
  247. return equals((AnyObjectId) o);
  248. else
  249. return false;
  250. }
  251. /**
  252. * Copy this ObjectId to an output writer in raw binary.
  253. *
  254. * @param w
  255. * the buffer to copy to. Must be in big endian order.
  256. */
  257. public void copyRawTo(final ByteBuffer w) {
  258. w.putInt(w1);
  259. w.putInt(w2);
  260. w.putInt(w3);
  261. w.putInt(w4);
  262. w.putInt(w5);
  263. }
  264. /**
  265. * Copy this ObjectId to a byte array.
  266. *
  267. * @param b
  268. * the buffer to copy to.
  269. * @param o
  270. * the offset within b to write at.
  271. */
  272. public void copyRawTo(final byte[] b, final int o) {
  273. NB.encodeInt32(b, o, w1);
  274. NB.encodeInt32(b, o + 4, w2);
  275. NB.encodeInt32(b, o + 8, w3);
  276. NB.encodeInt32(b, o + 12, w4);
  277. NB.encodeInt32(b, o + 16, w5);
  278. }
  279. /**
  280. * Copy this ObjectId to an int array.
  281. *
  282. * @param b
  283. * the buffer to copy to.
  284. * @param o
  285. * the offset within b to write at.
  286. */
  287. public void copyRawTo(final int[] b, final int o) {
  288. b[o] = w1;
  289. b[o + 1] = w2;
  290. b[o + 2] = w3;
  291. b[o + 3] = w4;
  292. b[o + 4] = w5;
  293. }
  294. /**
  295. * Copy this ObjectId to an output writer in raw binary.
  296. *
  297. * @param w
  298. * the stream to write to.
  299. * @throws IOException
  300. * the stream writing failed.
  301. */
  302. public void copyRawTo(final OutputStream w) throws IOException {
  303. writeRawInt(w, w1);
  304. writeRawInt(w, w2);
  305. writeRawInt(w, w3);
  306. writeRawInt(w, w4);
  307. writeRawInt(w, w5);
  308. }
  309. private static void writeRawInt(final OutputStream w, int v)
  310. throws IOException {
  311. w.write(v >>> 24);
  312. w.write(v >>> 16);
  313. w.write(v >>> 8);
  314. w.write(v);
  315. }
  316. /**
  317. * Copy this ObjectId to an output writer in hex format.
  318. *
  319. * @param w
  320. * the stream to copy to.
  321. * @throws IOException
  322. * the stream writing failed.
  323. */
  324. public void copyTo(final OutputStream w) throws IOException {
  325. w.write(toHexByteArray());
  326. }
  327. /**
  328. * Copy this ObjectId to a byte array in hex format.
  329. *
  330. * @param b
  331. * the buffer to copy to.
  332. * @param o
  333. * the offset within b to write at.
  334. */
  335. public void copyTo(byte[] b, int o) {
  336. formatHexByte(b, o + 0, w1);
  337. formatHexByte(b, o + 8, w2);
  338. formatHexByte(b, o + 16, w3);
  339. formatHexByte(b, o + 24, w4);
  340. formatHexByte(b, o + 32, w5);
  341. }
  342. /**
  343. * Copy this ObjectId to a ByteBuffer in hex format.
  344. *
  345. * @param b
  346. * the buffer to copy to.
  347. */
  348. public void copyTo(ByteBuffer b) {
  349. b.put(toHexByteArray());
  350. }
  351. private byte[] toHexByteArray() {
  352. final byte[] dst = new byte[Constants.OBJECT_ID_STRING_LENGTH];
  353. formatHexByte(dst, 0, w1);
  354. formatHexByte(dst, 8, w2);
  355. formatHexByte(dst, 16, w3);
  356. formatHexByte(dst, 24, w4);
  357. formatHexByte(dst, 32, w5);
  358. return dst;
  359. }
  360. private static final byte[] hexbyte = { '0', '1', '2', '3', '4', '5', '6',
  361. '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  362. private static void formatHexByte(final byte[] dst, final int p, int w) {
  363. int o = p + 7;
  364. while (o >= p && w != 0) {
  365. dst[o--] = hexbyte[w & 0xf];
  366. w >>>= 4;
  367. }
  368. while (o >= p)
  369. dst[o--] = '0';
  370. }
  371. /**
  372. * Copy this ObjectId to an output writer in hex format.
  373. *
  374. * @param w
  375. * the stream to copy to.
  376. * @throws IOException
  377. * the stream writing failed.
  378. */
  379. public void copyTo(final Writer w) throws IOException {
  380. w.write(toHexCharArray());
  381. }
  382. /**
  383. * Copy this ObjectId to an output writer in hex format.
  384. *
  385. * @param tmp
  386. * temporary char array to buffer construct into before writing.
  387. * Must be at least large enough to hold 2 digits for each byte
  388. * of object id (40 characters or larger).
  389. * @param w
  390. * the stream to copy to.
  391. * @throws IOException
  392. * the stream writing failed.
  393. */
  394. public void copyTo(final char[] tmp, final Writer w) throws IOException {
  395. toHexCharArray(tmp);
  396. w.write(tmp, 0, Constants.OBJECT_ID_STRING_LENGTH);
  397. }
  398. /**
  399. * Copy this ObjectId to a StringBuilder in hex format.
  400. *
  401. * @param tmp
  402. * temporary char array to buffer construct into before writing.
  403. * Must be at least large enough to hold 2 digits for each byte
  404. * of object id (40 characters or larger).
  405. * @param w
  406. * the string to append onto.
  407. */
  408. public void copyTo(final char[] tmp, final StringBuilder w) {
  409. toHexCharArray(tmp);
  410. w.append(tmp, 0, Constants.OBJECT_ID_STRING_LENGTH);
  411. }
  412. private char[] toHexCharArray() {
  413. final char[] dst = new char[Constants.OBJECT_ID_STRING_LENGTH];
  414. toHexCharArray(dst);
  415. return dst;
  416. }
  417. private void toHexCharArray(final char[] dst) {
  418. formatHexChar(dst, 0, w1);
  419. formatHexChar(dst, 8, w2);
  420. formatHexChar(dst, 16, w3);
  421. formatHexChar(dst, 24, w4);
  422. formatHexChar(dst, 32, w5);
  423. }
  424. private static final char[] hexchar = { '0', '1', '2', '3', '4', '5', '6',
  425. '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  426. static void formatHexChar(final char[] dst, final int p, int w) {
  427. int o = p + 7;
  428. while (o >= p && w != 0) {
  429. dst[o--] = hexchar[w & 0xf];
  430. w >>>= 4;
  431. }
  432. while (o >= p)
  433. dst[o--] = '0';
  434. }
  435. @Override
  436. public String toString() {
  437. return "AnyObjectId[" + name() + "]";
  438. }
  439. /**
  440. * @return string form of the SHA-1, in lower case hexadecimal.
  441. */
  442. public final String name() {
  443. return new String(toHexCharArray());
  444. }
  445. /**
  446. * @return string form of the SHA-1, in lower case hexadecimal.
  447. */
  448. public final String getName() {
  449. return name();
  450. }
  451. /**
  452. * Return an abbreviation (prefix) of this object SHA-1.
  453. *
  454. * This implementation does not guaranteeing uniqueness. Callers should
  455. * instead use {@link ObjectReader#abbreviate(AnyObjectId, int)} to obtain a
  456. * unique abbreviation within the scope of a particular object database.
  457. *
  458. * @param len
  459. * length of the abbreviated string.
  460. * @return SHA-1 abbreviation.
  461. */
  462. public AbbreviatedObjectId abbreviate(final int len) {
  463. final int a = AbbreviatedObjectId.mask(len, 1, w1);
  464. final int b = AbbreviatedObjectId.mask(len, 2, w2);
  465. final int c = AbbreviatedObjectId.mask(len, 3, w3);
  466. final int d = AbbreviatedObjectId.mask(len, 4, w4);
  467. final int e = AbbreviatedObjectId.mask(len, 5, w5);
  468. return new AbbreviatedObjectId(len, a, b, c, d, e);
  469. }
  470. /**
  471. * Obtain an immutable copy of this current object name value.
  472. * <p>
  473. * Only returns <code>this</code> if this instance is an unsubclassed
  474. * instance of {@link ObjectId}; otherwise a new instance is returned
  475. * holding the same value.
  476. * <p>
  477. * This method is useful to shed any additional memory that may be tied to
  478. * the subclass, yet retain the unique identity of the object id for future
  479. * lookups within maps and repositories.
  480. *
  481. * @return an immutable copy, using the smallest memory footprint possible.
  482. */
  483. public final ObjectId copy() {
  484. if (getClass() == ObjectId.class)
  485. return (ObjectId) this;
  486. return new ObjectId(this);
  487. }
  488. /**
  489. * Obtain an immutable copy of this current object name value.
  490. * <p>
  491. * See {@link #copy()} if <code>this</code> is a possibly subclassed (but
  492. * immutable) identity and the application needs a lightweight identity
  493. * <i>only</i> reference.
  494. *
  495. * @return an immutable copy. May be <code>this</code> if this is already
  496. * an immutable instance.
  497. */
  498. public abstract ObjectId toObjectId();
  499. }