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