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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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);
  68. }
  69. @Override
  70. @Nullable
  71. public ObjectId getPeeledObjectId() {
  72. return null;
  73. }
  74. @Override
  75. public boolean isPeeled() {
  76. return false;
  77. }
  78. }
  79. /** An annotated tag whose peeled object has been cached. */
  80. public static class PeeledTag extends ObjectIdRef {
  81. private final ObjectId peeledObjectId;
  82. /**
  83. * Create a new ref pairing.
  84. *
  85. * @param st
  86. * method used to store this ref.
  87. * @param name
  88. * name of this ref.
  89. * @param id
  90. * current value of the ref.
  91. * @param p
  92. * the first non-tag object that tag {@code id} points to.
  93. */
  94. public PeeledTag(@NonNull Storage st, @NonNull String name,
  95. @Nullable ObjectId id, @NonNull ObjectId p) {
  96. super(st, name, id);
  97. peeledObjectId = p;
  98. }
  99. @Override
  100. @NonNull
  101. public ObjectId getPeeledObjectId() {
  102. return peeledObjectId;
  103. }
  104. @Override
  105. public boolean isPeeled() {
  106. return true;
  107. }
  108. }
  109. /** A reference to a non-tag object coming from a cached source. */
  110. public static class PeeledNonTag extends ObjectIdRef {
  111. /**
  112. * Create a new ref pairing.
  113. *
  114. * @param st
  115. * method used to store this ref.
  116. * @param name
  117. * name of this ref.
  118. * @param id
  119. * current value of the ref. May be {@code null} to indicate
  120. * a ref that does not exist yet.
  121. */
  122. public PeeledNonTag(@NonNull Storage st, @NonNull String name,
  123. @Nullable ObjectId id) {
  124. super(st, name, id);
  125. }
  126. @Override
  127. @Nullable
  128. public ObjectId getPeeledObjectId() {
  129. return null;
  130. }
  131. @Override
  132. public boolean isPeeled() {
  133. return true;
  134. }
  135. }
  136. private final String name;
  137. private final Storage storage;
  138. private final ObjectId objectId;
  139. /**
  140. * Create a new ref pairing.
  141. *
  142. * @param st
  143. * method used to store this ref.
  144. * @param name
  145. * name of this ref.
  146. * @param id
  147. * current value of the ref. May be {@code null} to indicate a
  148. * ref that does not exist yet.
  149. */
  150. protected ObjectIdRef(@NonNull Storage st, @NonNull String name,
  151. @Nullable ObjectId id) {
  152. this.name = name;
  153. this.storage = st;
  154. this.objectId = id;
  155. }
  156. /** {@inheritDoc} */
  157. @Override
  158. @NonNull
  159. public String getName() {
  160. return name;
  161. }
  162. /** {@inheritDoc} */
  163. @Override
  164. public boolean isSymbolic() {
  165. return false;
  166. }
  167. /** {@inheritDoc} */
  168. @Override
  169. @NonNull
  170. public Ref getLeaf() {
  171. return this;
  172. }
  173. /** {@inheritDoc} */
  174. @Override
  175. @NonNull
  176. public Ref getTarget() {
  177. return this;
  178. }
  179. /** {@inheritDoc} */
  180. @Override
  181. @Nullable
  182. public ObjectId getObjectId() {
  183. return objectId;
  184. }
  185. /** {@inheritDoc} */
  186. @Override
  187. @NonNull
  188. public Storage getStorage() {
  189. return storage;
  190. }
  191. /** {@inheritDoc} */
  192. @NonNull
  193. @Override
  194. public String toString() {
  195. StringBuilder r = new StringBuilder();
  196. r.append("Ref["); //$NON-NLS-1$
  197. r.append(getName());
  198. r.append('=');
  199. r.append(ObjectId.toString(getObjectId()));
  200. r.append(']');
  201. return r.toString();
  202. }
  203. }