blob: 233dd64a3c73d2ec5bb81cd564ac1e289e7dc4fe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/*
* Copyright (C) 2023, GerritForge Ltd
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.revwalk;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.internal.JGitText;
import java.io.IOException;
import java.util.Comparator;
import java.util.PriorityQueue;
/**
* A queue of commits sorted by commit time order using a Java PriorityQueue.
* For the commits with the same commit time insertion order will be preserved.
*/
class DateRevPriorityQueue extends DateRevQueue {
private PriorityQueue<RevCommitEntry> queue;
private final AtomicInteger sequence = new AtomicInteger(1);
/**
* Create an empty queue of commits sorted by commit time order.
*/
public DateRevPriorityQueue() {
this(false);
}
/**
* Create an empty queue of commits sorted by commit time order.
*
* @param firstParent
* treat first element as a parent
*/
DateRevPriorityQueue(boolean firstParent) {
super(firstParent);
initPriorityQueue();
}
private void initPriorityQueue() {
sequence.set(1);
queue = new PriorityQueue<>(Comparator.comparingInt(
(RevCommitEntry ent) -> ent.getEntry().getCommitTime())
.reversed()
.thenComparingInt(RevCommitEntry::getInsertSequenceNumber));
}
DateRevPriorityQueue(Generator s) throws MissingObjectException,
IncorrectObjectTypeException, IOException {
this(s.firstParent);
for (;;) {
final RevCommit c = s.next();
if (c == null) {
break;
}
add(c);
}
}
@Override
public void add(RevCommit c) {
// PriorityQueue does not accept null values. To keep the same behaviour
// do the same check and throw the same exception before creating entry
if (c == null) {
throw new NullPointerException(JGitText.get().nullRevCommit);
}
queue.add(new RevCommitEntry(sequence.getAndIncrement(), c));
}
@Override
public RevCommit next() {
RevCommitEntry entry = queue.poll();
return entry == null ? null : entry.getEntry();
}
/**
* Peek at the next commit, without removing it.
*
* @return the next available commit; null if there are no commits left.
*/
@Override
public @Nullable RevCommit peek() {
RevCommitEntry entry = queue.peek();
return entry == null ? null : entry.getEntry();
}
/**
* {@inheritDoc}
*/
@Override
public void clear() {
sequence.set(1);
queue.clear();
}
@Override
boolean everbodyHasFlag(int f) {
return queue.stream().map(RevCommitEntry::getEntry)
.noneMatch(c -> (c.flags & f) == 0);
}
@Override
boolean anybodyHasFlag(int f) {
return queue.stream().map(RevCommitEntry::getEntry)
.anyMatch(c -> (c.flags & f) != 0);
}
@Override
int outputType() {
return outputType | SORT_COMMIT_TIME_DESC;
}
@Override
public String toString() {
final StringBuilder s = new StringBuilder();
for (RevCommitEntry e : queue) {
describe(s, e.getEntry());
}
return s.toString();
}
private static class RevCommitEntry {
private final int insertSequenceNumber;
private final RevCommit entry;
public RevCommitEntry(int insertSequenceNumber, RevCommit entry) {
this.insertSequenceNumber = insertSequenceNumber;
this.entry = entry;
}
public int getInsertSequenceNumber() {
return insertSequenceNumber;
}
public RevCommit getEntry() {
return entry;
}
}
}
|