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  abstract class AbstractRevQueue extends Generator {
47  	static final AbstractRevQueue EMPTY_QUEUE = new AlwaysEmptyQueue();
48  
49  	/** Current output flags set for this generator instance. */
50  	int outputType;
51  
52  	/**
53  	 * Add a commit to the queue.
54  	 * <p>
55  	 * This method always adds the commit, even if it is already in the queue or
56  	 * previously was in the queue but has already been removed. To control
57  	 * queue admission use {@link #add(RevCommit, RevFlag)}.
58  	 *
59  	 * @param c
60  	 *            commit to add.
61  	 */
62  	public abstract void add(RevCommit c);
63  
64  	/**
65  	 * Add a commit if it does not have a flag set yet, then set the flag.
66  	 * <p>
67  	 * This method permits the application to test if the commit has the given
68  	 * flag; if it does not already have the flag than the commit is added to
69  	 * the queue and the flag is set. This later will prevent the commit from
70  	 * being added twice.
71  	 *
72  	 * @param c
73  	 *            commit to add.
74  	 * @param queueControl
75  	 *            flag that controls admission to the queue.
76  	 */
77  	public final void add(RevCommit c, RevFlag queueControl) {
78  		if (!c.has(queueControl)) {
79  			c.add(queueControl);
80  			add(c);
81  		}
82  	}
83  
84  	/**
85  	 * Add a commit's parents if one does not have a flag set yet.
86  	 * <p>
87  	 * This method permits the application to test if the commit has the given
88  	 * flag; if it does not already have the flag than the commit is added to
89  	 * the queue and the flag is set. This later will prevent the commit from
90  	 * being added twice.
91  	 *
92  	 * @param c
93  	 *            commit whose parents should be added.
94  	 * @param queueControl
95  	 *            flag that controls admission to the queue.
96  	 */
97  	public final void addParents(RevCommit c, RevFlag queueControl) {
98  		final RevCommit[] pList = c.parents;
99  		if (pList == null)
100 			return;
101 		for (RevCommit p : pList)
102 			add(p, queueControl);
103 	}
104 
105 	/**
106 	 * {@inheritDoc}
107 	 * <p>
108 	 * Remove the first commit from the queue.
109 	 */
110 	@Override
111 	public abstract RevCommit next();
112 
113 	/**
114 	 * Remove all entries from this queue.
115 	 */
116 	public abstract void clear();
117 
118 	abstract boolean everbodyHasFlag(int f);
119 
120 	abstract boolean anybodyHasFlag(int f);
121 
122 	@Override
123 	int outputType() {
124 		return outputType;
125 	}
126 
127 	/**
128 	 * Describe this queue
129 	 *
130 	 * @param s
131 	 *            a StringBuilder
132 	 * @param c
133 	 *            a {@link org.eclipse.jgit.revwalk.RevCommit}
134 	 */
135 	protected static void describe(StringBuilder s, RevCommit c) {
136 		s.append(c.toString());
137 		s.append('\n');
138 	}
139 
140 	private static class AlwaysEmptyQueue extends AbstractRevQueue {
141 		@Override
142 		public void add(RevCommit c) {
143 			throw new UnsupportedOperationException();
144 		}
145 
146 		@Override
147 		public RevCommit next() {
148 			return null;
149 		}
150 
151 		@Override
152 		boolean anybodyHasFlag(int f) {
153 			return false;
154 		}
155 
156 		@Override
157 		boolean everbodyHasFlag(int f) {
158 			return true;
159 		}
160 
161 		@Override
162 		public void clear() {
163 			// Nothing to clear, we have no state.
164 		}
165 
166 	}
167 }