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