Skip to content

Commit

Permalink
Add a bounded-size version of the in-memory message queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Whitehouse committed Jul 27, 2023
1 parent dce91e6 commit 37b4d9c
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package quickfix;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* A bounded-size version of InMemoryMessageQueue.
* @see InMemoryMessageQueue
*/
public class BoundInMemoryMessageQueue implements MessageQueue {
// The map should be accessed from a single thread
private final Map<Integer, Message> backingMap = new LinkedHashMap<>();
private final int maxSize;

public BoundInMemoryMessageQueue(int maxSize) {
this.maxSize = maxSize;
}

public void enqueue(int sequence, Message message) {
if (backingMap.size() >= maxSize) {
List<Integer> keys = backingMap.keySet().stream().sorted().collect(Collectors.toList());
if (sequence < keys.get(0)) {
backingMap.remove(keys.get(keys.size()-1));
backingMap.put(sequence, message);
}
} else {
this.backingMap.put(sequence, message);
}
}

public Message dequeue(int sequence) {
return (Message) this.backingMap.remove(sequence);
}

public void clear() {
this.backingMap.clear();
}

Map<Integer, Message> getBackingMap() {
return this.backingMap;
}
}

0 comments on commit 37b4d9c

Please sign in to comment.