Skip to content

Commit

Permalink
make forEach return elements in required order
Browse files Browse the repository at this point in the history
  • Loading branch information
dlg99 committed May 3, 2024
1 parent 199c139 commit 29b1b3b
Showing 1 changed file with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,20 @@ static int getPriority(Message m) {
private final PriorityBlockingQueue<Message> queue;
private final AtomicBoolean terminated = new AtomicBoolean(false);

private static final Comparator<Message> comparator =
(o1, o2) -> {
int priority1 = getPriority(o1);
int priority2 = getPriority(o2);
return Integer.compare(priority2, priority1);
};

public MessagePriorityGrowableArrayBlockingQueue() {
this(10);
}

public MessagePriorityGrowableArrayBlockingQueue(int initialCapacity) {
queue =
new PriorityBlockingQueue<>(
initialCapacity,
new Comparator<Message>() {
@Override
public int compare(Message o1, Message o2) {
int priority1 = getPriority(o1);
int priority2 = getPriority(o2);
return Integer.compare(priority2, priority1);
}
});
new PriorityBlockingQueue<>(initialCapacity, comparator);
}

@Override
Expand Down Expand Up @@ -145,7 +143,7 @@ public List<Message> toList() {

@Override
public void forEach(Consumer<? super Message> action) {
queue.forEach(action);
queue.stream().sorted(comparator).forEach(action);
}

@Override
Expand Down

0 comments on commit 29b1b3b

Please sign in to comment.