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

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