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  /** Base object type accessed during revision walking. */
56  public abstract class RevObject extends ObjectIdOwnerMap.Entry {
57  	static final int PARSED = 1;
58  
59  	int flags;
60  
61  	RevObject(final AnyObjectId name) {
62  		super(name);
63  	}
64  
65  	abstract void parseHeaders(RevWalk walk) throws MissingObjectException,
66  			IncorrectObjectTypeException, IOException;
67  
68  	abstract void parseBody(RevWalk walk) throws MissingObjectException,
69  			IncorrectObjectTypeException, IOException;
70  
71  	/**
72  	 * Get Git object type. See {@link Constants}.
73  	 *
74  	 * @return object type
75  	 */
76  	public abstract int getType();
77  
78  	/**
79  	 * Get the name of this object.
80  	 *
81  	 * @return unique hash of this object.
82  	 */
83  	public final ObjectId getId() {
84  		return this;
85  	}
86  
87  	/**
88  	 * Test to see if the flag has been set on this object.
89  	 *
90  	 * @param flag
91  	 *            the flag to test.
92  	 * @return true if the flag has been added to this object; false if not.
93  	 */
94  	public final boolean has(final RevFlag flag) {
95  		return (flags & flag.mask) != 0;
96  	}
97  
98  	/**
99  	 * Test to see if any flag in the set has been set on this object.
100 	 *
101 	 * @param set
102 	 *            the flags to test.
103 	 * @return true if any flag in the set has been added to this object; false
104 	 *         if not.
105 	 */
106 	public final boolean hasAny(final RevFlagSet set) {
107 		return (flags & set.mask) != 0;
108 	}
109 
110 	/**
111 	 * Test to see if all flags in the set have been set on this object.
112 	 *
113 	 * @param set
114 	 *            the flags to test.
115 	 * @return true if all flags of the set have been added to this object;
116 	 *         false if some or none have been added.
117 	 */
118 	public final boolean hasAll(final RevFlagSet set) {
119 		return (flags & set.mask) == set.mask;
120 	}
121 
122 	/**
123 	 * Add a flag to this object.
124 	 * <p>
125 	 * If the flag is already set on this object then the method has no effect.
126 	 *
127 	 * @param flag
128 	 *            the flag to mark on this object, for later testing.
129 	 */
130 	public final void add(final RevFlag flag) {
131 		flags |= flag.mask;
132 	}
133 
134 	/**
135 	 * Add a set of flags to this object.
136 	 *
137 	 * @param set
138 	 *            the set of flags to mark on this object, for later testing.
139 	 */
140 	public final void add(final RevFlagSet set) {
141 		flags |= set.mask;
142 	}
143 
144 	/**
145 	 * Remove a flag from this object.
146 	 * <p>
147 	 * If the flag is not set on this object then the method has no effect.
148 	 *
149 	 * @param flag
150 	 *            the flag to remove from this object.
151 	 */
152 	public final void remove(final RevFlag flag) {
153 		flags &= ~flag.mask;
154 	}
155 
156 	/**
157 	 * Remove a set of flags from this object.
158 	 *
159 	 * @param set
160 	 *            the flag to remove from this object.
161 	 */
162 	public final void remove(final RevFlagSet set) {
163 		flags &= ~set.mask;
164 	}
165 
166 	@Override
167 	public String toString() {
168 		final StringBuilder s = new StringBuilder();
169 		s.append(Constants.typeString(getType()));
170 		s.append(' ');
171 		s.append(name());
172 		s.append(' ');
173 		appendCoreFlags(s);
174 		return s.toString();
175 	}
176 
177 	/**
178 	 * @param s
179 	 *            buffer to append a debug description of core RevFlags onto.
180 	 */
181 	protected void appendCoreFlags(final StringBuilder s) {
182 		s.append((flags & RevWalk.TOPO_DELAY) != 0 ? 'o' : '-');
183 		s.append((flags & RevWalk.TEMP_MARK) != 0 ? 't' : '-');
184 		s.append((flags & RevWalk.REWRITE) != 0 ? 'r' : '-');
185 		s.append((flags & RevWalk.UNINTERESTING) != 0 ? 'u' : '-');
186 		s.append((flags & RevWalk.SEEN) != 0 ? 's' : '-');
187 		s.append((flags & RevWalk.PARSED) != 0 ? 'p' : '-');
188 	}
189 }