View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.revwalk;
12  
13  import java.io.IOException;
14  
15  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
16  import org.eclipse.jgit.errors.MissingObjectException;
17  import org.eclipse.jgit.lib.AnyObjectId;
18  import org.eclipse.jgit.lib.Constants;
19  import org.eclipse.jgit.lib.ObjectId;
20  import org.eclipse.jgit.lib.ObjectIdOwnerMap;
21  
22  /**
23   * Base object type accessed during revision walking.
24   */
25  public abstract class RevObject extends ObjectIdOwnerMap.Entry {
26  	static final int PARSED = 1;
27  
28  	int flags;
29  
30  	RevObject(AnyObjectId name) {
31  		super(name);
32  	}
33  
34  	abstract void parseHeaders(RevWalk walk) throws MissingObjectException,
35  			IncorrectObjectTypeException, IOException;
36  
37  	abstract void parseBody(RevWalk walk) throws MissingObjectException,
38  			IncorrectObjectTypeException, IOException;
39  
40  	/**
41  	 * Get Git object type. See {@link org.eclipse.jgit.lib.Constants}.
42  	 *
43  	 * @return object type
44  	 */
45  	public abstract int getType();
46  
47  	/**
48  	 * Get the name of this object.
49  	 *
50  	 * @return unique hash of this object.
51  	 */
52  	public final ObjectId getId() {
53  		return this;
54  	}
55  
56  	/**
57  	 * Test to see if the flag has been set on this object.
58  	 *
59  	 * @param flag
60  	 *            the flag to test.
61  	 * @return true if the flag has been added to this object; false if not.
62  	 */
63  	public final boolean has(RevFlag flag) {
64  		return (flags & flag.mask) != 0;
65  	}
66  
67  	/**
68  	 * Test to see if any flag in the set has been set on this object.
69  	 *
70  	 * @param set
71  	 *            the flags to test.
72  	 * @return true if any flag in the set has been added to this object; false
73  	 *         if not.
74  	 */
75  	public final boolean hasAny(RevFlagSet set) {
76  		return (flags & set.mask) != 0;
77  	}
78  
79  	/**
80  	 * Test to see if all flags in the set have been set on this object.
81  	 *
82  	 * @param set
83  	 *            the flags to test.
84  	 * @return true if all flags of the set have been added to this object;
85  	 *         false if some or none have been added.
86  	 */
87  	public final boolean hasAll(RevFlagSet set) {
88  		return (flags & set.mask) == set.mask;
89  	}
90  
91  	/**
92  	 * Add a flag to this object.
93  	 * <p>
94  	 * If the flag is already set on this object then the method has no effect.
95  	 *
96  	 * @param flag
97  	 *            the flag to mark on this object, for later testing.
98  	 */
99  	public final void add(RevFlag flag) {
100 		flags |= flag.mask;
101 	}
102 
103 	/**
104 	 * Add a set of flags to this object.
105 	 *
106 	 * @param set
107 	 *            the set of flags to mark on this object, for later testing.
108 	 */
109 	public final void add(RevFlagSet set) {
110 		flags |= set.mask;
111 	}
112 
113 	/**
114 	 * Remove a flag from this object.
115 	 * <p>
116 	 * If the flag is not set on this object then the method has no effect.
117 	 *
118 	 * @param flag
119 	 *            the flag to remove from this object.
120 	 */
121 	public final void remove(RevFlag flag) {
122 		flags &= ~flag.mask;
123 	}
124 
125 	/**
126 	 * Remove a set of flags from this object.
127 	 *
128 	 * @param set
129 	 *            the flag to remove from this object.
130 	 */
131 	public final void remove(RevFlagSet set) {
132 		flags &= ~set.mask;
133 	}
134 
135 	/** {@inheritDoc} */
136 	@Override
137 	public String toString() {
138 		final StringBuilder s = new StringBuilder();
139 		s.append(Constants.typeString(getType()));
140 		s.append(' ');
141 		s.append(name());
142 		s.append(' ');
143 		appendCoreFlags(s);
144 		return s.toString();
145 	}
146 
147 	/**
148 	 * Append a debug description of core RevFlags to a buffer.
149 	 *
150 	 * @param s
151 	 *            buffer to append a debug description of core RevFlags onto.
152 	 */
153 	protected void appendCoreFlags(StringBuilder s) {
154 		s.append((flags & RevWalk.TOPO_DELAY) != 0 ? 'o' : '-');
155 		s.append((flags & RevWalk.TOPO_QUEUED) != 0 ? 'q' : '-');
156 		s.append((flags & RevWalk.TEMP_MARK) != 0 ? 't' : '-');
157 		s.append((flags & RevWalk.REWRITE) != 0 ? 'r' : '-');
158 		s.append((flags & RevWalk.UNINTERESTING) != 0 ? 'u' : '-');
159 		s.append((flags & RevWalk.SEEN) != 0 ? 's' : '-');
160 		s.append((flags & RevWalk.PARSED) != 0 ? 'p' : '-');
161 	}
162 }