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