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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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<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 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 &lt; 0 if this id comes before other; 0 if this id is equal to
  145. * other; &gt; 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. /**
  166. * Compare this ObjectId to a network-byte-order ObjectId.
  167. *
  168. * @param bs
  169. * array containing the other ObjectId in network byte order.
  170. * @param p
  171. * position within {@code bs} to start the compare at. At least
  172. * 20 bytes, starting at this position are required.
  173. * @return a negative integer, zero, or a positive integer as this object is
  174. * less than, equal to, or greater than the specified object.
  175. */
  176. public final int compareTo(final byte[] bs, final int p) {
  177. int cmp;
  178. cmp = NB.compareUInt32(w1, NB.decodeInt32(bs, p));
  179. if (cmp != 0)
  180. return cmp;
  181. cmp = NB.compareUInt32(w2, NB.decodeInt32(bs, p + 4));
  182. if (cmp != 0)
  183. return cmp;
  184. cmp = NB.compareUInt32(w3, NB.decodeInt32(bs, p + 8));
  185. if (cmp != 0)
  186. return cmp;
  187. cmp = NB.compareUInt32(w4, NB.decodeInt32(bs, p + 12));
  188. if (cmp != 0)
  189. return cmp;
  190. return NB.compareUInt32(w5, NB.decodeInt32(bs, p + 16));
  191. }
  192. /**
  193. * Compare this ObjectId to a network-byte-order ObjectId.
  194. *
  195. * @param bs
  196. * array containing the other ObjectId in network byte order.
  197. * @param p
  198. * position within {@code bs} to start the compare at. At least 5
  199. * integers, starting at this position are required.
  200. * @return a negative integer, zero, or a positive integer as this object is
  201. * less than, equal to, or greater than the specified object.
  202. */
  203. public final int compareTo(final int[] bs, final int p) {
  204. int cmp;
  205. cmp = NB.compareUInt32(w1, bs[p]);
  206. if (cmp != 0)
  207. return cmp;
  208. cmp = NB.compareUInt32(w2, bs[p + 1]);
  209. if (cmp != 0)
  210. return cmp;
  211. cmp = NB.compareUInt32(w3, bs[p + 2]);
  212. if (cmp != 0)
  213. return cmp;
  214. cmp = NB.compareUInt32(w4, bs[p + 3]);
  215. if (cmp != 0)
  216. return cmp;
  217. return NB.compareUInt32(w5, bs[p + 4]);
  218. }
  219. /**
  220. * Tests if this ObjectId starts with the given abbreviation.
  221. *
  222. * @param abbr
  223. * the abbreviation.
  224. * @return true if this ObjectId begins with the abbreviation; else false.
  225. */
  226. public boolean startsWith(final AbbreviatedObjectId abbr) {
  227. return abbr.prefixCompare(this) == 0;
  228. }
  229. public final int hashCode() {
  230. return w2;
  231. }
  232. /**
  233. * Determine if this ObjectId has exactly the same value as another.
  234. *
  235. * @param other
  236. * the other id to compare to. May be null.
  237. * @return true only if both ObjectIds have identical bits.
  238. */
  239. public final boolean equals(final AnyObjectId other) {
  240. return other != null ? equals(this, other) : false;
  241. }
  242. public final boolean equals(final Object o) {
  243. if (o instanceof AnyObjectId)
  244. return equals((AnyObjectId) o);
  245. else
  246. return false;
  247. }
  248. /**
  249. * Copy this ObjectId to an output writer in raw binary.
  250. *
  251. * @param w
  252. * the buffer to copy to. Must be in big endian order.
  253. */
  254. public void copyRawTo(final ByteBuffer w) {
  255. w.putInt(w1);
  256. w.putInt(w2);
  257. w.putInt(w3);
  258. w.putInt(w4);
  259. w.putInt(w5);
  260. }
  261. /**
  262. * Copy this ObjectId to a byte array.
  263. *
  264. * @param b
  265. * the buffer to copy to.
  266. * @param o
  267. * the offset within b to write at.
  268. */
  269. public void copyRawTo(final byte[] b, final int o) {
  270. NB.encodeInt32(b, o, w1);
  271. NB.encodeInt32(b, o + 4, w2);
  272. NB.encodeInt32(b, o + 8, w3);
  273. NB.encodeInt32(b, o + 12, w4);
  274. NB.encodeInt32(b, o + 16, w5);
  275. }
  276. /**
  277. * Copy this ObjectId to an int array.
  278. *
  279. * @param b
  280. * the buffer to copy to.
  281. * @param o
  282. * the offset within b to write at.
  283. */
  284. public void copyRawTo(final int[] b, final int o) {
  285. b[o] = w1;
  286. b[o + 1] = w2;
  287. b[o + 2] = w3;
  288. b[o + 3] = w4;
  289. b[o + 4] = w5;
  290. }
  291. /**
  292. * Copy this ObjectId to an output writer in raw binary.
  293. *
  294. * @param w
  295. * the stream to write to.
  296. * @throws IOException
  297. * the stream writing failed.
  298. */
  299. public void copyRawTo(final OutputStream w) throws IOException {
  300. writeRawInt(w, w1);
  301. writeRawInt(w, w2);
  302. writeRawInt(w, w3);
  303. writeRawInt(w, w4);
  304. writeRawInt(w, w5);
  305. }
  306. private static void writeRawInt(final OutputStream w, int v)
  307. throws IOException {
  308. w.write(v >>> 24);
  309. w.write(v >>> 16);
  310. w.write(v >>> 8);
  311. w.write(v);
  312. }
  313. /**
  314. * Copy this ObjectId to an output writer in hex format.
  315. *
  316. * @param w
  317. * the stream to copy to.
  318. * @throws IOException
  319. * the stream writing failed.
  320. */
  321. public void copyTo(final OutputStream w) throws IOException {
  322. w.write(toHexByteArray());
  323. }
  324. /**
  325. * Copy this ObjectId to a byte array in hex format.
  326. *
  327. * @param b
  328. * the buffer to copy to.
  329. * @param o
  330. * the offset within b to write at.
  331. */
  332. public void copyTo(byte[] b, int o) {
  333. formatHexByte(b, o + 0, w1);
  334. formatHexByte(b, o + 8, w2);
  335. formatHexByte(b, o + 16, w3);
  336. formatHexByte(b, o + 24, w4);
  337. formatHexByte(b, o + 32, w5);
  338. }
  339. /**
  340. * Copy this ObjectId to a ByteBuffer in hex format.
  341. *
  342. * @param b
  343. * the buffer to copy to.
  344. */
  345. public void copyTo(ByteBuffer b) {
  346. b.put(toHexByteArray());
  347. }
  348. private byte[] toHexByteArray() {
  349. final byte[] dst = new byte[Constants.OBJECT_ID_STRING_LENGTH];
  350. formatHexByte(dst, 0, w1);
  351. formatHexByte(dst, 8, w2);
  352. formatHexByte(dst, 16, w3);
  353. formatHexByte(dst, 24, w4);
  354. formatHexByte(dst, 32, w5);
  355. return dst;
  356. }
  357. private static final byte[] hexbyte = { '0', '1', '2', '3', '4', '5', '6',
  358. '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  359. private static void formatHexByte(final byte[] dst, final int p, int w) {
  360. int o = p + 7;
  361. while (o >= p && w != 0) {
  362. dst[o--] = hexbyte[w & 0xf];
  363. w >>>= 4;
  364. }
  365. while (o >= p)
  366. dst[o--] = '0';
  367. }
  368. /**
  369. * Copy this ObjectId to an output writer in hex format.
  370. *
  371. * @param w
  372. * the stream to copy to.
  373. * @throws IOException
  374. * the stream writing failed.
  375. */
  376. public void copyTo(final Writer w) throws IOException {
  377. w.write(toHexCharArray());
  378. }
  379. /**
  380. * Copy this ObjectId to an output writer in hex format.
  381. *
  382. * @param tmp
  383. * temporary char array to buffer construct into before writing.
  384. * Must be at least large enough to hold 2 digits for each byte
  385. * of object id (40 characters or larger).
  386. * @param w
  387. * the stream to copy to.
  388. * @throws IOException
  389. * the stream writing failed.
  390. */
  391. public void copyTo(final char[] tmp, final Writer w) throws IOException {
  392. toHexCharArray(tmp);
  393. w.write(tmp, 0, Constants.OBJECT_ID_STRING_LENGTH);
  394. }
  395. /**
  396. * Copy this ObjectId to a StringBuilder in hex format.
  397. *
  398. * @param tmp
  399. * temporary char array to buffer construct into before writing.
  400. * Must be at least large enough to hold 2 digits for each byte
  401. * of object id (40 characters or larger).
  402. * @param w
  403. * the string to append onto.
  404. */
  405. public void copyTo(final char[] tmp, final StringBuilder w) {
  406. toHexCharArray(tmp);
  407. w.append(tmp, 0, Constants.OBJECT_ID_STRING_LENGTH);
  408. }
  409. private char[] toHexCharArray() {
  410. final char[] dst = new char[Constants.OBJECT_ID_STRING_LENGTH];
  411. toHexCharArray(dst);
  412. return dst;
  413. }
  414. private void toHexCharArray(final char[] dst) {
  415. formatHexChar(dst, 0, w1);
  416. formatHexChar(dst, 8, w2);
  417. formatHexChar(dst, 16, w3);
  418. formatHexChar(dst, 24, w4);
  419. formatHexChar(dst, 32, w5);
  420. }
  421. private static final char[] hexchar = { '0', '1', '2', '3', '4', '5', '6',
  422. '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  423. static void formatHexChar(final char[] dst, final int p, int w) {
  424. int o = p + 7;
  425. while (o >= p && w != 0) {
  426. dst[o--] = hexchar[w & 0xf];
  427. w >>>= 4;
  428. }
  429. while (o >= p)
  430. dst[o--] = '0';
  431. }
  432. @SuppressWarnings("nls")
  433. @Override
  434. public String toString() {
  435. return "AnyObjectId[" + name() + "]";
  436. }
  437. /**
  438. * @return string form of the SHA-1, in lower case hexadecimal.
  439. */
  440. public final String name() {
  441. return new String(toHexCharArray());
  442. }
  443. /**
  444. * @return string form of the SHA-1, in lower case hexadecimal.
  445. */
  446. public final String getName() {
  447. return name();
  448. }
  449. /**
  450. * Return an abbreviation (prefix) of this object SHA-1.
  451. * <p>
  452. * This implementation does not guarantee uniqueness. Callers should
  453. * instead use {@link ObjectReader#abbreviate(AnyObjectId, int)} to obtain a
  454. * unique abbreviation within the scope of a particular object database.
  455. *
  456. * @param len
  457. * length of the abbreviated string.
  458. * @return SHA-1 abbreviation.
  459. */
  460. public AbbreviatedObjectId abbreviate(final int len) {
  461. final int a = AbbreviatedObjectId.mask(len, 1, w1);
  462. final int b = AbbreviatedObjectId.mask(len, 2, w2);
  463. final int c = AbbreviatedObjectId.mask(len, 3, w3);
  464. final int d = AbbreviatedObjectId.mask(len, 4, w4);
  465. final int e = AbbreviatedObjectId.mask(len, 5, w5);
  466. return new AbbreviatedObjectId(len, a, b, c, d, e);
  467. }
  468. /**
  469. * Obtain an immutable copy of this current object name value.
  470. * <p>
  471. * Only returns <code>this</code> if this instance is an unsubclassed
  472. * instance of {@link ObjectId}; otherwise a new instance is returned
  473. * holding the same value.
  474. * <p>
  475. * This method is useful to shed any additional memory that may be tied to
  476. * the subclass, yet retain the unique identity of the object id for future
  477. * lookups within maps and repositories.
  478. *
  479. * @return an immutable copy, using the smallest memory footprint possible.
  480. */
  481. public final ObjectId copy() {
  482. if (getClass() == ObjectId.class)
  483. return (ObjectId) this;
  484. return new ObjectId(this);
  485. }
  486. /**
  487. * Obtain an immutable copy of this current object name value.
  488. * <p>
  489. * See {@link #copy()} if <code>this</code> is a possibly subclassed (but
  490. * immutable) identity and the application needs a lightweight identity
  491. * <i>only</i> reference.
  492. *
  493. * @return an immutable copy. May be <code>this</code> if this is already
  494. * an immutable instance.
  495. */
  496. public abstract ObjectId toObjectId();
  497. }