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

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