View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.lib;
13  
14  import org.eclipse.jgit.annotations.NonNull;
15  import org.eclipse.jgit.annotations.Nullable;
16  
17  /**
18   * A {@link org.eclipse.jgit.lib.Ref} that points directly at an
19   * {@link org.eclipse.jgit.lib.ObjectId}.
20   */
21  public abstract class ObjectIdRef implements Ref {
22  	/** Any reference whose peeled value is not yet known. */
23  	public static class Unpeeled extends ObjectIdRef {
24  		/**
25  		 * Create a new ref pairing.
26  		 *
27  		 * @param st
28  		 *            method used to store this ref.
29  		 * @param name
30  		 *            name of this ref.
31  		 * @param id
32  		 *            current value of the ref. May be {@code null} to indicate
33  		 *            a ref that does not exist yet.
34  		 */
35  		public Unpeeled(@NonNull Storage st, @NonNull String name,
36  				@Nullable ObjectId id) {
37  			super(st, name, id, UNDEFINED_UPDATE_INDEX);
38  		}
39  
40  		/**
41  		 * Create a new ref pairing with update index.
42  		 *
43  		 * @param st
44  		 *            method used to store this ref.
45  		 * @param name
46  		 *            name of this ref.
47  		 * @param id
48  		 *            current value of the ref. May be {@code null} to indicate
49  		 *            a ref that does not exist yet.
50  		 * @param updateIndex
51  		 *            number increasing with each update to the reference.
52  		 * @since 5.3
53  		 */
54  		public Unpeeled(@NonNull Storage st, @NonNull String name,
55  				@Nullable ObjectId id, long updateIndex) {
56  			super(st, name, id, updateIndex);
57  		}
58  
59  		@Override
60  		@Nullable
61  		public ObjectId getPeeledObjectId() {
62  			return null;
63  		}
64  
65  		@Override
66  		public boolean isPeeled() {
67  			return false;
68  		}
69  	}
70  
71  	/** An annotated tag whose peeled object has been cached. */
72  	public static class PeeledTag extends ObjectIdRef {
73  		private final ObjectId peeledObjectId;
74  
75  		/**
76  		 * Create a new ref pairing.
77  		 *
78  		 * @param st
79  		 *            method used to store this ref.
80  		 * @param name
81  		 *            name of this ref.
82  		 * @param id
83  		 *            current value of the ref.
84  		 * @param p
85  		 *            the first non-tag object that tag {@code id} points to.
86  		 */
87  		public PeeledTag(@NonNull Storage st, @NonNull String name,
88  				@Nullable ObjectId id, @NonNull ObjectId p) {
89  			super(st, name, id, UNDEFINED_UPDATE_INDEX);
90  			peeledObjectId = p;
91  		}
92  
93  		/**
94  		 * Create a new ref pairing with update index.
95  		 *
96  		 * @param st
97  		 *            method used to store this ref.
98  		 * @param name
99  		 *            name of this ref.
100 		 * @param id
101 		 *            current value of the ref. May be {@code null} to indicate
102 		 *            a ref that does not exist yet.
103 		 * @param p
104 		 *            the first non-tag object that tag {@code id} points to.
105 		 * @param updateIndex
106 		 *            number increasing with each update to the reference.
107 		 * @since 5.3
108 		 */
109 		public PeeledTag(@NonNull Storage st, @NonNull String name,
110 				@Nullable ObjectId id, @NonNull ObjectId p, long updateIndex) {
111 			super(st, name, id, updateIndex);
112 			peeledObjectId = p;
113 		}
114 
115 		@Override
116 		@NonNull
117 		public ObjectId getPeeledObjectId() {
118 			return peeledObjectId;
119 		}
120 
121 		@Override
122 		public boolean isPeeled() {
123 			return true;
124 		}
125 	}
126 
127 	/** A reference to a non-tag object coming from a cached source. */
128 	public static class PeeledNonTag extends ObjectIdRef {
129 		/**
130 		 * Create a new ref pairing.
131 		 *
132 		 * @param st
133 		 *            method used to store this ref.
134 		 * @param name
135 		 *            name of this ref.
136 		 * @param id
137 		 *            current value of the ref. May be {@code null} to indicate
138 		 *            a ref that does not exist yet.
139 		 */
140 		public PeeledNonTag(@NonNull Storage st, @NonNull String name,
141 				@Nullable ObjectId id) {
142 			super(st, name, id, UNDEFINED_UPDATE_INDEX);
143 		}
144 
145 		/**
146 		 * Create a new ref pairing with update index.
147 		 *
148 		 * @param st
149 		 *            method used to store this ref.
150 		 * @param name
151 		 *            name of this ref.
152 		 * @param id
153 		 *            current value of the ref. May be {@code null} to indicate
154 		 *            a ref that does not exist yet.
155 		 * @param updateIndex
156 		 *            number increasing with each update to the reference.
157 		 * @since 5.3
158 		 */
159 		public PeeledNonTag(@NonNull Storage st, @NonNull String name,
160 				@Nullable ObjectId id, long updateIndex) {
161 			super(st, name, id, updateIndex);
162 		}
163 
164 		@Override
165 		@Nullable
166 		public ObjectId getPeeledObjectId() {
167 			return null;
168 		}
169 
170 		@Override
171 		public boolean isPeeled() {
172 			return true;
173 		}
174 	}
175 
176 	private final String name;
177 
178 	private final Storage storage;
179 
180 	private final ObjectId objectId;
181 
182 	private final long updateIndex;
183 
184 	/**
185 	 * Create a new ref pairing.
186 	 *
187 	 * @param st
188 	 *            method used to store this ref.
189 	 * @param name
190 	 *            name of this ref.
191 	 * @param id
192 	 *            current value of the ref. May be {@code null} to indicate a
193 	 *            ref that does not exist yet.
194 	 * @param updateIndex
195 	 *            number that increases with each ref update. Set to -1 if the
196 	 *            storage doesn't support versioning.
197 	 * @since 5.3
198 	 */
199 	protected ObjectIdRef(@NonNull Storage st, @NonNull String name,
200 			@Nullable ObjectId id, long updateIndex) {
201 		this.name = name;
202 		this.storage = st;
203 		this.objectId = id;
204 		this.updateIndex = updateIndex;
205 	}
206 
207 	/** {@inheritDoc} */
208 	@Override
209 	@NonNull
210 	public String getName() {
211 		return name;
212 	}
213 
214 	/** {@inheritDoc} */
215 	@Override
216 	public boolean isSymbolic() {
217 		return false;
218 	}
219 
220 	/** {@inheritDoc} */
221 	@Override
222 	@NonNull
223 	public Ref getLeaf() {
224 		return this;
225 	}
226 
227 	/** {@inheritDoc} */
228 	@Override
229 	@NonNull
230 	public Ref getTarget() {
231 		return this;
232 	}
233 
234 	/** {@inheritDoc} */
235 	@Override
236 	@Nullable
237 	public ObjectId getObjectId() {
238 		return objectId;
239 	}
240 
241 	/** {@inheritDoc} */
242 	@Override
243 	@NonNull
244 	public Storage getStorage() {
245 		return storage;
246 	}
247 
248 	/**
249 	 * {@inheritDoc}
250 	 * @since 5.3
251 	 */
252 	@Override
253 	public long getUpdateIndex() {
254 		if (updateIndex == UNDEFINED_UPDATE_INDEX) {
255 			throw new UnsupportedOperationException();
256 		}
257 		return updateIndex;
258 	}
259 
260 	/** {@inheritDoc} */
261 	@NonNull
262 	@Override
263 	public String toString() {
264 		StringBuilder r = new StringBuilder();
265 		r.append("Ref["); //$NON-NLS-1$
266 		r.append(getName());
267 		r.append('=');
268 		r.append(ObjectId.toString(getObjectId()));
269 		r.append('(');
270 		r.append(updateIndex); // Print value, even if -1
271 		r.append(")]"); //$NON-NLS-1$
272 		return r.toString();
273 	}
274 }