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  
18  /**
19   * A queue of commits in FIFO order.
20   */
21  public class FIFORevQueue extends BlockRevQueue {
22  	private Block head;
23  
24  	private Block tail;
25  
26  	/** Create an empty FIFO queue. */
27  	public FIFORevQueue() {
28  		super(false);
29  	}
30  
31  	FIFORevQueue(boolean firstParent) {
32  		super(firstParent);
33  	}
34  
35  	FIFORevQueue(Generator s) throws MissingObjectException,
36  			IncorrectObjectTypeException, IOException {
37  		super(s);
38  	}
39  
40  	/** {@inheritDoc} */
41  	@Override
42  	public void add(RevCommit c) {
43  		Block b = tail;
44  		if (b == null) {
45  			b = free.newBlock();
46  			b.add(c);
47  			head = b;
48  			tail = b;
49  			return;
50  		} else if (b.isFull()) {
51  			b = free.newBlock();
52  			tail.next = b;
53  			tail = b;
54  		}
55  		b.add(c);
56  	}
57  
58  	/**
59  	 * Insert the commit pointer at the front of the queue.
60  	 *
61  	 * @param c
62  	 *            the commit to insert into the queue.
63  	 */
64  	public void unpop(RevCommit c) {
65  		Block b = head;
66  		if (b == null) {
67  			b = free.newBlock();
68  			b.resetToMiddle();
69  			b.add(c);
70  			head = b;
71  			tail = b;
72  			return;
73  		} else if (b.canUnpop()) {
74  			b.unpop(c);
75  			return;
76  		}
77  
78  		b = free.newBlock();
79  		b.resetToEnd();
80  		b.unpop(c);
81  		b.next = head;
82  		head = b;
83  	}
84  
85  	/** {@inheritDoc} */
86  	@Override
87  	public RevCommit next() {
88  		final Block b = head;
89  		if (b == null)
90  			return null;
91  
92  		final RevCommit c = b.pop();
93  		if (b.isEmpty()) {
94  			head = b.next;
95  			if (head == null)
96  				tail = null;
97  			free.freeBlock(b);
98  		}
99  		return c;
100 	}
101 
102 	/** {@inheritDoc} */
103 	@Override
104 	public void clear() {
105 		head = null;
106 		tail = null;
107 		free.clear();
108 	}
109 
110 	@Override
111 	boolean everbodyHasFlag(int f) {
112 		for (Block b = head; b != null; b = b.next) {
113 			for (int i = b.headIndex; i < b.tailIndex; i++)
114 				if ((b.commits[i].flags & f) == 0)
115 					return false;
116 		}
117 		return true;
118 	}
119 
120 	@Override
121 	boolean anybodyHasFlag(int f) {
122 		for (Block b = head; b != null; b = b.next) {
123 			for (int i = b.headIndex; i < b.tailIndex; i++)
124 				if ((b.commits[i].flags & f) != 0)
125 					return true;
126 		}
127 		return false;
128 	}
129 
130 	void removeFlag(int f) {
131 		final int not_f = ~f;
132 		for (Block b = head; b != null; b = b.next) {
133 			for (int i = b.headIndex; i < b.tailIndex; i++)
134 				b.commits[i].flags &= not_f;
135 		}
136 	}
137 
138 	/** {@inheritDoc} */
139 	@Override
140 	public String toString() {
141 		final StringBuilder s = new StringBuilder();
142 		for (Block q = head; q != null; q = q.next) {
143 			for (int i = q.headIndex; i < q.tailIndex; i++)
144 				describe(s, q.commits[i]);
145 		}
146 		return s.toString();
147 	}
148 }