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.

ObjectIdRef.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import org.eclipse.jgit.annotations.NonNull;
  46. import org.eclipse.jgit.annotations.Nullable;
  47. /**
  48. * A {@link org.eclipse.jgit.lib.Ref} that points directly at an
  49. * {@link org.eclipse.jgit.lib.ObjectId}.
  50. */
  51. public abstract class ObjectIdRef implements Ref {
  52. /** Any reference whose peeled value is not yet known. */
  53. public static class Unpeeled extends ObjectIdRef {
  54. /**
  55. * Create a new ref pairing.
  56. *
  57. * @param st
  58. * method used to store this ref.
  59. * @param name
  60. * name of this ref.
  61. * @param id
  62. * current value of the ref. May be {@code null} to indicate
  63. * a ref that does not exist yet.
  64. */
  65. public Unpeeled(@NonNull Storage st, @NonNull String name,
  66. @Nullable ObjectId id) {
  67. super(st, name, id, UNDEFINED_UPDATE_INDEX);
  68. }
  69. /**
  70. * Create a new ref pairing with update index.
  71. *
  72. * @param st
  73. * method used to store this ref.
  74. * @param name
  75. * name of this ref.
  76. * @param id
  77. * current value of the ref. May be {@code null} to indicate
  78. * a ref that does not exist yet.
  79. * @param updateIndex
  80. * number increasing with each update to the reference.
  81. * @since 5.3
  82. */
  83. public Unpeeled(@NonNull Storage st, @NonNull String name,
  84. @Nullable ObjectId id, long updateIndex) {
  85. super(st, name, id, updateIndex);
  86. }
  87. @Override
  88. @Nullable
  89. public ObjectId getPeeledObjectId() {
  90. return null;
  91. }
  92. @Override
  93. public boolean isPeeled() {
  94. return false;
  95. }
  96. }
  97. /** An annotated tag whose peeled object has been cached. */
  98. public static class PeeledTag extends ObjectIdRef {
  99. private final ObjectId peeledObjectId;
  100. /**
  101. * Create a new ref pairing.
  102. *
  103. * @param st
  104. * method used to store this ref.
  105. * @param name
  106. * name of this ref.
  107. * @param id
  108. * current value of the ref.
  109. * @param p
  110. * the first non-tag object that tag {@code id} points to.
  111. */
  112. public PeeledTag(@NonNull Storage st, @NonNull String name,
  113. @Nullable ObjectId id, @NonNull ObjectId p) {
  114. super(st, name, id, UNDEFINED_UPDATE_INDEX);
  115. peeledObjectId = p;
  116. }
  117. /**
  118. * Create a new ref pairing with update index.
  119. *
  120. * @param st
  121. * method used to store this ref.
  122. * @param name
  123. * name of this ref.
  124. * @param id
  125. * current value of the ref. May be {@code null} to indicate
  126. * a ref that does not exist yet.
  127. * @param p
  128. * the first non-tag object that tag {@code id} points to.
  129. * @param updateIndex
  130. * number increasing with each update to the reference.
  131. * @since 5.3
  132. */
  133. public PeeledTag(@NonNull Storage st, @NonNull String name,
  134. @Nullable ObjectId id, @NonNull ObjectId p, long updateIndex) {
  135. super(st, name, id, updateIndex);
  136. peeledObjectId = p;
  137. }
  138. @Override
  139. @NonNull
  140. public ObjectId getPeeledObjectId() {
  141. return peeledObjectId;
  142. }
  143. @Override
  144. public boolean isPeeled() {
  145. return true;
  146. }
  147. }
  148. /** A reference to a non-tag object coming from a cached source. */
  149. public static class PeeledNonTag extends ObjectIdRef {
  150. /**
  151. * Create a new ref pairing.
  152. *
  153. * @param st
  154. * method used to store this ref.
  155. * @param name
  156. * name of this ref.
  157. * @param id
  158. * current value of the ref. May be {@code null} to indicate
  159. * a ref that does not exist yet.
  160. */
  161. public PeeledNonTag(@NonNull Storage st, @NonNull String name,
  162. @Nullable ObjectId id) {
  163. super(st, name, id, UNDEFINED_UPDATE_INDEX);
  164. }
  165. /**
  166. * Create a new ref pairing with update index.
  167. *
  168. * @param st
  169. * method used to store this ref.
  170. * @param name
  171. * name of this ref.
  172. * @param id
  173. * current value of the ref. May be {@code null} to indicate
  174. * a ref that does not exist yet.
  175. * @param updateIndex
  176. * number increasing with each update to the reference.
  177. * @since 5.3
  178. */
  179. public PeeledNonTag(@NonNull Storage st, @NonNull String name,
  180. @Nullable ObjectId id, long updateIndex) {
  181. super(st, name, id, updateIndex);
  182. }
  183. @Override
  184. @Nullable
  185. public ObjectId getPeeledObjectId() {
  186. return null;
  187. }
  188. @Override
  189. public boolean isPeeled() {
  190. return true;
  191. }
  192. }
  193. private final String name;
  194. private final Storage storage;
  195. private final ObjectId objectId;
  196. private final long updateIndex;
  197. /**
  198. * Create a new ref pairing.
  199. *
  200. * @param st
  201. * method used to store this ref.
  202. * @param name
  203. * name of this ref.
  204. * @param id
  205. * current value of the ref. May be {@code null} to indicate a
  206. * ref that does not exist yet.
  207. * @param updateIndex
  208. * number that increases with each ref update. Set to -1 if the
  209. * storage doesn't support versioning.
  210. * @since 5.3
  211. */
  212. protected ObjectIdRef(@NonNull Storage st, @NonNull String name,
  213. @Nullable ObjectId id, long updateIndex) {
  214. this.name = name;
  215. this.storage = st;
  216. this.objectId = id;
  217. this.updateIndex = updateIndex;
  218. }
  219. /** {@inheritDoc} */
  220. @Override
  221. @NonNull
  222. public String getName() {
  223. return name;
  224. }
  225. /** {@inheritDoc} */
  226. @Override
  227. public boolean isSymbolic() {
  228. return false;
  229. }
  230. /** {@inheritDoc} */
  231. @Override
  232. @NonNull
  233. public Ref getLeaf() {
  234. return this;
  235. }
  236. /** {@inheritDoc} */
  237. @Override
  238. @NonNull
  239. public Ref getTarget() {
  240. return this;
  241. }
  242. /** {@inheritDoc} */
  243. @Override
  244. @Nullable
  245. public ObjectId getObjectId() {
  246. return objectId;
  247. }
  248. /** {@inheritDoc} */
  249. @Override
  250. @NonNull
  251. public Storage getStorage() {
  252. return storage;
  253. }
  254. /**
  255. * {@inheritDoc}
  256. * @since 5.3
  257. */
  258. @Override
  259. public long getUpdateIndex() {
  260. if (updateIndex == UNDEFINED_UPDATE_INDEX) {
  261. throw new UnsupportedOperationException();
  262. }
  263. return updateIndex;
  264. }
  265. /** {@inheritDoc} */
  266. @NonNull
  267. @Override
  268. public String toString() {
  269. StringBuilder r = new StringBuilder();
  270. r.append("Ref["); //$NON-NLS-1$
  271. r.append(getName());
  272. r.append('=');
  273. r.append(ObjectId.toString(getObjectId()));
  274. r.append('(');
  275. r.append(updateIndex); // Print value, even if -1
  276. r.append(")]"); //$NON-NLS-1$
  277. return r.toString();
  278. }
  279. }