View Javadoc
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  
45  package org.eclipse.jgit.lib;
46  
47  import org.eclipse.jgit.annotations.NonNull;
48  import org.eclipse.jgit.annotations.Nullable;
49  
50  /**
51   * A {@link org.eclipse.jgit.lib.Ref} that points directly at an
52   * {@link org.eclipse.jgit.lib.ObjectId}.
53   */
54  public abstract class ObjectIdRef implements Ref {
55  	/** Any reference whose peeled value is not yet known. */
56  	public static class Unpeeled extends ObjectIdRef {
57  		/**
58  		 * Create a new ref pairing.
59  		 *
60  		 * @param st
61  		 *            method used to store this ref.
62  		 * @param name
63  		 *            name of this ref.
64  		 * @param id
65  		 *            current value of the ref. May be {@code null} to indicate
66  		 *            a ref that does not exist yet.
67  		 */
68  		public Unpeeled(@NonNull Storage st, @NonNull String name,
69  				@Nullable ObjectId id) {
70  			super(st, name, id, -1);
71  		}
72  
73  		/**
74  		 * Create a new ref pairing with update index.
75  		 *
76  		 * @param st
77  		 *            method used to store this ref.
78  		 * @param name
79  		 *            name of this ref.
80  		 * @param id
81  		 *            current value of the ref. May be {@code null} to indicate
82  		 *            a ref that does not exist yet.
83  		 * @param updateIndex
84  		 *            number increasing with each update to the reference.
85  		 * @since 5.3
86  		 */
87  		public Unpeeled(@NonNull Storage st, @NonNull String name,
88  				@Nullable ObjectId id, long updateIndex) {
89  			super(st, name, id, updateIndex);
90  		}
91  
92  		@Override
93  		@Nullable
94  		public ObjectId getPeeledObjectId() {
95  			return null;
96  		}
97  
98  		@Override
99  		public boolean isPeeled() {
100 			return false;
101 		}
102 	}
103 
104 	/** An annotated tag whose peeled object has been cached. */
105 	public static class PeeledTag extends ObjectIdRef {
106 		private final ObjectId peeledObjectId;
107 
108 		/**
109 		 * Create a new ref pairing.
110 		 *
111 		 * @param st
112 		 *            method used to store this ref.
113 		 * @param name
114 		 *            name of this ref.
115 		 * @param id
116 		 *            current value of the ref.
117 		 * @param p
118 		 *            the first non-tag object that tag {@code id} points to.
119 		 */
120 		public PeeledTag(@NonNull Storage st, @NonNull String name,
121 				@Nullable ObjectId/../../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId id, @NonNull ObjectId p) {
122 			super(st, name, id, -1);
123 			peeledObjectId = p;
124 		}
125 
126 		/**
127 		 * Create a new ref pairing with update index.
128 		 *
129 		 * @param st
130 		 *            method used to store this ref.
131 		 * @param name
132 		 *            name of this ref.
133 		 * @param id
134 		 *            current value of the ref. May be {@code null} to indicate
135 		 *            a ref that does not exist yet.
136 		 * @param p
137 		 *            the first non-tag object that tag {@code id} points to.
138 		 * @param updateIndex
139 		 *            number increasing with each update to the reference.
140 		 * @since 5.3
141 		 */
142 		public PeeledTag(@NonNull Storage st, @NonNull String name,
143 				@Nullable ObjectId/../../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId id, @NonNull ObjectId p, long updateIndex) {
144 			super(st, name, id, updateIndex);
145 			peeledObjectId = p;
146 		}
147 
148 		@Override
149 		@NonNull
150 		public ObjectId getPeeledObjectId() {
151 			return peeledObjectId;
152 		}
153 
154 		@Override
155 		public boolean isPeeled() {
156 			return true;
157 		}
158 	}
159 
160 	/** A reference to a non-tag object coming from a cached source. */
161 	public static class PeeledNonTag extends ObjectIdRef {
162 		/**
163 		 * Create a new ref pairing.
164 		 *
165 		 * @param st
166 		 *            method used to store this ref.
167 		 * @param name
168 		 *            name of this ref.
169 		 * @param id
170 		 *            current value of the ref. May be {@code null} to indicate
171 		 *            a ref that does not exist yet.
172 		 */
173 		public PeeledNonTag(@NonNull Storage st, @NonNull String name,
174 				@Nullable ObjectId id) {
175 			super(st, name, id, -1);
176 		}
177 
178 		/**
179 		 * Create a new ref pairing with update index.
180 		 *
181 		 * @param st
182 		 *            method used to store this ref.
183 		 * @param name
184 		 *            name of this ref.
185 		 * @param id
186 		 *            current value of the ref. May be {@code null} to indicate
187 		 *            a ref that does not exist yet.
188 		 * @param updateIndex
189 		 *            number increasing with each update to the reference.
190 		 * @since 5.3
191 		 */
192 		public PeeledNonTag(@NonNull Storage st, @NonNull String name,
193 				@Nullable ObjectId id, long updateIndex) {
194 			super(st, name, id, updateIndex);
195 		}
196 
197 		@Override
198 		@Nullable
199 		public ObjectId getPeeledObjectId() {
200 			return null;
201 		}
202 
203 		@Override
204 		public boolean isPeeled() {
205 			return true;
206 		}
207 	}
208 
209 	private final String name;
210 
211 	private final Storage storage;
212 
213 	private final ObjectId objectId;
214 
215 	private final long updateIndex;
216 
217 	/**
218 	 * Create a new ref pairing.
219 	 *
220 	 * @param st
221 	 *            method used to store this ref.
222 	 * @param name
223 	 *            name of this ref.
224 	 * @param id
225 	 *            current value of the ref. May be {@code null} to indicate a
226 	 *            ref that does not exist yet.
227 	 * @param updateIndex
228 	 *            number that increases with each ref update. Set to -1 if the
229 	 *            storage doesn't support versioning.
230 	 * @since 5.3
231 	 */
232 	protected ObjectIdRef(@NonNull Storage st, @NonNull String name,
233 			@Nullable ObjectId id, long updateIndex) {
234 		this.name = name;
235 		this.storage = st;
236 		this.objectId = id;
237 		this.updateIndex = updateIndex;
238 	}
239 
240 	/** {@inheritDoc} */
241 	@Override
242 	@NonNull
243 	public String getName() {
244 		return name;
245 	}
246 
247 	/** {@inheritDoc} */
248 	@Override
249 	public boolean isSymbolic() {
250 		return false;
251 	}
252 
253 	/** {@inheritDoc} */
254 	@Override
255 	@NonNull
256 	public Ref getLeaf() {
257 		return this;
258 	}
259 
260 	/** {@inheritDoc} */
261 	@Override
262 	@NonNull
263 	public Ref getTarget() {
264 		return this;
265 	}
266 
267 	/** {@inheritDoc} */
268 	@Override
269 	@Nullable
270 	public ObjectId getObjectId() {
271 		return objectId;
272 	}
273 
274 	/** {@inheritDoc} */
275 	@Override
276 	@NonNull
277 	public Storage getStorage() {
278 		return storage;
279 	}
280 
281 	/**
282 	 * {@inheritDoc}
283 	 * @since 5.3
284 	 */
285 	@Override
286 	public long getUpdateIndex() {
287 		if (updateIndex == -1) {
288 			throw new UnsupportedOperationException();
289 		}
290 		return updateIndex;
291 	}
292 
293 	/** {@inheritDoc} */
294 	@NonNull
295 	@Override
296 	public String toString() {
297 		StringBuilder r = new StringBuilder();
298 		r.append("Ref["); //$NON-NLS-1$
299 		r.append(getName());
300 		r.append('=');
301 		r.append(ObjectId.toString(getObjectId()));
302 		r.append('(');
303 		r.append(updateIndex); // Print value, even if -1
304 		r.append(")]"); //$NON-NLS-1$
305 		return r.toString();
306 	}
307 }