View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.revwalk;
45  
46  import java.io.IOException;
47  
48  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
49  import org.eclipse.jgit.errors.MissingObjectException;
50  import org.eclipse.jgit.lib.AnyObjectId;
51  import org.eclipse.jgit.lib.Constants;
52  import org.eclipse.jgit.lib.ObjectId;
53  import org.eclipse.jgit.lib.ObjectIdOwnerMap;
54  
55  /**
56   * Base object type accessed during revision walking.
57   */
58  public abstract class RevObject extends ObjectIdOwnerMap.Entry {
59  	static final int PARSED = 1;
60  
61  	int flags;
62  
63  	RevObject(AnyObjectId name) {
64  		super(name);
65  	}
66  
67  	abstract void parseHeaders(RevWalk walk) throws MissingObjectException,
68  			IncorrectObjectTypeException, IOException;
69  
70  	abstract void parseBody(RevWalk walk) throws MissingObjectException,
71  			IncorrectObjectTypeException, IOException;
72  
73  	/**
74  	 * Get Git object type. See {@link org.eclipse.jgit.lib.Constants}.
75  	 *
76  	 * @return object type
77  	 */
78  	public abstract int getType();
79  
80  	/**
81  	 * Get the name of this object.
82  	 *
83  	 * @return unique hash of this object.
84  	 */
85  	public final ObjectId getId() {
86  		return this;
87  	}
88  
89  	/**
90  	 * Test to see if the flag has been set on this object.
91  	 *
92  	 * @param flag
93  	 *            the flag to test.
94  	 * @return true if the flag has been added to this object; false if not.
95  	 */
96  	public final boolean has(RevFlag flag) {
97  		return (flags & flag.mask) != 0;
98  	}
99  
100 	/**
101 	 * Test to see if any flag in the set has been set on this object.
102 	 *
103 	 * @param set
104 	 *            the flags to test.
105 	 * @return true if any flag in the set has been added to this object; false
106 	 *         if not.
107 	 */
108 	public final boolean hasAny(RevFlagSet set) {
109 		return (flags & set.mask) != 0;
110 	}
111 
112 	/**
113 	 * Test to see if all flags in the set have been set on this object.
114 	 *
115 	 * @param set
116 	 *            the flags to test.
117 	 * @return true if all flags of the set have been added to this object;
118 	 *         false if some or none have been added.
119 	 */
120 	public final boolean hasAll(RevFlagSet set) {
121 		return (flags & set.mask) == set.mask;
122 	}
123 
124 	/**
125 	 * Add a flag to this object.
126 	 * <p>
127 	 * If the flag is already set on this object then the method has no effect.
128 	 *
129 	 * @param flag
130 	 *            the flag to mark on this object, for later testing.
131 	 */
132 	public final void add(RevFlag flag) {
133 		flags |= flag.mask;
134 	}
135 
136 	/**
137 	 * Add a set of flags to this object.
138 	 *
139 	 * @param set
140 	 *            the set of flags to mark on this object, for later testing.
141 	 */
142 	public final void add(RevFlagSet set) {
143 		flags |= set.mask;
144 	}
145 
146 	/**
147 	 * Remove a flag from this object.
148 	 * <p>
149 	 * If the flag is not set on this object then the method has no effect.
150 	 *
151 	 * @param flag
152 	 *            the flag to remove from this object.
153 	 */
154 	public final void remove(RevFlag flag) {
155 		flags &= ~flag.mask;
156 	}
157 
158 	/**
159 	 * Remove a set of flags from this object.
160 	 *
161 	 * @param set
162 	 *            the flag to remove from this object.
163 	 */
164 	public final void remove(RevFlagSet set) {
165 		flags &= ~set.mask;
166 	}
167 
168 	/** {@inheritDoc} */
169 	@Override
170 	public String toString() {
171 		final StringBuilder s = new StringBuilder();
172 		s.append(Constants.typeString(getType()));
173 		s.append(' ');
174 		s.append(name());
175 		s.append(' ');
176 		appendCoreFlags(s);
177 		return s.toString();
178 	}
179 
180 	/**
181 	 * Append a debug description of core RevFlags to a buffer.
182 	 *
183 	 * @param s
184 	 *            buffer to append a debug description of core RevFlags onto.
185 	 */
186 	protected void appendCoreFlags(StringBuilder s) {
187 		s.append((flags & RevWalk.TOPO_DELAY) != 0 ? 'o' : '-');
188 		s.append((flags & RevWalk.TEMP_MARK) != 0 ? 't' : '-');
189 		s.append((flags & RevWalk.REWRITE) != 0 ? 'r' : '-');
190 		s.append((flags & RevWalk.UNINTERESTING) != 0 ? 'u' : '-');
191 		s.append((flags & RevWalk.SEEN) != 0 ? 's' : '-');
192 		s.append((flags & RevWalk.PARSED) != 0 ? 'p' : '-');
193 	}
194 }