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