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 16KB

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