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(final RevCommit c, final 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(final RevCommit c, final 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 	 * Remove the first commit from the queue.
107 	 *
108 	 * @return the first commit of this queue.
109 	 */
110 	public abstract RevCommit next();
111 
112 	/** Remove all entries from this queue. */
113 	public abstract void clear();
114 
115 	abstract boolean everbodyHasFlag(int f);
116 
117 	abstract boolean anybodyHasFlag(int f);
118 
119 	@Override
120 	int outputType() {
121 		return outputType;
122 	}
123 
124 	protected static void describe(final StringBuilder s, final RevCommit c) {
125 		s.append(c.toString());
126 		s.append('\n');
127 	}
128 
129 	private static class AlwaysEmptyQueue extends AbstractRevQueue {
130 		@Override
131 		public void add(RevCommit c) {
132 			throw new UnsupportedOperationException();
133 		}
134 
135 		@Override
136 		public RevCommit next() {
137 			return null;
138 		}
139 
140 		@Override
141 		boolean anybodyHasFlag(int f) {
142 			return false;
143 		}
144 
145 		@Override
146 		boolean everbodyHasFlag(int f) {
147 			return true;
148 		}
149 
150 		@Override
151 		public void clear() {
152 			// Nothing to clear, we have no state.
153 		}
154 
155 	}
156 }