aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdRef.java
blob: c3c58372b1a14e3ed2f462f887da1d8c6b7c0ab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright (C) 2010, Google Inc.
 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.lib;

import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.annotations.Nullable;

/**
 * A {@link org.eclipse.jgit.lib.Ref} that points directly at an
 * {@link org.eclipse.jgit.lib.ObjectId}.
 */
public abstract class ObjectIdRef implements Ref {
	/** Any reference whose peeled value is not yet known. */
	public static class Unpeeled extends ObjectIdRef {
		/**
		 * Create a new ref pairing.
		 *
		 * @param st
		 *            method used to store this ref.
		 * @param name
		 *            name of this ref.
		 * @param id
		 *            current value of the ref. May be {@code null} to indicate
		 *            a ref that does not exist yet.
		 */
		public Unpeeled(@NonNull Storage st, @NonNull String name,
				@Nullable ObjectId id) {
			super(st, name, id, UNDEFINED_UPDATE_INDEX);
		}

		/**
		 * Create a new ref pairing with update index.
		 *
		 * @param st
		 *            method used to store this ref.
		 * @param name
		 *            name of this ref.
		 * @param id
		 *            current value of the ref. May be {@code null} to indicate
		 *            a ref that does not exist yet.
		 * @param updateIndex
		 *            number increasing with each update to the reference.
		 * @since 5.3
		 */
		public Unpeeled(@NonNull Storage st, @NonNull String name,
				@Nullable ObjectId id, long updateIndex) {
			super(st, name, id, updateIndex);
		}

		@Override
		@Nullable
		public ObjectId getPeeledObjectId() {
			return null;
		}

		@Override
		public boolean isPeeled() {
			return false;
		}
	}

	/** An annotated tag whose peeled object has been cached. */
	public static class PeeledTag extends ObjectIdRef {
		private final ObjectId peeledObjectId;

		/**
		 * Create a new ref pairing.
		 *
		 * @param st
		 *            method used to store this ref.
		 * @param name
		 *            name of this ref.
		 * @param id
		 *            current value of the ref.
		 * @param p
		 *            the first non-tag object that tag {@code id} points to.
		 */
		public PeeledTag(@NonNull Storage st, @NonNull String name,
				@Nullable ObjectId id, @NonNull ObjectId p) {
			super(st, name, id, UNDEFINED_UPDATE_INDEX);
			peeledObjectId = p;
		}

		/**
		 * Create a new ref pairing with update index.
		 *
		 * @param st
		 *            method used to store this ref.
		 * @param name
		 *            name of this ref.
		 * @param id
		 *            current value of the ref. May be {@code null} to indicate
		 *            a ref that does not exist yet.
		 * @param p
		 *            the first non-tag object that tag {@code id} points to.
		 * @param updateIndex
		 *            number increasing with each update to the reference.
		 * @since 5.3
		 */
		public PeeledTag(@NonNull Storage st, @NonNull String name,
				@Nullable ObjectId id, @NonNull ObjectId p, long updateIndex) {
			super(st, name, id, updateIndex);
			peeledObjectId = p;
		}

		@Override
		@NonNull
		public ObjectId getPeeledObjectId() {
			return peeledObjectId;
		}

		@Override
		public boolean isPeeled() {
			return true;
		}
	}

	/** A reference to a non-tag object coming from a cached source. */
	public static class PeeledNonTag extends ObjectIdRef {
		/**
		 * Create a new ref pairing.
		 *
		 * @param st
		 *            method used to store this ref.
		 * @param name
		 *            name of this ref.
		 * @param id
		 *            current value of the ref. May be {@code null} to indicate
		 *            a ref that does not exist yet.
		 */
		public PeeledNonTag(@NonNull Storage st, @NonNull String name,
				@Nullable ObjectId id) {
			super(st, name, id, UNDEFINED_UPDATE_INDEX);
		}

		/**
		 * Create a new ref pairing with update index.
		 *
		 * @param st
		 *            method used to store this ref.
		 * @param name
		 *            name of this ref.
		 * @param id
		 *            current value of the ref. May be {@code null} to indicate
		 *            a ref that does not exist yet.
		 * @param updateIndex
		 *            number increasing with each update to the reference.
		 * @since 5.3
		 */
		public PeeledNonTag(@NonNull Storage st, @NonNull String name,
				@Nullable ObjectId id, long updateIndex) {
			super(st, name, id, updateIndex);
		}

		@Override
		@Nullable
		public ObjectId getPeeledObjectId() {
			return null;
		}

		@Override
		public boolean isPeeled() {
			return true;
		}
	}

	private final String name;

	private final Storage storage;

	private final ObjectId objectId;

	private final long updateIndex;

	/**
	 * Create a new ref pairing.
	 *
	 * @param st
	 *            method used to store this ref.
	 * @param name
	 *            name of this ref.
	 * @param id
	 *            current value of the ref. May be {@code null} to indicate a
	 *            ref that does not exist yet.
	 * @param updateIndex
	 *            number that increases with each ref update. Set to -1 if the
	 *            storage doesn't support versioning.
	 * @since 5.3
	 */
	protected ObjectIdRef(@NonNull Storage st, @NonNull String name,
			@Nullable ObjectId id, long updateIndex) {
		this.name = name;
		this.storage = st;
		this.objectId = id;
		this.updateIndex = updateIndex;
	}

	@Override
	@NonNull
	public String getName() {
		return name;
	}

	@Override
	public boolean isSymbolic() {
		return false;
	}

	@Override
	@NonNull
	public Ref getLeaf() {
		return this;
	}

	@Override
	@NonNull
	public Ref getTarget() {
		return this;
	}

	@Override
	@Nullable
	public ObjectId getObjectId() {
		return objectId;
	}

	@Override
	@NonNull
	public Storage getStorage() {
		return storage;
	}

	/**
	 * {@inheritDoc}
	 * @since 5.3
	 */
	@Override
	public long getUpdateIndex() {
		if (updateIndex == UNDEFINED_UPDATE_INDEX) {
			throw new UnsupportedOperationException();
		}
		return updateIndex;
	}

	@NonNull
	@Override
	public String toString() {
		StringBuilder r = new StringBuilder();
		r.append("Ref["); //$NON-NLS-1$
		r.append(getName());
		r.append('=');
		r.append(ObjectId.toString(getObjectId()));
		r.append('(');
		r.append(updateIndex); // Print value, even if -1
		r.append(")]"); //$NON-NLS-1$
		return r.toString();
	}
}