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

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