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