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  abstract class AbstractRevQueue extends Generator {
14  	static final AbstractRevQueue EMPTY_QUEUE = new AlwaysEmptyQueue();
15  
16  	/** Current output flags set for this generator instance. */
17  	int outputType;
18  
19  	AbstractRevQueue(boolean firstParent) {
20  		super(firstParent);
21  	}
22  
23  	/**
24  	 * Add a commit to the queue.
25  	 * <p>
26  	 * This method always adds the commit, even if it is already in the queue or
27  	 * previously was in the queue but has already been removed. To control
28  	 * queue admission use {@link #add(RevCommit, RevFlag)}.
29  	 *
30  	 * @param c
31  	 *            commit to add.
32  	 */
33  	public abstract void add(RevCommit c);
34  
35  	/**
36  	 * Add a commit if it does not have a flag set yet, then set the flag.
37  	 * <p>
38  	 * This method permits the application to test if the commit has the given
39  	 * flag; if it does not already have the flag than the commit is added to
40  	 * the queue and the flag is set. This later will prevent the commit from
41  	 * being added twice.
42  	 *
43  	 * @param c
44  	 *            commit to add.
45  	 * @param queueControl
46  	 *            flag that controls admission to the queue.
47  	 */
48  	public final void add(RevCommit c, RevFlag queueControl) {
49  		if (!c.has(queueControl)) {
50  			c.add(queueControl);
51  			add(c);
52  		}
53  	}
54  
55  	/**
56  	 * Add a commit's parents if one does not have a flag set yet.
57  	 * <p>
58  	 * This method permits the application to test if the commit has the given
59  	 * flag; if it does not already have the flag than the commit is added to
60  	 * the queue and the flag is set. This later will prevent the commit from
61  	 * being added twice.
62  	 *
63  	 * @param c
64  	 *            commit whose parents should be added.
65  	 * @param queueControl
66  	 *            flag that controls admission to the queue.
67  	 */
68  	public final void addParents(RevCommit c, RevFlag queueControl) {
69  		final RevCommit[] pList = c.parents;
70  		if (pList == null) {
71  			return;
72  		}
73  		for (int i = 0; i < pList.length; i++) {
74  			if (firstParent && i > 0) {
75  				break;
76  			}
77  			add(pList[i], queueControl);
78  		}
79  	}
80  
81  	/**
82  	 * {@inheritDoc}
83  	 * <p>
84  	 * Remove the first commit from the queue.
85  	 */
86  	@Override
87  	public abstract RevCommit next();
88  
89  	/**
90  	 * Remove all entries from this queue.
91  	 */
92  	public abstract void clear();
93  
94  	abstract boolean everbodyHasFlag(int f);
95  
96  	abstract boolean anybodyHasFlag(int f);
97  
98  	@Override
99  	int outputType() {
100 		return outputType;
101 	}
102 
103 	/**
104 	 * Describe this queue
105 	 *
106 	 * @param s
107 	 *            a StringBuilder
108 	 * @param c
109 	 *            a {@link org.eclipse.jgit.revwalk.RevCommit}
110 	 */
111 	protected static void describe(StringBuilder s, RevCommit c) {
112 		s.append(c.toString());
113 		s.append('\n');
114 	}
115 
116 	private static class AlwaysEmptyQueue extends AbstractRevQueue {
117 		private AlwaysEmptyQueue() {
118 			super(false);
119 		}
120 
121 		@Override
122 		public void add(RevCommit c) {
123 			throw new UnsupportedOperationException();
124 		}
125 
126 		@Override
127 		public RevCommit next() {
128 			return null;
129 		}
130 
131 		@Override
132 		boolean anybodyHasFlag(int f) {
133 			return false;
134 		}
135 
136 		@Override
137 		boolean everbodyHasFlag(int f) {
138 			return true;
139 		}
140 
141 		@Override
142 		public void clear() {
143 			// Nothing to clear, we have no state.
144 		}
145 
146 	}
147 }